35

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?

4 Answers 4

59

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.

3
  • 1
    This is the most comprehensive and accurate answer by far. Don't understand why its rated lowest. This should be the accepted answer. Aug 16, 2015 at 12:59
  • 1
    it's worth noting that currently setImageAlpha() does not work well when the resource is an adaptive icon. In that case, use View.setAlpha() instead. issuetracker.google.com/issues/72694861
    – Mark
    Jan 31, 2018 at 13:54
  • Good point setBackground not produce bad result. This was what i was searching. thanks buddy
    – Ashu Kumar
    Apr 15, 2019 at 4:19
27
  • 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 underlying View.setAlpha(float)
  • As other responders have pointed out ImageView.setImageAlpha(int) simply calls through to the deprecated ImageView.setAlpha(int). You should expect that ImageView.setAlpha(int) will be removed in a future API update and should therefore avoid using it.
2
  • 10
    This answer is ambiguous. ImageView.setAlpha(int) is deprecated only because of its name, and it's been renamed to ImageView.setImageAlpha(int). When you need to apply an alpha filter on an ImageView, it's preferred to use setImageAlpha(int) or the deprecated setAlpha(int) because they apply the alpha value directly to the underlying Paint which draws the image on the Canvas, while setAlpha(float) creates a temporary buffer where your view is drawn, then the buffer is blend with the rest of the screen with the specified alpha value.
    – BladeCoder
    Jul 21, 2015 at 23:42
  • Chet Haase clarifies what @BladeCoder pointed out in Google I/O 2013 - Android Graphics Performance video
    – azizbekian
    Oct 24, 2015 at 12:42
4

yes, it is only a naming difference - the current implementation in the Android source in API level 16 is:

/**
 * 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);
}
3
  • 1
    This is true, however setAlpha() has a @Deprecated annotation, so it's most probably setImageAlpha() implementation will change in the future
    – Jose_GD
    May 8, 2014 at 15:52
  • Not really. There's setAlpha(float alpha) which takes a float, not an int. Big difference. I mean, not the same thing at all.
    – Louis CAD
    Mar 13, 2019 at 13:39
  • Sorry Louis, you failed to see that in ImageView, there was a setAlpha(int alpha) in API 16, which this question was about.
    – cania
    Nov 28, 2019 at 12:56
1

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.

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.

Besides this i can't really think of a reason they have both an alpha method.

2
  • 1
    you hit on the key difference between setAlpha() and setImageAlpha() 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.
    – CrimsonX
    Nov 20, 2014 at 20:25
  • @CrimsonX: Looking at the source code (e.g. in cania's answer) it appears that the difference in behavior you describe doesn't actually exist. The two methods do the same thing. So it seems that this answer and the comment are incorrect.
    – LarsH
    Oct 2, 2015 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.