Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将图像视图转换为位图?_Android_Android Bitmap - Fatal编程技术网

Android 如何将图像视图转换为位图?

Android 如何将图像视图转换为位图?,android,android-bitmap,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_gravi

我的应用程序中正在运行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"

/>
试试这个

@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
 }

image.getDrawingCache()返回null?我得到image.buildDrawingCache()。如果它为null,则应该崩溃。为什么说它可能返回null?也许我能意识到一些事情。那么你说的代码似乎不起作用是什么意思?您是否得到了“几乎”的祝酒词?您是否设置了
image.setDrawingCacheEnabled(true)image.getDrawingCache()之前的代码>?谢谢你的建议,但没用。我知道问题总是有解决办法的。我会尝试搜索更多。什么??真正地如果你能分享你的代码,我将不胜感激,这样我就可以测试它并调试我的代码。求你了。我可以把gif文件给你,如果你愿意,你可以测试一下。我试过的就是答案。对我来说,
b2
不是空的。我如何知道它是否不是空的?
@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
 }