step1: D:\workspace\SplashDemoTwo\app\src\main\java\com\mondor\splashdemotwo\MainActivity.java
package com.mondor.splashdemotwo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_scan_dialog = findViewById(R.id.btn_scan_dialog);
btn_scan_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TestingDialogFragment testingDialogFragment = new TestingDialogFragment();
testingDialogFragment.show(getFragmentManager(), "");
testingDialogFragment.onSetClickDialogListener(new TestingDialogFragment.SetOnClickDialogListener() {
@Override
public void onClickDialogListener() {
Log.e("TAG", "onClickDialogListener: ");
}
});
}
});
}
}
step2: TestingDialogFragment
package com.mondor.splashdemotwo;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import java.util.Timer;
import java.util.TimerTask;
public class TestingDialogFragment extends DialogFragment {
private Timer timer;
private TimerTask timerTask;
private SetOnClickDialogListener mSetOnClickListener;
private int age = 5;
public void onSetClickDialogListener(SetOnClickDialogListener listener) {
this.mSetOnClickListener = listener;
}
public interface SetOnClickDialogListener {
void onClickDialogListener();
}
@Override
public void onStart() {
super.onStart();
if (getDialog() != null && getDialog().getWindow() != null) {
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(false);
getDialog().setCancelable(false);
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
}
View mView = inflater.inflate(R.layout.activity_splash, container, false);
TextView iv_ad = mView.findViewById(R.id.iv_ad);
iv_ad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (age == 0) {
getDialog().cancel();
if (mSetOnClickListener != null) {
mSetOnClickListener.onClickDialogListener();
}
}
}
});
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
age--;
iv_ad.setText(String.valueOf(age));
Log.e("TAG", "time_data:" + age);
if (age == 0) {
iv_ad.setText(String.valueOf("跳过"));
releaseTimer();
}
}
};
timer.schedule(timerTask, 0, 1000);
return mView;
}
private void releaseTimer() {
if (timer != null) {
timer.cancel();
}
if (timerTask != null) {
timerTask.cancel();
}
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
}
step3: shape 圆形按钮
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#E8EBF2"/>
<corners
android:radius="45dp"/>
</shape>
step4: 首页布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/qrcode_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_scan_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="闪屏" />
</RelativeLayout>
step5: 闪屏页布局
<?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"
android:fitsSystemWindows="false"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/ad" />
<TextView
android:id="@+id/iv_ad"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/bg_go"
android:gravity="center"
android:textSize="13sp" />
</RelativeLayout>
end
|