Android APP实现视频背景

步骤

1.导入Class
2.在需要使用的Activity中重写方法,加载本地资源
3.使用帧,布局中使用自定义Video控件并FULL全屏

第一步 导入重写的VideoView class

package com.scxx.wangli_qq.UserControl;import android.content.Context;import android.media.MediaPlayer;import android.util.AttributeSet;import android.view.KeyEvent;import android.widget.VideoView;public class UserVideoView extends VideoView {    public UserVideoView(Context context) {        super(context);    }    public UserVideoView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public UserVideoView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        //我们重新计算高度        int width = getDefaultSize(0, widthMeasureSpec);        int height = getDefaultSize(0, heightMeasureSpec);        setMeasuredDimension(width, height);    }    @Override    public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {        super.setOnPreparedListener(l);    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        return super.onKeyDown(keyCode, event);    }}

在需要使用的Activity中重写方法,加载本地资源

/**     * 视频加载     */    //返回重启加载    @Override    protected void onRestart() {        super.onRestart();        InitView();    }    //防止锁屏或者切出的时候,音乐在播放    @Override    protected void onStop() {        super.onStop();        userVideoView.stopPlayback();    }    public void InitView()    {        userVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video));        userVideoView.start();        userVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {            @Override            public void onCompletion(MediaPlayer mp) {                userVideoView.start();            }        });    }

InitView函数需要在onCreate方法中调用,才能生效
R.raw.video为视频资源

使用帧

使用帧布局,并把自定义控件放在第一位

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.scxx.wangli_qq.UserControl.UserVideoView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/Video_player"/></FrameLayout>

更多相关文章

  1. android ListView向上滑动隐藏标题,下拉显示标题栏
  2. Android获取assets目录下的文件和图片
  3. EventBus源码解析
  4. 菜单栏上没有android机器人的图标(AVD)的解决方法
  5. Android(安卓)root的两种方法 -- udev漏洞和setuid漏洞
  6. Android(安卓)listview多类型item问题
  7. android 铃声设置流程
  8. android 中如何获取camera当前状态
  9. android获取时间差的方法

随机推荐

  1. 一触即发——App启动优化最佳实践
  2. [Android] Gradle 安装
  3. Android(安卓)Launcher 研究学习
  4. Drawable资源——LevelListDrawable 级列
  5. android一个小网络图片查看器
  6. Android高手进阶教程(十)-----Android(安
  7. android音量控制setVolumeControlStream
  8. Android(安卓)Studio Gradle 对应的下载
  9. Android(安卓)闪屏页与引导页制作
  10. Android(安卓)NDK R8 环境部署