Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

android - Difference between setAlpha and setImageAlpha

ImageView has two methods related methods: setAlpha and 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 setAlpha method, from the View class and this is introduced in API level 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

View.setAlpha(float) is a general method available on all Views, 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...