Android - AnimationDrawable 在播放自定义位图可绘制对象时闪烁白洞

Android - AnimationDrawable flashing white hole while playing a custom bitmap drawable

我编写了自定义 ImageView 来显示带有位图列表的动画。这是我的源代码:

public class CustomImageView extends ImageView {
    public CustomImageView(Context context) {
        super(context);
    }
    public void startAnimation(List<BitmapDrawable> arrBitmapDelay, int[] durations) {
        if (arrBitmapDelay.size() > 1 && arrBitmapDelay.size() == durations.length) {
            final AnimationDrawable oAnimation = new AnimationDrawable();
            oAnimation.setOneShot(false);

            int i = 0;
            for (BitmapDrawable oBitmapDelay : arrBitmapDelay) {
                oAnimation.addFrame(oBitmapDelay, durations[i]);
                i++;
            }

            if(getContext() instanceof Activity)
                if(((Activity)getContext()).isFinishing())
                    return;
            if(oAnimation.getNumberOfFrames()<=0) return;

            setImageDrawable(oAnimation);
            post(new Runnable() {
                @Override
                public void run() {
                    oAnimation.start();
                }
            });

        }
    }
}

结果:https://goo.gl/photos/FSC5RaEE2ajfe23v6

你可以看到,它正确循环时间,但有时会闪烁...请帮助!

编辑

我添加阅读列表位图代码

public void decodeAndShow() {
    List<Bitmap> bitmaps = new ArrayList<>();
    int[] duration = new int[20];
    for (int i=0; i<20; i++) {
        bitmaps.add(BitmapFactory.decodeFile(new File(getContext().getCacheDir(), "bitmapsample"+i+".png").getAbsolutePath()));
        duration[i] = 100;
    }
    img.startAnimation(bitmaps, duration);
}

抱歉,因为我的项目太复杂,无法复制到这里。

为 或 将 android:hardwareAccelerated="true" 添加到您的清单中。这可确保您的应用程序使用设备图形卡并且应该有助于您的动画。

我自己用外挂修复的。不知道为什么没问题

这是我的代码:

public class CustomImageView extends ImageView {
    private Runnable runningAnimation;
    public CustomImageView(Context context) {
        super(context);
    }
    public void startAnimation(List<BitmapDrawable> arrBitmapDelay, int[] durations) {
        if(runningAnimation != null) {
            this.removeCallbacks(runningAnimation);
        }
        if (arrBitmapDelay.size() > 1 && arrBitmapDelay.size() == durations.length) {
            final AnimationDrawable oAnimation = new AnimationDrawable();
            oAnimation.setOneShot(false);

            int i = 0;
            for (BitmapDrawable oBitmapDelay : arrBitmapDelay) {
                oAnimation.addFrame(oBitmapDelay, durations[i]);
                i++;
            }

            if(getContext() instanceof Activity)
                if(((Activity)getContext()).isFinishing())
                    return;
            if(oAnimation.getNumberOfFrames()<=0) return;

            setImageDrawable(oAnimation);
            runningAnimation = new Runnable() {
                @Override
                public void run() {
                    oAnimation.start();
                }
            }
            post(runningAnimation);

        }
    }
}