Android 設置view透明度(Alpha)包括子view
Android 設置view透明度工具方法
/**
* 設置view 透明度 包括子view
*
* @param view
* @param alpha 10進制
*/
public static void setAlphaAllView(View view, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (view == null) {
return;
}
if (view.getBackground() != null) {
view.getBackground().mutate().setAlpha((int) (alpha * 255));
}
float alphaNum = alpha;
EvtLog.d(TAG, "setAlphaAllView alpha:" + alpha + " alphaNum:" + alphaNum);
view.setAlpha(alphaNum);
//設置子view透明度
if (view instanceof ViewGroup) {
ViewGroup vp = (ViewGroup) view;
for (int i = 0; i < vp.getChildCount(); i++) {
View viewChild = vp.getChildAt(i);
//調用本身(遞歸)
setAlphaAllView(viewChild, alpha);
}
}
}
部分簡單動畫效果可能用到這種方法