Android - ImageView:setImageBitmap VS setImageDrawable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12001793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - ImageView: setImageBitmap VS setImageDrawable
提问by meeeee
What is the difference between setImageBitmap
and setImageDrawable
?
setImageBitmap
和 和有setImageDrawable
什么区别?
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.
我有一个图像,我想从文件中动态设置它。我遵循的教程说将 my 转换Bitmap
为 aBitmapDrawable
然后使用setImageDrawable
. 我注意到Bitmap
直接设置withsetImageBitmap
也有效,但我没有注意到任何区别。
Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
BitmapDrawable bitmapDrawable = new BitmapDrawable(image);
imageView.setImageDrawable(bitmapDrawable);
OR
或者
Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(image);
回答by nandeesh
There is no difference between the two internally setImageBitmap
is calling setImageDrawable
.
两者在内部没有区别setImageBitmap
就是调用setImageDrawable
。
Below code is picked from ImageView.javaof AOSP
下面的代码摘自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));
}
回答by Fenix Voltres
Actually, you may see the difference, if you sometimes use null
as an argument:
实际上,如果您有时将其null
用作参数,您可能会看到差异:
imageView.setImageDrawable(null);
Drawable d = imageView.getDrawable(); // d == null
imageView.setImageBitmap(null);
Drawable d = imageView.getDrawable(); // d == some BitmapDrawable, != null
so if you are checking for existence of a drawable in ImageView
, beware of it.
因此,如果您正在检查 drawable in 的存在ImageView
,请注意它。
回答by pierrotlefou
I just wrote up an article for this. Wish to be able to answer your question.
我刚刚为此写了一篇文章。希望能够回答你的问题。
https://plus.google.com/112740367348600290235/posts/VNAfFLDcKrw
https://plus.google.com/112740367348600290235/posts/VNAfFLDcKrw
ImageView
has 4 APIs to specify the image. Which one to use? What is the difference?
ImageView
有 4 个 API 来指定图像。使用哪一种?有什么不同?
- setImageDrawable(Drawable drawable)
- setImageBitmap(Bitmap bm)
- setImageResource(int resId)
- setImageURI(URI uri)
- setImageDrawable(Drawable drawable)
- setImageBitmap(位图bm)
- setImageResource(int resId)
- setImageURI(URI uri)
ImageView
, by the name, is used to display an image. But what is a image? A Bitmap
is-a image, not hard to understand and we use setImageBitmap
for that purpose. However, internally, the ImageView
has-a Drawable
but not a Bitmap
and that is what setImageDrawable
for. When you call setImageBitmap
, internally, first the bitmap will be wrapped to BitmapDrawable
, which IS-A Drawable
, and then call setImageDrawable
.
ImageView
顾名思义,用于显示图像。但什么是图像?一个Bitmap
是-一个图像,不难理解,我们setImageBitmap
为此目的使用。然而,在内部,ImageView
has-aDrawable
但不是 a Bitmap
,这就是为什么setImageDrawable
。当您在setImageBitmap
内部调用时,首先将位图包装到BitmapDrawable
,即 IS-A Drawable
,然后调用setImageDrawable
.
Here is the code.
这是代码。
public void setImageBitmap(Bitmap bm) {
setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}
So, what about the 3 and 4 API?
那么,3 和 4 API 呢?
You should already know that that are bunches of ways to create a bitmap: from a file path, from an input stream, from the Uri, or from the resource file.
您应该已经知道有多种创建位图的方法:从文件路径、从输入流、从 Uri 或从资源文件。
BitmapFactory.decodeFile(String pathName)
BitmapFactory.decodeStream(Inputstream)
BitmapFactory.decodeResource(Resource res, int id)
BitmapFactory.decodeByteArray(byte[] data)
Aware of this, it is easy to understand setImageResource
/setImageUri
is just same as setImageBitmap
.
意识到这一点,很容易理解setImageResource
/setImageUri
与setImageBitmap
.
To sum up, setImageDrawable
is the primitive function other APIs rely on. The other 3 are just helper methods making you write less code.
总结一下,setImageDrawable
就是其他API依赖的原始函数。其他 3 个只是帮助方法,让您编写更少的代码。
In addition, it is very important to keep in mind that ImageView
actually has-a Drawable
, which not necessarily to be a BitmapDrawable
! You could set any Drawable
to the Image view.
此外,记住ImageView
实际上 has-a是非常重要的Drawable
,它不一定是 a BitmapDrawable
!您可以将 any 设置Drawable
为 Image 视图。
Besides setting the Drawable
through Java API, you could also using XML attribution to set the source Drawable
for ImageView
. See example below. Note that the shape could be either an image file (.png, .jpg, .bmp) or xml file.
除了设置了Drawable
通过Java API,你也可以使用XML属性设置源Drawable
的ImageView
。请参阅下面的示例。请注意,形状可以是图像文件(.png、.jpg、.bmp)或 xml 文件。
回答by Max Raskin
Both methods are valid and achieve the same result. In the first method you wrap your bitmap around a drawable object which is an abstraction for anything that can be drawn in a View.
这两种方法都是有效的,并获得相同的结果。在第一种方法中,您将位图包裹在一个可绘制对象周围,该对象是可以在视图中绘制的任何内容的抽象。
The special thing about Drawables is that they let you do all kinds of operations on the graphical object they wrap around (scaling, translation, opacity etc..).
Drawables 的特别之处在于,它们允许您对它们环绕的图形对象进行各种操作(缩放、平移、不透明度等)。
A bitmap is one kind of drawable, you can learn further about drawables here: http://developer.android.com/guide/topics/resources/drawable-resource.html
位图是一种可绘制对象,您可以在此处进一步了解可绘制对象:http: //developer.android.com/guide/topics/resources/drawable-resource.html
In the second method, you directly access the bitmap bits without any drawable related operations and simply draw the bitmap as-is on your view.
在第二种方法中,您无需任何可绘制相关操作直接访问位图位,只需在视图上按原样绘制位图。
Hope this helps, cheers, Max.
希望这会有所帮助,欢呼,Max。
回答by lemon
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));
}
maybe the difference is the mContext.getResources(), from mContext.getResources() can get the density of the divice, so the display on sceen is different
可能是mContext.getResources()不同,从mContext.getResources()可以得到div的密度,所以sceen上的显示不一样