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