本文介绍了android 仿QQ动态背景、视频背景的示例代码,分享给大家,具体如下:
别人的效果图如下:
1, 自定义视频类 继承VideoView
public class BackVideoView extends VideoView {
public BackVideoView (Context context) {
super(context);
}
public BackVideoView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public BackVideoView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr, int
defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@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);
}
}
MainActivity 中:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private BackVideoView videoview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化
*/
private void initView() {
videoview = (BackVideoView ) findViewById(R.id.videoview);
videoview.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.shipin2));
//播放
videoview.start();
//循环播放
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoview.start();
mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
return false;
}
});
}
});
}
//重启加载 防止退出返回时视频黑屏
@Override
protected void onRestart() {
super.onRestart();
initView();
}
//防止锁屏或者切出的时候音乐还在播放
@Override
protected void onStop() {
super.onStop();
videoview.stopPlayback();
}
}
读取视频的问题: 把视频放到和res/raw文件夹里面了
布局文件中:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
>
<!--BackVideoView 自定义视频类的位置 包名+类名-->
<com.hcx.ocrapp.ui.video.BackVideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
注意:
视频资源要添加res文件夹下创建raw文件夹
以上就是本文的全部内容,希望对大家的学习有所帮助。