问题描述
setImageBitmap
和 setImageDrawable
有什么区别?
我有一个图像,我想从文件中动态设置它.我遵循的教程说将我的 Bitmap
转换为 BitmapDrawable
然后使用 setImageDrawable
设置它.我注意到直接使用 setImageBitmap
设置 Bitmap
也有效,但我没有注意到任何区别.
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);
或
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
Below code is picked from ImageView.java of AOSP
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!