android view保存成背景,Android用VideoView实现MP4作为页面背景

类似Tumblr, Spotify, Keep等应用在登录界面都有要采用了背景是动画的效果。自己现在做课程设计,也想使用,所以经过捣鼓以后实现如下图:

a1697737fbae

动态背景.gif

1. 新建一个用于显示VideoView背景mp4的布局文件 video_background.xml

背景mp4的布局文件 video_background.xml:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/videoView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentBottom="true"

android:layout_alignParentTop="true"

android:layout_gravity="center" />

2. 在主内容布局中include上面的布局

承载背景布局的主内容布局需要定义为RelativeLayout布局,否则背景布局会挤占空间。

主内容布局文件:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:fitsSystemWindows="true">

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_centerInParent="true"

android:background="@color/transparent_background"

android:paddingTop="56dp"

android:paddingLeft="24dp"

android:paddingRight="24dp">

....

3. 保留原布局的方法:

在主内容布局中申明一个RelativeLayout布局,include背景布局,然后,将原布局全部嵌套在该RelativeLayout布局中,并在原布局的最外一层设置属性

android:layout_centerInParent="true"

4. 在raw资源文件夹中放入需要播放的mp4文件

5. 在需要加载动态背景的Activity的OnCreate()方法中加入相关控制代码,实现自动循环播放

Activity中的OnCreate中加入代码:

myVideoView = (VideoView) findViewById(R.id.videoView);

final String videoPath = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.要播放的mp4文件).toString();

myVideoView.setVideoPath(videoPath);

myVideoView.start();

myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

@Override

public void onPrepared(MediaPlayer mp) {

mp.start();

mp.setLooping(true);

}});

myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override

public void onCompletion(MediaPlayer mp) {

myVideoView.setVideoPath(videoPath);

myVideoView.start();

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值