Android setAlpha 和 setImageAlpha 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21831775/
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
Difference between setAlpha and setImageAlpha
提问by kolistivra
ImageViewhas two methods related methods: setAlphaand setImageAlpha. The former is available since API level 1, but is deprecated since level 16. The latter is available since level 16. There's also another setAlphamethod, from the Viewclass and this is introduced in API level 11.
ImageView有两个方法相关的方法:setAlpha和setImageAlpha。前者自 API 级别 1 起可用,但自级别 16 起已弃用。后者自级别 16 起可用。 还有另一个setAlpha方法,来自View类,这是在 API 级别 11 中引入的。
Is the difference between ImageView#setAlpha and ImageView#setImageAlpha only in the naming? Is there any behavioral difference? What's the relationship between View#setAlpha and ImageView#setAlpha?
ImageView#setAlpha 和 ImageView#setImageAlpha 的区别只在命名上吗?是否有任何行为差异?View#setAlpha 和 ImageView#setAlpha 有什么关系?
回答by BladeCoder
ImageView.setAlpha(int)
has been renamed to ImageView.setImageAlpha(int)
to avoid confusion with the new method View.setAlpha(float)
introduced in API level 11.
ImageView.setAlpha(int)
已重命名为ImageView.setImageAlpha(int)
以避免与View.setAlpha(float)
API 级别 11 中引入的新方法混淆。
View.setAlpha(float)
is a general method available on all View
s, including ImageView
. It applies the specified opacity to the whole view. To achieve this, by default the system creates a temporary buffer (a hardware layer) where the View is drawn as usual, then the buffer is drawn on the screen with the specified alpha value. It's a two-pass mechanism which requires the initial allocation of a buffer, so it's somewhat slower. See this video for more information and how to change the default behavior: Hidden Cost of Transparency.
It's important to note that ImageView
includes by default an optimization that will avoid this buffer allocation if it has no background, so in practice there will be no performance penalty when calling ImageView.setAlpha(float)
if the ImageView
has no background.
View.setAlpha(float)
是适用于所有View
s的通用方法,包括ImageView
。它将指定的不透明度应用于整个视图。为了实现这一点,默认情况下,系统会创建一个临时缓冲区(硬件层),像往常一样在其中绘制视图,然后使用指定的 alpha 值在屏幕上绘制缓冲区。这是一个两遍机制,需要初始分配缓冲区,所以它有点慢。有关更多信息以及如何更改默认行为,请参阅此视频:透明度的隐藏成本。这是需要注意的重要ImageView
默认情况下,包括优化,这将避免这个缓冲区分配,如果没有背景,所以在实践中打电话时不会有性能损失ImageView.setAlpha(float)
,如果ImageView
没有背景。
ImageView.setImageAlpha(int)
(and ImageView.setAlpha(int)
) are methods proper to the ImageView
. They control the alpha value which is used to draw the content image (bitmap or other) directly on the screen, with no intermediate pass, so this is the preferred method to use to apply transparency to an image displayed by an ImageView
. Of course if you set a background Drawable on your ImageView
that you also want to be translucent, this method will not produce the expected result.
ImageView.setImageAlpha(int)
(和ImageView.setAlpha(int)
) 是ImageView
. 它们控制用于直接在屏幕上绘制内容图像(位图或其他)的 alpha 值,没有中间传递,因此这是将透明度应用于ImageView
. 当然,如果你设置了一个ImageView
你也想要半透明的背景Drawable ,这个方法不会产生预期的结果。
回答by Dean
View.setAlpha(float)
accepts a float as input and expects a value in the range 0..1 inclusive.ImageView.setAlpha(int)
accepts an int as input and expects a value on the range 0..255 inclusive.ImageView.setAlpha(int)
is deprecated. This is probably because they wanted to remove the conflict with the underlyingView.setAlpha(float)
- As other responders have pointed out
ImageView.setImageAlpha(int)
simply calls through to the deprecatedImageView.setAlpha(int)
. You should expect thatImageView.setAlpha(int)
will be removed in a future API update and should therefore avoid using it.
View.setAlpha(float)
接受一个浮点数作为输入,并期望一个 0..1 范围内的值。ImageView.setAlpha(int)
接受一个 int 作为输入,并期望一个 0..255 范围内的值。ImageView.setAlpha(int)
已弃用。这可能是因为他们想消除与底层的冲突View.setAlpha(float)
- 正如其他响应者所指出的,
ImageView.setImageAlpha(int)
只是调用了已弃用的ImageView.setAlpha(int)
. 您应该期望ImageView.setAlpha(int)
它将在未来的 API 更新中删除,因此应避免使用它。
回答by cania
yes, it is only a naming difference - the current implementation in the Android source in API level 16 is:
是的,这只是命名差异 - API 级别 16 中 Android 源中的当前实现是:
/**
* Sets the alpha value that should be applied to the image.
*
* @param alpha the alpha value that should be applied to the image
*
* @see #getImageAlpha()
*/
@RemotableViewMethod
public void setImageAlpha(int alpha) {
setAlpha(alpha);
}
回答by Crimson
I believe only the naming is a difference. Because setImageAlpha()
is more specific than setAlpha()
. For the View#setAlpha
and ImageView#setAlpha/setImageAlpha
there is not a direct relation.. the View
class has a setAlpha because it could function as a parent of a View#ImageView
so it functions like a container.
我相信只有命名是不同的。因为setImageAlpha()
比setAlpha()
. 对于View#setAlpha
和ImageView#setAlpha/setImageAlpha
没有直接关系..View
类有一个 setAlpha 因为它可以作为 a 的父级,View#ImageView
所以它的功能就像一个容器。
With the setAlpha
you could set the opacity of the whole container therefor it needs that method. with the setImageAlpha
you could set the alpha of only the image and not the whole container.
随着setAlpha
为此,它需要的是方法,你可以设置整个容器的不透明度。使用setImageAlpha
您可以只设置图像而不是整个容器的 alpha。
Besides this i can't really think of a reason they have both an alpha method.
除此之外,我真的想不出他们同时拥有 alpha 方法的原因。