先看效果图: (GIF动图太大,所以删帧加快。实际原视频怎么样就是怎么样)
话不多说,开工
首先重新定义一个视频类继承videoView类:
public class CustomVideoView extends VideoView {
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@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);
}
}
然后布局中应用这个重新定义的视频控件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.hsfappdemo.videobglogin.CustomVideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<EditText
android:id="@+id/et_user_name"
android:layout_width="400dp"
android:layout_height="50dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="300dp"
android:ems="10"
android:hint="用户名"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/et_pwd"
android:layout_width="400dp"
android:layout_height="50dp"
android:inputType="textPassword"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:layout_alignTop="@+id/et_user_name"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:ems="10"
android:hint="密码" />
<Button
android:id="@+id/btn_login"
android:textColor="#ffffff"
android:layout_width="400dp"
android:layout_height="50dp"
android:onClick="login"
android:layout_below="@+id/et_pwd"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="登陆" />
<TextView
android:id="@+id/textView1"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="@+id/btn_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="#ffffff"
android:text="|" />
<TextView
android:id="@+id/tv_reg"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@+id/textView1"
android:text="注册账号" />
<TextView
android:id="@+id/tv_remember_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginRight="50dp"
android:layout_toLeftOf="@+id/textView1"
android:text="忘记密码"
android:textColor="#ffffff" />
</RelativeLayout>
然后定义加载并播放视频背景的方法:
//CustomVideoView videoview = (CustomVideoView)findviewbyId(R.id.videoview);
private void initVideoView() {
// 设置播放加载路径(R.raw.loginbg是视频文件的id)
videoview.setVideoURI(Uri.parse("android.resource://"
+ getPackageName() + "/" + R.raw.loginbg));
// 播放
videoview.start();
// 循环播放
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoview.start();
}
});
videoview.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return true;// 如果设置true就可以防止他弹出错误的提示框!
}
});
}
使用:
public class LoginActivity extends Activity {
CustomVideoView videoview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(this, R.layout.activity_login_layout);
videoview = (CustomVideoView)findviewbyId(R.id.videoview);
initVideoView();
}
}
注意:
// 返回重启加载
@Override
protected void onRestart() {
initVideoView();
super.onRestart();
}
// 防止锁屏或者切出的时候,音乐在播放
@Override
protected void onStop() {
videoview.stopPlayback();
super.onStop();
}