使用 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);
}
}