使用 AlphaAnimation實現字符閃爍的TextView

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章