我有这个淡入功能

    private void fadeIn() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(ButtonA, "alpha", 0f, 1f);

        objectAnimator.setDuration(2000L);
        objectAnimator.addListener(new AnimatorListenerAdapter() {
        });
        objectAnimator.start();
}


现在唯一的目标是ButtonA,我还有3个按钮(ButtonB,ButtonC ....),无论如何我都可以以全部四个为目标,而无需将此代码段编写4次以上?

最佳答案

尝试这个

private static void fadeIn(long duration, final View... views) {
    if (views == null) return;
    final ValueAnimator va = ValueAnimator.ofFloat(0, 1);
    va.setDuration(duration);
    va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animator) {
            final float alpha = (float) animator.getAnimatedValue();
            for (View view : views) view.setAlpha(alpha);
        }
    });
    va.start();
}


如何使用:

// first argument duration and then pass any number of views
fadeIn(2000, buttonA, buttonB, buttonC);

关于java - 是否可以将float设置为多个目标?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54810161/

10-10 09:12