如何将GifImageView转换为位图?

时间:2017-05-18 07:40:47

标签: android android-bitmap

我的应用程序中运行了Gif。当我按下停止按钮时,Gif停止。我想要做的是将停止的Gif转换为位图,以便我可以将它与另一个也转换为位图的图像进行比较。 我的代码似乎不起作用。

我正在使用这个库

<pl.droidsonroids.gif.GifImageView
android:id="@+id/img_1"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/gif_1"

/>

这是我的onClickListener

    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(counter%2==0){


                image.setFreezesAnimation(true);
                ((GifDrawable)image.getBackground()).stop();

                image.buildDrawingCache();



                Bitmap b2 = image.getDrawingCache();

                Drawable d1 = getDrawable(R.drawable.lvl_1);
                Bitmap b1 = ((BitmapDrawable)d1).getBitmap();



                if(b1.sameAs(b2))
                {
                    Toast.makeText(NextActivity.this,"GOOD JOB!",Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(NextActivity.this,"ALMOST!",Toast.LENGTH_SHORT).show();


                counter++;


            }
            else{
                image.setFreezesAnimation(false);

                ((GifDrawable)image.getBackground()).start();
                counter++;


            }
        }
    });

1 个答案:

答案 0 :(得分:0)

试试这个,

@Override
public void onClick(View view) {
 image.setFreezesAnimation(true);
 ((GifDrawable) image.getDrawable()).stop();
 image.buildDrawingCache();
 Bitmap b2 = image.getDrawingCache();

 Drawable d1 = getDrawable(R.drawable.lvl_1);
 Bitmap b1 = ((BitmapDrawable)d1).getBitmap();
    //...rest of the code
 }