LoginSignup
29
31

More than 5 years have passed since last update.

[Android] 文字をなめらかに点滅させる

Last updated at Posted at 2014-07-14

完成イメージ

measure.gif

VISIBLE/INVISIBLE でなく、フェードさせてなめらかに点滅させます。
(GIFアニメのループの都合で点滅タイミングが若干乱れていますが実際は等間隔で点滅します)

実装

AnimatorSample.java(抜粋)
private Handler mHandler = new Handler();
private ScheduledExecutorService mScheduledExecutor;
private TextView mLblMeasuring;

private void startMeasure() {


    /**
     * 点滅させたいView
     * TextViewじゃなくてもよい。
     */
    mLblMeasuring = (TextView) findViewById(R.id.lbl_measuring);

    /**
     * 第一引数: 繰り返し実行したい処理
     * 第二引数: 指定時間後に第一引数の処理を開始
     * 第三引数: 第一引数の処理完了後、指定時間後に再実行
     * 第四引数: 第二、第三引数の単位
     *
     * new Runnable(無名オブジェクト)をすぐに(0秒後に)実行し、完了後1700ミリ秒ごとに繰り返す。
     * (ただしアニメーションの完了からではない。Handler#postが即時実行だから??)
     */
    mScheduledExecutor = Executors.newScheduledThreadPool(2);

    mScheduledExecutor.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mLblMeasuring.setVisibility(View.VISIBLE);

                    // HONEYCOMBより前のAndroid SDKがProperty Animation非対応のため
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        animateAlpha();
                    }
                }
            });
        }


        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        private void animateAlpha() {

            // 実行するAnimatorのリスト
            List<Animator> animatorList = new ArrayList<Animator>();

            // alpha値を0から1へ1000ミリ秒かけて変化させる。
            ObjectAnimator animeFadeIn = ObjectAnimator.ofFloat(mLblMeasuring, "alpha", 0f, 1f);
            animeFadeIn.setDuration(1000);

            // alpha値を1から0へ600ミリ秒かけて変化させる。
            ObjectAnimator animeFadeOut = ObjectAnimator.ofFloat(mLblMeasuring, "alpha", 1f, 0f);
            animeFadeOut.setDuration(600);

            // 実行対象Animatorリストに追加。
            animatorList.add(animeFadeIn);
            animatorList.add(animeFadeOut);

            final AnimatorSet animatorSet = new AnimatorSet();

            // リストの順番に実行
            animatorSet.playSequentially(animatorList);

            animatorSet.start();
        }
    }, 0, 1700, TimeUnit.MILLISECONDS);

}

29
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
31