setAnimatedVectorDrawable.start()在某些设备上不起作用

时间:2020-06-03 06:24:41

标签: java android android-animation android-vectordrawable animationdrawable

我有问题:
1.我有一个Activty类,其中将矢量动画设置为背景:

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

new CenterCropDrawable(animationBackground)-用于设置scaleType = CenterCrop(用于Drawable);

  1. 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();
    }
}

问题的实质:此代码允许您为scaleType设置Drawable并开始矢量动画。这适用于许多旧/新设备,但已检测到问题。一些用户不播放动画(该设备是新的29 API)。如果您更改代码:
来自
getWindow().setBackgroundDrawable(new CenterCropDrawable(animationBackground));

getWindow().setBackgroundDrawable(animationBackground);
这些设备上的动画开始播放。可能是什么问题?

defaultConfig{   
minSDKVersion 21
}  

我将不胜感激!

0 个答案:

没有答案