是否可以将float设置为多个目标?

时间:2019-02-21 15:04:22

标签: java android

我具有此淡入功能

    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次?

1 个答案:

答案 0 :(得分:1)

尝试

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);