本文介绍了Android - ImageView:setImageBitmap VS setImageDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
setImageBitmap
和 setImageDrawable
之间有什么区别?
我有一个我想从文件中动态设置的图像。我遵循的教程说我将位图
转换为 BitmapDrawable
然后使用 setImageDrawable设置它
。我注意到直接用 setImageBitmap
设置位图
也有效,但我没有发现任何差异。
I have an image which I would like to set dynamically from file. The tutorial that I followed says to convert my Bitmap
to a BitmapDrawable
then set it using setImageDrawable
. I've notice that setting the Bitmap
directly with setImageBitmap
also works but I don't notice any difference.
Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
BitmapDrawable bitmapDrawable = new BitmapDrawable(image);
imageView.setImageDrawable(bitmapDrawable);
OR
Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(image);
推荐答案
内部两个没有区别 setImageBitmap
正在调用 setImageDrawable
。
There is no difference between the two internally setImageBitmap
is calling setImageDrawable
.
下面的代码是从 AOSP的ImageView.java
public void setImageBitmap(Bitmap bm) {
// if this is used frequently, may handle bitmaps explicitly
// to reduce the intermediate drawable object
setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}
这篇关于Android - ImageView:setImageBitmap VS setImageDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!