使用 AlphaAnimation實現字符閃爍的TextView
- 目的:字符閃爍提示用戶點擊
- 原理:使用 AlphaAnimation 進行透明度改變動畫達到效果
package com.fadi.su.runner;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;
import androidx.annotation.Nullable;
@SuppressLint("AppCompatCustomView")
public class FlickerTextView extends TextView {
private static final int DELAY_TIME = 1_000;
public FlickerTextView(Context context) {
this(context, null, 0);
}
public FlickerTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public FlickerTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
startAlphaAnimation();
}
private void startAlphaAnimation() {
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(DELAY_TIME);
alphaAnimation.setRepeatCount(Animation.INFINITE);
alphaAnimation.setRepeatMode(Animation.REVERSE);
startAnimation(alphaAnimation);
}
}