Android 如何将图像视图转换为位图?
我的应用程序中正在运行Gif。当我按下停止按钮时,Gif停止。我想做的是将停止的Gif转换为位图,以便将其与另一个也转换为位图的图像进行比较。 我的代码似乎不起作用 我正在使用这个图书馆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
<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
}