sephiroth74 / ImageViewZoom

Android ImageView widget with zoom and pan capabilities
Other
1.9k stars 533 forks source link

Bug with AnimationDrawable in Android 4 #77

Open msphn opened 9 years ago

msphn commented 9 years ago

The bug exists in Android 4.* as far as I know, it's not reproducible in Android 5.*. It broke in API 15 and 17.

If you put a AnimationDrawable as a drawable to the ImageViewTouch, the animation does only start in Android 5.

I guess it's sort of a timing problem, I could work around by wasting with the object stack.

Example animation-list:

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    android:id="@+id/led_animation_1"
    android:oneshot="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/test1" android:duration="150" />
        <item android:drawable="@drawable/test2" android:duration="150" />
</animation-list>

Example code how it works in Android 5 but not in Android 4:

        this.imageView = (ImageViewTouch) view.findViewById(R.id.step_led_image);
        this.imageView.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        this.imageView.setScaleEnabled(true);

        AnimationDrawable frameAnimation = (AnimationDrawable) getResources().getDrawable(R.drawable.led_animation_1);

        this.imageView.setImageDrawable(frameAnimation);
        frameAnimation.start();

How it actually works in Android 4:

        this.imageView = (ImageViewTouch) view.findViewById(R.id.step_led_image);
        this.imageView.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        this.imageView.setScaleEnabled(true);

        AnimationDrawable frameAnimation = (AnimationDrawable) getResources().getDrawable(R.drawable.led_animation_1);

        this.imageView.setImageDrawable(frameAnimation);

        this.imageView.setBackground(frameAnimation);

        AnimationDrawable test = (AnimationDrawable) this.imageView.getBackground();
        test.start();

        this.imageView.setBackground(null);

The first one should be the good way, the second one is a dirty hack and not a solution, don't use that code or you end in hell.

But it's here to show the problem. The first code sticks at frame[0] in Android 4 but works well in 5. The second one sets the same drawable as background and as drawable, that results in some references. If I use getBackground to get my object back, I can run play on it and both starts! I guess we need a function like getImageDrawable, to fix it in a better way for Android 4.

I could implement that, if you like. I leave that here for discussion.

msphn commented 9 years ago

Btw. the first code works fine with just using an ImageView.