0

I have a problem:
1. I have an Activty class in which vector animation is set as a background:

    private void showSplashScreenAnimation() {
    AnimatedVectorDrawable animationBackground = (AnimatedVectorDrawable) getDrawable(R.drawable.splash_screen_anim);
    getWindow().setBackgroundDrawable(new CenterCropDrawable(animationBackground));
    animationBackground.start();
}

new CenterCropDrawable(animationBackground) - used to set scaleType = CenterCrop (forDrawable);

  1. Class CenterCropDrawable:
public class CenterCropDrawable extends Drawable {

    @NonNull
    private final Drawable target;

    public CenterCropDrawable(@NonNull Drawable target) {
        this.target = target;
    }

    @Override
    public void setBounds(@NonNull Rect bounds) {
        super.setBounds(bounds.left, bounds.top, bounds.right, bounds.bottom);
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        final RectF sourceRect = new RectF(0, 0, target.getIntrinsicWidth(), target.getIntrinsicHeight());
        final RectF screenRect = new RectF(left, top, right, bottom);

        final Matrix matrix = new Matrix();
        matrix.setRectToRect(screenRect, sourceRect, Matrix.ScaleToFit.CENTER);

        final Matrix inverse = new Matrix();
        matrix.invert(inverse);
        inverse.mapRect(sourceRect);

        target.setBounds(Math.round(sourceRect.left), Math.round(sourceRect.top),
                Math.round(sourceRect.right), Math.round(sourceRect.bottom));

        super.setBounds(left, top, right, bottom);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.save();
        canvas.clipRect(getBounds());
        target.draw(canvas);
        canvas.restore();
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
        target.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        target.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return target.getOpacity();
    }
}

The essence of the problem: this code allows you to set the scaleType forDrawable and start vector animation. This works on a lot of old / new devices, but has detected a problem. Some users do not play animation (the device was new 29 API). If you change the code:
from
getWindow().setBackgroundDrawable(new CenterCropDrawable(animationBackground));
to
getWindow().setBackgroundDrawable(animationBackground);
the animation on these devices starts to play. What could be the problem?

defaultConfig{   
minSDKVersion 21
}  

I would be grateful for any help!

PcheLL
  • 1
  • 1
  • AnimatedVectorDrawable Added in API level 21 – IntelliJ Amiya Jun 03 '20 at 06:29
  • @IntelliJAmiya I know. `defaultConfig { minSdkVersion 21 }` – PcheLL Jun 03 '20 at 10:39
  • @PcheLL I guess this is because the parent of `CenterCropDrawable` is simply `Drawable` and not `AnimatedVectorDrawable`. So, you set apparently a `Drawable` object as background of window which is animation-stripped off version of original `AnimatedVectorDrawable`. – cgb_pandey Jun 03 '20 at 12:46
  • @cgb_pandey Maybe) I will check it. But why then does this bug appear only on some devices? Thanks for the help! – PcheLL Jun 03 '20 at 13:54
  • @PcheLL I am curious too. It was just my opinion on what could have happened. You can try logging some stuff in your `CenterCropDrawable` class to find out why. – cgb_pandey Jun 03 '20 at 14:09
  • @cgb_pandey Yes, I’ll try today and write to you) Thank you – PcheLL Jun 03 '20 at 15:06
  • @cgb_pandey Does not work. On some devices, animation does not start ( – PcheLL Jun 03 '20 at 15:36
  • What exactly did you changed? Did you also changed the parameter type of the constructor ? You can do something like : ` @NonNull private final AnimatedVectorDrawable target; public CenterCropDrawable(@NonNull AnimatedVectorDrawable target) { this.target = target; } ` Also, you need to use `compat` version if there is one. It will fix issues in some devices. – cgb_pandey Jun 04 '20 at 00:48
  • @cgb_pandey Changing to `AnimatedVectorDrawableCompat` did not solve the problem. I think the animation freezes. Since intermediate states are sometimes drawn. – PcheLL Jun 04 '20 at 08:10
  • so the class looked before compat: `public class CenterCropDrawable extends AnimatedVectorDrawable { @NonNull private final AnimatedVectorDrawable target; public CenterCropDrawable(@NonNull AnimatedVectorDrawable target) { this.target = target; }` – PcheLL Jun 04 '20 at 08:10
  • `@Override public void showBackgroundAnimation(int resId) { animationBackground = (AnimatedVectorDrawable) getDrawable(resId); getWindow().setBackgroundDrawable(new CenterCropDrawable(animationBackground)); animationBackground.start(); }` – PcheLL Jun 04 '20 at 08:13

0 Answers0