Android Java Play gif 仅一次

问题描述

用户双击帖子时,我想要鹅播放 gif。为此,我像这样使用了 pl.droidsonroids.gif.GifImageView

<pl.droidsonroids.gif.GifImageView
            android:layout_width="match_parent"
            android:layout_height="303dp"

            android:src="@drawable/likepopgif"
            android:visibility="invisible"
            />

为了开始使用,我只是在需要时使用了可见和不可见:

 holder.imgPost.setonTouchListener(new View.OnTouchListener() {
                    @SuppressLint("ClickableViewAccessibility")
                    GestureDetector gestureDetector = new GestureDetector(getApplicationContext(),new GestureDetector.SimpleOnGestureListener(){

                        @SuppressLint("ClickableViewAccessibility")
                        @Override
                        public boolean onDoubleTap(MotionEvent e) {
                            Toast.makeText(visualizzaPost.this,"Liked post",Toast.LENGTH_SHORT).show();
                            holder.gifLike.setVisibility(View.VISIBLE);
                            holder.gifLike.setVisibility(View.INVISIBLE);
                            return super.onDoubleTap(e);
                        }
                    });
                    @Override
                    public boolean onTouch(View v,MotionEvent event) {
                        gestureDetector.onTouchEvent(event);
                        return false;
                    }
                });

然而,显然,不可见与动画长度的秒数是异步的。我正在寻找一个事件/属性或任何其他可以告诉应用程序何时播放动画然后我可以让它不可见的东西。

如果我使用 app:loopCount="1" 在循环以这种方式结束时仍然是不道德的:

enter image description here

解决方法

如果您想播放一次 gif,您可以执行以下操作:

<pl.droidsonroids.gif.GifImageView
            android:layout_width="match_parent"
            android:layout_height="303dp"

            android:src="@drawable/likepopgif"
            android:visibility="invisible"
            app:loopCount="1"
            />

如果您想知道 gif 何时结束,您可以使用 getDuration() 并检查它是否与 getCurrentPosition() 相同