AndroidのColor色の過剰計算

2774 ワード

MIUIでいろいろな色のグラデーションアニメーションを見て、とても眩しくて、ObjectAnimatorで試してみて初歩的な効果を実現することができて、次はソースコードです
ObjectAnimatorによるカラーグラデーションアニメーション
        ObjectAnimator  animator = ObjectAnimator.ofObject(testLayout, "backgroundColor",new ArgbEvaluator(),0xff40c7b6,0xffff7a59);
        animator.setDuration(1000);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.start();


簡単そうに見えますが、プログレスバーが変化すると背景の色が一緒に変化することに気づきました.この方法では、ある色からある色に変化しても途中で止まることはありません.インターネットで検索してみましたが、多くの大物がカスタムTypeEvaluatorを使って属性アニメーションの属性値を計算していますが、滞在中の色を実現するには滞在できません.そこでDALAOのコードを一部抜きました
public void startValueAnimator(int progress,int mProgress){
        if(animator != null && animator.isRunning()){
            animator.cancel();
            animator = null;
        }
        int duration = 15 * Math.abs(progress - mProgress);
        animator = ObjectAnimator.ofObject(testLayout, "backgroundColor",new ArgbEvaluator(),getCurrentColor(mProgress/100f,0xff40c7b6,0xffff7a59),getCurrentColor(progress/100f,0xff40c7b6,0xffff7a59));
        animator.setDuration(duration);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.d("MainActivity", "animation.getAnimatedValue():" + animation.getAnimatedValue());
            }
        });
        animator.start();
    }
/**
     *   fraction         。 fraction     0f-1f
     */
    private int getCurrentColor(float fraction, int startColor, int endColor) {
        int redCurrent;
        int blueCurrent;
        int greenCurrent;
        int alphaCurrent;

        int redStart = Color.red(startColor);
        int blueStart = Color.blue(startColor);
        int greenStart = Color.green(startColor);
        int alphaStart = Color.alpha(startColor);

        int redEnd = Color.red(endColor);
        int blueEnd = Color.blue(endColor);
        int greenEnd = Color.green(endColor);
        int alphaEnd = Color.alpha(endColor);

        int redDifference = redEnd - redStart;
        int blueDifference = blueEnd - blueStart;
        int greenDifference = greenEnd - greenStart;
        int alphaDifference = alphaEnd - alphaStart;

        redCurrent = (int) (redStart + fraction * redDifference);
        blueCurrent = (int) (blueStart + fraction * blueDifference);
        greenCurrent = (int) (greenStart + fraction * greenDifference);
        alphaCurrent = (int) (alphaStart + fraction * alphaDifference);

        return Color.argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent);
    }

OK完了後の使用保留