gpt4 book ai didi

android - 在 Android 上同步动画两个值

转载 作者:行者123 更新时间:2023-11-29 20:20:48 25 4
gpt4 key购买 nike

一般来说,我尝试对 int 值数组进行动画处理,这与您只是对 int 或 float 从一个数字到另一个数字进行动画处理的典型情况不同。

具体来说,我正在尝试为 GradientDrawable 对象上的 int [] 颜色设置动画。

GradientDrawable 有一个名为“setColors(int [])”的属性,我在其中设置了 2 种颜色,即起始颜色和结束颜色,它们构成了整个渐变。

我想从一种颜色组合到另一种颜色组合进行动画处理。如果它是纯色,我已经可以这样做了,如下所示:

Integer colorFrom = getResources().getColor(R.color.red);
Integer colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator animator) {
textView.setBackgroundColor((Integer)animator.getAnimatedValue());
}

});

colorAnimation.start();

所以,我需要这样的东西:

//Define 2 starting colors
Integer colorFrom1 = getResources().getColor(R.color.red);
Integer colorFrom2 = getResources().getColor(R.color.darkred);
int[] startColors = new int[] {colorFrom1, colorFrom2};

//Define 2 ending colors
Integer colorTo1 = getResources().getColor(R.color.blue);
Integer colorTo2 = getResources().getColor(R.color.darkblue);
int[] endColors = new int[] {colorTo1, colorTo2};

ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), startColors, endColors);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator animator) {
gradientView.setColors((Integer)animator.getAnimatedValues()[0] , (Integer)animator.getAnimatedValues()[1]);
}

});

colorAnimation.start();

很明显,代码不会存在,因为没有返回数组的 getAnimatedValues() 方法,而且也没有接受数组作为起始值和结束值的 ValueAnimator.ofObject 方法。

有什么想法吗?

我现在唯一的想法是并行运行两个动画师,每个动画师对渐变的一个维度进行动画处理,并且每个设置只设置 gradientDrawable.setColors() 接受的数组的一半....但是 boyyyy 效率低得几乎无法接受并且可能会出现危险的不同步。

最佳答案

这个怎么样?

public class ArgbArrayEvaluator implements TypeEvaluator<Integer[]> {
ArgbEvaluator evaluator = new ArgbEvaluator();

public Integer[] evaluate(float fraction, Integer[] startValues, Integer[] endValues) {

if(startValues.length != endValues.length) throw new ArrayIndexOutOfBoundsException();
Integer[] values = new Integer[startValues.length];
for(int = 0;i<startValues.length;i++) {
values[i] = (Integer) evaluator.evaluate(fraction,startValues[i],endValues[i]);
}
return values;
}
}

然后做

/* Make sure startColors and endColors are Integer[] not int[] */
final ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbArrayEvaluator(),startColors,endColors);

您的监听器代码:

public void onAnimationUpdate(ValueAnimator animator) {
Integer[] values = (Integer[]) animator.getAnimatedValue();
gradientView.setColors(values[0],values[1]);
}

或者,使用 ObjectAnimator

final ObjectAnimator colorAnimation = ObjectAnimator.ofMultiInt(gradientView,"colors",null, new ArgbArrayEvaluator(), startColors,endColors);

另外,在 start() 方法的 ValueAnimator 文档中它说:

The animation started by calling this method will be run on the thread that called this method. This thread should have a Looper on it (a runtime exception will be thrown if this is not the case). Also, if the animation will animate properties of objects in the view hierarchy, then the calling thread should be the UI thread for that view hierarchy.

所以如果您还没有在 UI 线程中,我会使用以下内容。

runOnUiThread(new Runnable() {
public void run() {
colorAnimation.start();
}
});

我认为它会起作用!

关于android - 在 Android 上同步动画两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com