第一步話就是設定我們的布局檔案的開發:
先創建我們的activity即是我們的splashActivity:
然后的話就是創建我們的 FullScreenVideoView.java檔案相當于是我們自建的videoview

package com.example.moneyandroid;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;
public class FullScreenVideoView extends VideoView {
// 這個構造方法主要用于直接new出來的物件
public FullScreenVideoView(Context context) {
super(context);
}
// 這個構造方法主要用于xml檔案中,支持自定義的樣式
public FullScreenVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
// 這個構造方法主要用于xml檔案中,支持自定義的樣式
public FullScreenVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//重寫我們的onMeasure
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// widthMeasureSpec 包含兩個主要的內容: 1: 測量模 2: 大小
int with = getDefaultSize(0,widthMeasureSpec);
int height = getDefaultSize(0,heightMeasureSpec);
setMeasuredDimension(with,height);
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/*
* todo 現在的話就是我們的模式的講解,就是我們在使用我們的這個方式的時候會用到的模式
* EXACTLY 的模式的話就是我們的 精確的模式-----------》 如果系統呼叫的是該模式的話、
* 這時的話控制元件的大小會隨著子控制元件或是內容的變化而變化----》 不超出父控制元件允許的最大范圍即可
*
* AT_MOST -----> 該模式的話是我們的最大值的測量模式
*
*
*UnsPECIFIDE 該模式只有我們自定義控制元件的時候才會用到 view 想要多大就多大
*
* tetxview 系統提供的處理文本的控制元件
*
* */
}
}
然后的話就是設定我們的splash.xml檔案:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashActivity">
<!--todo 使用的是我么的幀布局我們的布局的話就是可以和我們的控制元件相互重疊-->
<com.example.moneyandroid.FullScreenVideoView
android:id="@+id/video_play"
style="@style/video_view_style" />
<!--接下來的話就是我們的倒計時的設定-->
<TextView
android:id="@+id/times"
android:textSize="18dp"
android:textColor="#FF6200EE"
android:layout_gravity="right"
android:gravity="center"
android:text="跳過"
android:background="@drawable/drawable"
android:layout_marginTop="50dp"
android:layout_width="90dp"
android:layout_marginRight="50dp"
android:layout_height="wrap_content"/>
<!--dp android距離單位 sp的話android的字體大小單位 px 像素單位-->
</FrameLayout>
同時在我們的res的目錄下創建我們的就是rew檔案目錄然后將我們準備好的視屏放置進去:

同時將我們的themes檔案的actionbar改為noactionbar:

然后的話就是撰寫我們的相關的java代碼:

package com.example.moneyandroid;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.TextView;
import android.widget.VideoView;
import java.io.File;
public class SplashActivity extends AppCompatActivity {
private FullScreenVideoView videoPlay;
private TextView times;
private CustomCountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) { // todo Bundle 存盤資料的一個載體
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash); // 這個方法的意思-----》 把我們自定義的layout加入到id為content的viewGroup中
// todo 現在的話這個oncreate方法是我們oncreat的生命周期
initView();
}
private void initView() {
videoPlay = (FullScreenVideoView) findViewById(R.id.video_play);
times = (TextView) findViewById(R.id.times);
times.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// todo 顯示的啟動 ---------------》然后的話就是設定我們的頁面的跳轉然后的話就是設定我們的主界面
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
});
videoPlay.setVideoURI(Uri.parse("android.resource://"+getPackageName() + File.separator+R.raw.splash));
// 回呼的介面
videoPlay.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start(); // 接下來的話就是設定我們的視頻的播放完成
}
});
// 資料回呼 todo 確定我們的播放器播放完成后就是再次的呼叫我們呢的回呼再次播放
videoPlay.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();// 接下來的話就是證明我們的自定義view的作用是有用的
}
});
timer = new CustomCountDownTimer(5,
new CustomCountDownTimer.ICountDownHandle() {
@Override
public void onTicker(int time) {
times.setText(time + "秒");
}
@Override
public void onFinesh() {
times.setText("跳過");
}
});
timer.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
timer.cancel(); // 銷毀我們的就是actiity
}
}
同時創建我們的就是啊CustomCountDownTimer.java檔案然后的話就是設定我們的handle撰寫我們的倒計時:

package com.example.moneyandroid;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
public class CustomCountDownTimer implements Runnable{
private int time;
private int countDowntime;
private final ICountDownHandle iCountDownHandle;
private boolean isRunn;
private final android.os.Handler handler1;
// 實時去回呼 這個時間是什么時間,倒計時是幾秒,使用到的設計模式觀察者設計模式
// 支持傳入的總時間
// 每過一秒,總秒數減去1
// 總時間倒計時為0時要回呼完成的狀態
// todo handle ----------------> handle --------------> 執行緒之間的通信
public CustomCountDownTimer(int time, ICountDownHandle iCountDownHandle){
handler1 = new android.os.Handler();
this.time = time;
this.countDowntime = time; // 設定它的值
this.iCountDownHandle = iCountDownHandle;
}
// 實作的具體邏輯
@Override
public void run() {
// todo ------------>第三步------------》
if(isRunn){
if(iCountDownHandle != null){
iCountDownHandle.onTicker(countDowntime);
}if(countDowntime == 0){
if(iCountDownHandle != null){
iCountDownHandle.onFinesh();
}
}else{
countDowntime = time --;
handler1.postDelayed(this,1000); // 延時1秒
}
}
}
public void start(){ // 開啟倒計時
isRunn = true;
handler1.post(this);
}
// 這個的話就是我們的
public void cancel(){ // 結束倒計時
isRunn = false;
handler1.removeCallbacks(this);
}
// 定義一個介面(ioc資料回呼)
public interface ICountDownHandle{
void onTicker(int time); // 倒計時回呼
// todo ------------>第二步
void onFinesh(); // 完成時回呼
}
}
這里的話我們還在android中自己畫了一個倒計時的背景檔案:

將該檔案參考到我們的Splash.xml檔案中
然后的話程式運行的效果:



轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/262067.html
標籤:其他
上一篇:iOS中事件處理流程
下一篇:Mac m1恢復出廠設定
