本文介绍了android 仿QQ动态背景、视频背景的示例代码,分享给大家,具体如下:
效果如下:
如上图类似效果图:
1, 自定义视频类 继承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 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); } }
MainActivity 中:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private CustomVideoView videoview; private Button btn_enter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); } /** * 初始化 */ private void initView() { btn_enter = (Button) findViewById(R.id.btn_enter); btn_enter.setOnClickListener(this); videoview = (CustomVideoView) 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 public void onClick(View view) { switch (view.getId()){ case R.id.btn_enter: Toast.makeText(this,"登录成功了",Toast.LENGTH_SHORT).show(); break; } } //返回重启加载 @Override protected void onRestart() { super.onRestart(); initView(); } //防止锁屏或者切出的时候,音乐在播放 @Override protected void onStop() { super.onStop(); videoview.stopPlayback(); } }
读取视频的问题: 把视频放到和res/raw文件夹里面了
布局文件中:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
注意:
视频资源要添加res文件夹下创建raw文件夹
需要在onRestart()方法里重新加载视频,防止退出返回时视频黑屏
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。