Android用VideoView实现MP4作为页面背景(仿QQ登录页面效果)

类似Tumblr, Spotify, Keep等应用在登录界面都有要采用了背景是动画的效果。


1、布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.huan.percy.backgroundvideotest.FullScreenVideoView
        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" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="手机/邮箱"
            android:textColor="#fff"
            android:textColorHint="#fff"
            android:background="@null"
            android:textSize="20sp" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/fls"
            android:scaleType="fitXY"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:hint="密码"
            android:textColorHint="#fff"
            android:textColor="#fff"
            android:background="@null"
            android:textSize="20sp" />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/fls"
            android:scaleType="fitXY"/>

        <Button
            android:id="@+id/login"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@mipmap/nhl"
            android:layout_marginTop="30dp"
            android:text="登 录"
            android:textColor="#fff"
            android:textSize="25sp" />
    </LinearLayout>

</RelativeLayout>
2、在raw文件夹中添加播放的MP4资源

3、自定义VideoView,使得播放视频填充屏幕

/**
 * Created by ZhuMing on 2017/7/12.
 * 自定义 填充屏幕的VideoView
 */
public class FullScreenVideoView extends VideoView {
    public FullScreenVideoView(Context context) {
        super(context);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        setMeasuredDimension(width, height);
    }
}

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

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mp = null;
    private FullScreenVideoView myVideoView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        final String videoPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.welcome_video).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();
            }
        });
    }

    private void initView() {
        myVideoView = (FullScreenVideoView) findViewById(R.id.videoView);
    }

}



demo下载链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值