思路
自定义一个布局容器(SplashView),在这个布局容器中添加启动页图片,倒计时控件等逻辑操作,也就是把启动界面的一些相同的操作都封装到该容器中,最后将容器添加到启动界面 Activity 中。
具体实现
SimpleView 的完整代码
public class SplashView extends FrameLayout {
ImageView splashImg; // 欢迎界面图片控件
TextView skipBtn; // 欢迎界面倒计时/跳转控件
int skipBtnSizeInDip = 35; // 跳转控件的大小
int skipBtnMarginInDip = 15; // 跳转控件的距离屏幕边缘相对位置
int duration = 5; // 欢迎界面持续时间
int delayTime = 1000; // 计时器每隔 1 秒动作---更新倒计时数字
Activity activity; // 保存当前界面的 Activity ,并对当前 Activity 进行相关操作
GradientDrawable skipBtnBg = new GradientDrawable(); // 倒计时控件圆形背景图
SplashViewOnClickListener listener = null; // 页面动作接口
Handler handler = new Handler(); // 计时器管理者
Runnable runnable = new Runnable() {
@Override
public void run() {
// 每隔一秒更新倒计时控件文字,当倒计时结束,动作,移除欢迎界面内容,调用动作监听器的界面跳转方法
if (0 == duration) {
skipOutSplash();
return;
} else {
setDuration(--duration);
}
handler.postDelayed(runnable, delayTime);
}
};
String actUrl;
public void setActUrl(String actUrl) {
this.actUrl = actUrl;
}
public void setDuration(int duration) {
this.duration = duration;
skipBtn.setText(String.format("跳过\n%d s", duration));
}
public SplashView(@NonNull Activity context) {
super(context);
activity = context;
init();
}
public SplashView(@NonNull Activity context, @Nullable AttributeSet attrs) {
super(context, attrs);
activity = context;
init();
}
public SplashView(@NonNull Activity context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
activity = context;
init();
}
public void setListener(final SplashViewOnClickListener listener) {
if (null == listener)
return;
this.listener = listener;
splashImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
listener.splashImgClick(actUrl);
}
});
}
private void init() {
// 添加图片控件,设置图片铺满屏幕
splashImg = new ImageView(activity);
splashImg.setScaleType(ImageView.ScaleType.FIT_XY);
LayoutParams imgLayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
addView(splashImg, imgLayoutParams);
splashImg.setClickable(true);
// 添加倒计时控件
skipBtn = new TextView(activity);
// 设置控件大小
int skipButtonSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
skipBtnSizeInDip, activity.getResources().getDisplayMetrics());
LayoutParams skipLayoutParams = new LayoutParams(skipButtonSize,
skipButtonSize);
// 设置控件相对位置
skipLayoutParams.gravity = Gravity.TOP|Gravity.RIGHT;
int skipButtonMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
skipBtnMarginInDip, activity.getResources().getDisplayMetrics());
skipLayoutParams.setMargins(0, skipButtonMargin, skipButtonMargin, 0);
skipBtnBg.setShape(GradientDrawable.OVAL); // 设置计时器控件背景图片圆形
skipBtnBg.setColor(Color.parseColor("#66232323"));
skipBtn.setGravity(Gravity.CENTER);
skipBtn.setTextColor(activity.getResources().getColor(android.R.color.white));
skipBtn.setBackgroundDrawable(skipBtnBg);
skipBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
addView(skipBtn, skipLayoutParams);
skipBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
skipOutSplash();
}
});
setDuration(duration);
handler.postDelayed(runnable, delayTime); // 触发器计时器
}
/**
* 显示欢迎界面控件
*
* @params
* */
public static void showSplashView(Activity activity, Integer duration, String imgUrl,String actUrl,
Integer defaultImgResouse, SplashViewOnClickListener listener) {
// 获取欢迎界面的容器对象
ViewGroup contentView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
if (null == contentView || 0 == contentView.getChildCount()) {
throw new IllegalStateException("You should call showSplashView() after setContentView() in Activity instance");
}
// 创建 SplashView 对象,开始将图片和文字添加到 layoutparams 中,计时器开始计时
SplashView splashView = new SplashView(activity);
// 定义 SplashView 布局大小
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// 设置接口回调
splashView.setListener(listener);
splashView.setActUrl(actUrl);
if (null != duration)
splashView.setDuration(duration);
Drawable drawable = null;
if (null != defaultImgResouse) {
drawable = activity.getResources().getDrawable(defaultImgResouse);
}
// 通过 Glide 加载欢迎图片
Glide.with(activity).load(imgUrl).error(drawable).placeholder(drawable).into(splashView.splashImg);
// 设置当前 activity 窗口的标记,充满屏幕
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 将 SplashView 添加到当前 Activity 布局界面
contentView.addView(splashView, params);
}
private void skipOutSplash() {
if (null != listener)
listener.splashSkipClick();
// 移除计时器
handler.removeCallbacks(runnable);
// 将 splashView 移除欢迎界面,设置动画
final ViewGroup parent = (ViewGroup) this.getParent();
if (parent != null) {
ObjectAnimator animator = ObjectAnimator.ofFloat(this,"alpha",1.0f, 0.0f)
.setDuration(600);
animator.start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
}
});
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
parent.removeView(SplashView.this);
}
@Override
public void onAnimationCancel(Animator animator) {
parent.removeView(SplashView.this);
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
}
}
// 欢迎界面的动作监听器
public interface SplashViewOnClickListener {
void splashImgClick(String actionUrl); // 点击欢迎界面图片跳转到 actionUrl
void splashSkipClick(); // 点击跳转过欢迎界面进入主界面
}
}
在启动界面 Activity 中
public class SplashActivity extends AppCompatActivity {
String imgUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505905221463&di=67cfa9f3a08a5359fa86a243fc6aebdb&imgtype=0&src=http%3A%2F%2Ff1.topitme.com%2F1%2Fed%2F14%2F1129731340b7414ed1o.jpg";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SplashView.showSplashView(SplashActivity.this, 6, imgUrl,"点我上哪儿?", R.drawable.splash_df, new SplashView.SplashViewOnClickListener() {
@Override
public void splashImgClick(String actionUrl) {
Toast.makeText(SplashActivity.this, actionUrl, Toast.LENGTH_SHORT).show();
}
@Override
public void splashSkipClick() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
});
}
@Override
public void onBackPressed() {
// super.onBackPressed();
}
}
总结
在功能的实现过程中,我们通过代码动态添加控件到布局容器,Handler 计时器管理者动态修改倒计时文字显示,使用 Glide 加载图片信息,通过创建接口监听界面的动作。重点还是在通过代码动态添加控件的过程。