android-gif-drawable icon indicating copy to clipboard operation
android-gif-drawable copied to clipboard

GifDrawable.getCurrentPosition() may return the negative?

Open BCsl opened this issue 5 years ago • 6 comments

I call GifDrawable.getCurrentPosition() every 10ms to update the UI progress. This method will return a negative value when the gif is closing to the end.

BCsl avatar May 13 '19 01:05 BCsl

Could you attach a code snippet which reproduces this issue?

koral-- avatar May 13 '19 14:05 koral--

class TestActivity : AppCompatActivity() {

    var mGif: GifDrawable? = null


    @SuppressLint("CheckResult")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        mGif = GifDrawable(resources, R.raw.test_gif)
        Observable.interval(10, TimeUnit.MILLISECONDS)
            .compose(RxLifecycleCompact.bind(this).withObservable())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { tick ->
                mGif?.also {
                    if (it.currentPosition < 0) {
                        if (BuildConfig.LOG_ENABLE) {
                            Log.e("GIF", "current position: ${it.currentPosition}")
                        }
                    }
                    pb_gif?.progress = ((it.currentPosition / it.duration.toFloat()) * 100).toInt()
                }
            }
        iv_gif_test.setImageDrawable(mGif)
    }


    override fun onResume() {
        super.onResume()
        mGif?.start()
    }

    override fun onPause() {
        super.onPause()
        mGif?.pause()
    }
}

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:background="@android:color/white"
             android:layout_height="match_parent">

    <ImageView android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_gravity="center"
               android:id="@+id/iv_gif_test"/>

    <ProgressBar android:layout_width="match_parent"
                 android:layout_height="3dp"
                 style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
                 android:id="@+id/pb_gif"
                 android:indeterminate="false"
                 android:layout_gravity="bottom"
                 android:max="100"
                 tools:progress="50"
    />
</FrameLayout>

@koral--

BCsl avatar May 15 '19 07:05 BCsl

test_gif

BCsl avatar May 15 '19 07:05 BCsl

Can you reproduce this problem? @koral--

BCsl avatar May 16 '19 07:05 BCsl

Yep, I was able to reproduce this bug. I'll fix it ASAP.

koral-- avatar May 21 '19 21:05 koral--

Sorry for the very long delay. The fix will be released in v 1.2.25.

The current position was miscalculated when the animation was on the last frame (in terms of what is displayed) on the subsequent animation loops. The formula should be <whole loop duration> - <remainder duration of the last frame displayed so far> but there was 0 used instead of the loop duration.

koral-- avatar Aug 01 '22 01:08 koral--