天天看点

Android 实现引导页视频播放效果

效果图:素材资源以QQ音乐分享代替,后续可以替换为自己需要的视频资源 

Android 实现引导页视频播放效果
  • 自定义view集成VideoView
package com.ex.theme.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;

/**
 * @author sd
 * @date 2019/4/1
 */
public class CustomView extends VideoView {

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        计算屏幕宽高
        int width = getDefaultSize(0, widthMeasureSpec);
        int heigth = getDefaultSize(0, heightMeasureSpec);
        setMeasuredDimension(width, heigth);
    }
}
           
  •  布局页面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.ex.theme.view.CustomView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/tv_jump"
        android:text="跳过"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:layout_margin="10dp"
        android:textSize="16sp"
        android:textColor="@color/color_green8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

     <Button
        android:id="@+id/bt_voice"
        android:text="@string/splash_bt_voice"
        android:drawableRight="@drawable/open_voice"
        android:visibility="gone"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:layout_margin="15dp"
        android:textSize="16sp"
        android:textColor="@color/color_green5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <Button
        android:visibility="gone"
        android:id="@+id/bt_start"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="56dp"
        android:text="欢迎进入"/>

</RelativeLayout>
           
  • 编辑SplashActivity页代码
package com.ex.theme.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.ex.theme.R;
import com.ex.theme.view.CustomView;

/**
 * @author sd
 * @date 2019/4/1    app启动 导航页
 */
public class SplashActivity extends Activity implements View.OnClickListener {

    private CustomView mVideoView;
    private Button mBtStart;
    private Button mTvJump;
    private int recLen = 15;                  //media             //视频的时间一般控制控制在5~6s
    private Thread mThread;
    private MediaPlayer mVoice;     //声音

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        initView();
        initData();
    }

    private void initView() {
        mTvJump = findViewById(R.id.tv_jump);                 //跳过
        mBtStart = findViewById(R.id.bt_start);              //欢迎按钮
        mBtStart.setAlpha(0.6f);                             //按钮的透明度
        mTvJump.setOnClickListener(this);
        mBtStart.setOnClickListener(this);
        mVideoView = findViewById(R.id.video_view);

        mThread = new Thread(new Runnable() {
             @Override
             public void run() {
                     while (true){
                         try {
                             Thread.sleep(1000);
                             Message message = new Message();
                             message.what =1;
                             mHandler.sendMessage(message);
                         } catch (Exception e) {
                             e.printStackTrace();
                         }
                     }
             }
         });
       mThread.start();
    }

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    recLen--;
                if (recLen <=0){
                    mTvJump.setVisibility(View.GONE);
                    mHandler.removeCallbacks(mThread);          //移除
                }else {
                    mTvJump.setText("跳过 "+recLen);
                }
            }
            super.handleMessage(msg);
        }
    };

    private void initData() {
        // 加载视频资源
        mVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.all));
        //播放
        mVideoView.start();
        //循环播放
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                 mVoice = mp;                                    //控制声音的变量
                mBtStart.setVisibility(View.VISIBLE);                    //一轮视频结束后,显示欢迎进入按钮
                mVideoView.start();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_jump:                  //跳过
//                mVideoView.stopPlayback();                  //停止播放
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
                finish();                        //退出
                break;
            case R.id.bt_voice:                 //声音按钮
                if (mBtVoice.getText().equals("关闭声音")){
                    mVoice.setVolume(0f,0f);            //关闭声音
                    mBtVoice.setText("打开声音");
                    mBtVoice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.close_voice,0);
                }else if (mBtVoice.getText().equals("打开声音")){
                    mVoice.setVolume(1,1);              //打开声音 
                    mBtVoice.setText("关闭声音");
                    mBtVoice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.open_voice,0);
                }
                break;
            case R.id.bt_start:                     //欢迎进入
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
                finish();
                break;
            default:
                break;
        }
    }
}
           
  •  视频资源放在res文件raw下:
Android 实现引导页视频播放效果

继续阅读