有时候大家看到QQ等软件的引导页,播放一段酷炫的视频,觉得很有意思,于是就会想要尝试自己去实现以下。其实很简单,用VideoView就可以实现。
首先,创建定义的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);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//我们重新计算高度(这样就可以匹配不同手机的尺寸)
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
LauncherActivity:
public class LauncherActivity extends AppCompatActivity implements View.OnClickListener {
private CustomVideoView vv;
private Button btn_start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
vv = (CustomVideoView) findViewById(R.id.videoview);
btn_start = (Button) findViewById(R.id.btn_start);
btn_start.setOnClickListener(this);
initView();
}
private void initView() {
//设置播放加载路径
vv.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.wildlife));
//播放
vv.start();
//循环播放
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
vv.start();
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_start:
Toast.makeText(this,"进入了主页",Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, MainActivity.class));
finish();
break;
}
}
}
这里播放了raw目录下的一段视频,wildlife.3gp。
activity_launcher.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="org.hunter.videobootpage.LauncherActivity">
<org.hunter.videobootpage.CustomVideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/btn_start"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="56dp"
android:text="进入"/>
</android.support.constraint.ConstraintLayout>
直接运行就能看到效果了。So easy!
源码下载:https://github.com/gengqifu/VideoBootPage