LoginSignup
0
0

More than 5 years have passed since last update.

Android-ViewGroupごとフェードイン・フェードアウト

Posted at

お久しぶりでございます。

PHPとかC#とかLuaとかAndroid JavaとかSwiftとか色々書いてるとまじでこんがらがる。
ってわけでハマった。
AndroidってViewGroupにalphaしても効かないんねー。

    private static void fadeViewGroup(final ViewGroup viewGroup, final boolean in, int duration) {
        fadeView(viewGroup, in, duration);

        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            final View view = viewGroup.getChildAt(i);
            if (view instanceof ViewGroup)
                fadeViewGroup((ViewGroup) view, in, duration);
            else
                fadeView(view, in, duration);
        }
    }

    private static void fadeView(final View view, final boolean in, int duration) {
        final ValueAnimator valueAnimator = ValueAnimator.ofFloat(
                in ? 0 : 1,
                in ? 1 : 0
        );
        valueAnimator.setDuration(duration);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                final float value = (float) valueAnimator.getAnimatedValue();
                if (view.getBackground() == null)
                    view.setAlpha(value);
                else
                    view.getBackground().mutate().setAlpha((int) (value * 255));
            }
        });
        valueAnimator.start();
    }
0
0
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
0
0