適用于啟動(dòng)頁(yè)的幾秒倒計(jì)時(shí)進(jìn)入主頁(yè)使用:
package com.example.zd.baselibrary.view;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
/**
* Package: com.example.zd.baselibrary.view
* <p>
* describe: 倒計(jì)時(shí)的自定義TextView
*
* @author zhangdong on 2019/10/24 0024.
* @version 1.0
* @see .
* @since 1.0
*/
public class CountdownView extends AppCompatTextView {
private static int MAX_TIME = 5; //最大倒計(jì)時(shí)時(shí)間
private String CONCAT_STR = "s跳過(guò)"; //默認(rèn)倒計(jì)時(shí)后面顯示的文字
private int updateTime = 0; //當(dāng)前倒計(jì)時(shí)時(shí)間
private int bgColor = Color.GRAY; //背景顏色
private int bgCorner = 10; //有設(shè)置畫(huà)的背景時(shí)默認(rèn)圓角10dp
private BgStyle bgStyle = BgStyle.CLEAR; //背景樣式
private Paint mPaint; //繪制背景的畫(huà)筆
private int vW, vH; //此view的寬\高
private ValueAnimator valueAnimator; //動(dòng)畫(huà)晤硕,通過(guò)動(dòng)畫(huà)的進(jìn)度來(lái)控制view的文字繪制
private CountdownEndListener endListener; //倒計(jì)時(shí)結(jié)束
//背景樣式
public enum BgStyle {
LINE, //一圈線
FILL, //填充背景
CLEAR //沒(méi)有背景 可以自己設(shè)置background
}
/**
* 設(shè)置倒計(jì)時(shí)結(jié)束監(jiān)聽(tīng)
*
* @param endListener .
* @return thisView
*/
public CountdownView setEndListener(CountdownEndListener endListener) {
this.endListener = endListener;
return this;
}
/**
* 設(shè)置倒計(jì)時(shí)時(shí)間 (單位s)
*
* @param maxTime 最大時(shí)間 default 5 ms
* @return thisView
*/
public CountdownView setMaxTime(int maxTime) {
MAX_TIME = maxTime;
return this;
}
/**
* 設(shè)置倒計(jì)時(shí)后面拼接的字符串
*
* @param concatStr eg:"s跳過(guò)"
* @return thisView
*/
public CountdownView setConcatStr(String concatStr) {
this.CONCAT_STR = concatStr;
return this;
}
/**
* 設(shè)置背景樣式
*
* @param bgStyle 背景樣式 默認(rèn)無(wú)
* @return thisView
*/
public CountdownView setBgStyle(BgStyle bgStyle) {
this.bgStyle = bgStyle;
invalidate();
return this;
}
/**
* 設(shè)置背景圓角大小 (單位dp)
*
* @param bgCorner 圓角大小
* @return thisView
*/
public CountdownView setBgCorner(int bgCorner) {
this.bgCorner = bgCorner;
invalidate();
return this;
}
/**
* 存在背景時(shí)梯皿,背景的顏色
*
* @param bgColor 顏色值
* @return thisView
*/
public CountdownView setBgColor(int bgColor) {
this.bgColor = bgColor;
invalidate();
return this;
}
public CountdownView(Context context) {
this(context, null);
}
public CountdownView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CountdownView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
vW = w;
vH = h;
}
@Override
protected void onDraw(Canvas canvas) {
//繪制背景
drawBgShape(canvas);
//textView顯示文字
super.onDraw(canvas);
}
@SuppressLint("NewApi")
private void drawBgShape(Canvas canvas) {
switch (bgStyle) {
case LINE:
//畫(huà)線模式
mPaint.setStyle(Paint.Style.STROKE);
break;
case FILL:
//填充模式
mPaint.setStyle(Paint.Style.FILL);
break;
case CLEAR:
//不畫(huà)背景
return;
default:
return;
}
//清除原背景
setBackgroundResource(0);
//畫(huà)背景
mPaint.setColor(bgColor);
int minW = Math.min(vW, vH);
//圓角大小
if (bgCorner > (minW >> 1))
bgCorner = (minW >> 1);
//圓角小于0的情況不存在 強(qiáng)制為0
if (bgCorner < 0)
bgCorner = 0;
canvas.save();
canvas.drawRoundRect(0, 0, vW, vH,
dp2Px(getContext(), bgCorner), dp2Px(getContext(), bgCorner), mPaint);
canvas.restore();
}
/**
* 獲取當(dāng)前應(yīng)該顯示的文字內(nèi)容
*
* @return .
*/
private String getShouldShowText() {
if (updateTime != 0)
return String.valueOf(updateTime).concat(CONCAT_STR);
else
return CONCAT_STR.substring(1);
}
/**
* 倒計(jì)時(shí)開(kāi)始
*/
public synchronized void timeStart() {
startAnimation();
}
/**
* 倒計(jì)時(shí)結(jié)束叨粘;或者手動(dòng)調(diào)用此方法結(jié)束倒計(jì)時(shí)動(dòng)畫(huà)
*/
public synchronized void timeEnd() {
if (valueAnimator != null) {
//停止動(dòng)畫(huà),清空動(dòng)畫(huà)資源
if (valueAnimator.isRunning()) {
valueAnimator.cancel();
}
valueAnimator.removeAllUpdateListeners();
valueAnimator = null;
}
}
private void startAnimation() {
if (null == valueAnimator) {
valueAnimator = ValueAnimator.ofInt(MAX_TIME, 0);
//動(dòng)畫(huà)進(jìn)度監(jiān)聽(tīng)
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//當(dāng)前進(jìn)度值
int value = (int) animation.getAnimatedValue();
if (updateTime != value) {
//重置倒計(jì)時(shí)時(shí)間,
updateTime = value;
//重新繪制view顯示文字
setText(getShouldShowText());
//倒計(jì)時(shí)結(jié)束的回調(diào)
if (updateTime == 0 && null != endListener)
endListener.countdownEnd();
}
}
});
//設(shè)置勻速差值器
valueAnimator.setInterpolator(new LinearInterpolator());
}
if (valueAnimator.isRunning())
return;
//設(shè)置動(dòng)畫(huà)執(zhí)行時(shí)間
valueAnimator.setDuration(MAX_TIME * 1000);
valueAnimator.start();
}
@Override
protected void onDetachedFromWindow() {
//銷(xiāo)毀時(shí),清空動(dòng)畫(huà)
timeEnd();
super.onDetachedFromWindow();
}
private static int dp2Px(Context context, float dpValue) {
float density = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * density);
}
/**
* 倒計(jì)時(shí)結(jié)束監(jiān)聽(tīng)
*/
public interface CountdownEndListener {
//倒計(jì)時(shí)結(jié)束
void countdownEnd();
}
}
使用:
正常textView的相關(guān)設(shè)置汗茄,主要設(shè)置字體大小、顏色等
然后代碼設(shè)置相關(guān)屬性铭若,開(kāi)始倒計(jì)時(shí):
final CountdownView countdownView = (CountdownView) findViewById(R.id.view);
//點(diǎn)擊view時(shí)停止倒計(jì)時(shí)
countdownView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//調(diào)用view停止倒計(jì)時(shí)方法
countdownView.timeEnd();
//去吧皮卡丘
Toast.makeText(MainActivity.this, "點(diǎn)擊結(jié)束倒計(jì)時(shí)", Toast.LENGTH_SHORT).show();
}
});
//倒計(jì)時(shí)view相關(guān)設(shè)置
countdownView.setMaxTime(5) //倒計(jì)時(shí)時(shí)間洪碳,單位秒
.setConcatStr("s跳過(guò)廣告頁(yè)") //倒計(jì)時(shí)后面拼接的字符串
.setBgStyle(CountdownView.BgStyle.FILL) //背景樣式 LINE:線 FILL:填充 CLEAR:不需要通過(guò)代碼畫(huà)背景
.setBgColor(Color.RED) //要畫(huà)背景的顏色值
.setBgCorner(40) //要畫(huà)背景的圓角值 單位dp
.setEndListener(new CountdownView.CountdownEndListener() { //倒計(jì)時(shí)結(jié)束的回調(diào)監(jiān)聽(tīng)
@Override
public void countdownEnd() {
//倒計(jì)時(shí)結(jié)束递览,去吧皮卡丘
Toast.makeText(MainActivity.this, "倒計(jì)時(shí)結(jié)束", Toast.LENGTH_SHORT).show();
}
})
.timeStart(); //開(kāi)始倒計(jì)時(shí)
效果圖:
image.png