Android ImageView setAlpha(float)、setAlpha(int)及setImageAlpha(int)的区别及踩过的坑

做项目中踩过的一个坑,记录一下

需求是先把ImageView设置成透明不可见,然后在某个条件下再设置成可见,代码如下

1
2
mImageView.setAlpha(0); // 设置成透明
mImageView.setAlpha(1f); // 设置成不透明

Click and drag to move

结果是设置成透明可以,但是要再设置成不透明显示控件却死活不可以,后来改成如下代码就可以了

1
2
mImageView.setAlpha(0f); // 设置成透明
mImageView.setAlpha(1f); // 设置成不透明

Click and drag to move

原来setAlpha(0)和setAlpha(0f)分别调用的是不同的方法

setAlpha(int)

这是在ImageView中定义的方法,已经被废弃了,取值范围是0-255,

0表示透明

255表示不透明

所以如果要设置ImageView为不透明,正确的值是255,而不是1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* Sets the alpha value that should be applied to the image.
*
* @param alpha the alpha value that should be applied to the image
*
* @deprecated use #setImageAlpha(int) instead
*/
@Deprecated
@RemotableViewMethod
public void setAlpha(int alpha) {
alpha &= 0xFF; // keep it legal
if (mAlpha != alpha) {
mAlpha = alpha;
mColorMod = true;
applyColorMod();
invalidate();
}
}

Click and drag to move

setAlpha(float)

这其实是在ImageView的父类View中定义的方法,推荐用此方法,取值范围为0.0f-1.0f

0.0f表示透明

1.0f表示不透明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Sets the opacity of the view to a value from 0 to 1, where 0 means the view is
* completely transparent and 1 means the view is completely opaque.
*
* <p class="note"><strong>Note:</strong> setting alpha to a translucent value (0 < alpha < 1)
* can have significant performance implications, especially for large views. It is best to use
* the alpha property sparingly and transiently, as in the case of fading animations.</p>
*
* <p>For a view with a frequently changing alpha, such as during a fading animation, it is
* strongly recommended for performance reasons to either override
* {@link #hasOverlappingRendering()} to return <code>false</code> if appropriate, or setting a
* {@link #setLayerType(int, android.graphics.Paint) layer type} on the view for the duration
* of the animation. On versions {@link android.os.Build.VERSION_CODES#M} and below,
* the default path for rendering an unlayered View with alpha could add multiple milliseconds
* of rendering cost, even for simple or small views. Starting with
* {@link android.os.Build.VERSION_CODES#M}, {@link #LAYER_TYPE_HARDWARE} is automatically
* applied to the view at the rendering level.</p>
*
* <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
* responsible for applying the opacity itself.</p>
*
* <p>On versions {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and below, note that if
* the view is backed by a {@link #setLayerType(int, android.graphics.Paint) layer} and is
* associated with a {@link #setLayerPaint(android.graphics.Paint) layer paint}, setting an
* alpha value less than 1.0 will supersede the alpha of the layer paint.</p>
*
* <p>Starting with {@link android.os.Build.VERSION_CODES#M}, setting a translucent alpha
* value will clip a View to its bounds, unless the View returns <code>false</code> from
* {@link #hasOverlappingRendering}.</p>
*
* @param alpha The opacity of the view.
*
* @see #hasOverlappingRendering()
* @see #setLayerType(int, android.graphics.Paint)
*
* @attr ref android.R.styleable#View_alpha
*/
public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) {
ensureTransformationInfo();
if (mTransformationInfo.mAlpha != alpha) {
setAlphaInternal(alpha);
if (onSetAlpha((int) (alpha * 255))) {
mPrivateFlags |= PFLAG_ALPHA_SET;
// subclass is handling alpha - don't optimize rendering cache invalidation
invalidateParentCaches();
invalidate(true);
} else {
mPrivateFlags &= ~PFLAG_ALPHA_SET;
invalidateViewProperty(true, false);
mRenderNode.setAlpha(getFinalAlpha());
}
}
}

Click and drag to move

setImageAlpha(int)

看源码可知setImageAlpha(int)实际内部调用的是setAlpha(int),所以取值返回也是0-255,表示从透明到不透明

1
2
3
4
5
6
7
8
9
10
11
12
/**
* Sets the alpha value that should be applied to the image.
*
* @param alpha the alpha value that should be applied to the image (between
* 0 and 255 inclusive, with 0 being transparent and 255 being opaque)
*
* @see #getImageAlpha()
*/
@RemotableViewMethod
public void setImageAlpha(int alpha) {
setAlpha(alpha);
}

Click and drag to move

总结

  • setAlpha(float)推荐使用,取值范围为0.0f-1.0f,表示透明到不透明
  • setAlpha(int),已废弃,不推荐使用,取值范围为1-255,表示透明到不透明
  • setImageAlpha(int),内部实际调用setAlpha(int),取值范围也是1-255,表示透明到不透明