(仿头条APP项目)1.app载入界面相关功能实现

app载入界面相关功能实现

模块功能

1.设置页面布局

(仿头条APP项目)1.app载入界面相关功能实现_第1张图片

2.初始化视频播放

 //控制是否跳到主界面
    private boolean isjump = false;
    private VideoView videoView;
    private void startSplash() {
        //播放地址
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
        //初始化vdeoView对象
         videoView = findViewById(R.id.vv_splash);
        //设置视频播放地址
        videoView.setVideoURI(uri);
        //开始播放
        videoView.start();
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if(!isjump){
                    //关闭当前界面
                    finish();
                    startActivity(new Intent(SplashActivity.this,MainActivity.class));

                }
            }
        });

    }

3.跳过载入页面功能

private void listenJumpBtn() {
        //初始化Button对象
        Button jump = findViewById(R.id.bt_jump);
        //绑定按钮点击事件
        jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isjump = true;
                //关闭当前界面
                finish();
                //跳转到主界面
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
            }
        });
    }

4.设置屏幕大小覆盖全屏

    private void setVideoSize() {
        //获取屏幕尺寸对象
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        //创建布局参数
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(displayMetrics.widthPixels,displayMetrics.heightPixels);
        //设置给videoview
        videoView.setLayoutParams(params);
        //低版本模拟器上添加
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    }

总Acrivity代码

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.example.administrator.zhjrtt.R;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //1.设置页面布局
        //2.初始化视频播放
        startSplash();
        //3.跳转功能
        listenJumpBtn();
        //4.将画面设置成屏幕大小
        setVideoSize();
    }

    private void setVideoSize() {
        //获取屏幕尺寸对象
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        //创建布局参数
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(displayMetrics.widthPixels,displayMetrics.heightPixels);
        //设置给videoview
        videoView.setLayoutParams(params);
        //低版本模拟器上添加
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    }

    private void listenJumpBtn() {
        //初始化Button对象
        Button jump = findViewById(R.id.bt_jump);
        //绑定按钮点击事件
        jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isjump = true;
                //关闭当前界面
                finish();
                //跳转到主界面
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
            }
        });
    }

    //控制是否跳到主界面
    private boolean isjump = false;
    private VideoView videoView;
    private void startSplash() {
        //播放地址
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
        //初始化vdeoView对象
         videoView = findViewById(R.id.vv_splash);
        //设置视频播放地址
        videoView.setVideoURI(uri);
        //开始播放
        videoView.start();
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if(!isjump){
                    //关闭当前界面
                    finish();
                    startActivity(new Intent(SplashActivity.this,MainActivity.class));

                }
            }
        });

    }
}

结果展示

(仿头条APP项目)1.app载入界面相关功能实现_第2张图片

你可能感兴趣的:(仿头条APP项目)