1. 程式人生 > >Android Drawable更改背景透明度

Android Drawable更改背景透明度

Drawable修改透明度

 <FrameLayout
            android:background="#ffffff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           />

1、getBackground();一定要設定background,不然報空指標。然後的到Drawable 2、通過drawable.setAlpha(0-255) 0完全透明,255不透明。

當然也可以直接在佈局裡設定android:alpha="",這個方法也可以0f-1f,但是如果在父佈局使用,那麼其他的子佈局也會跟著透明狀態顯示。 各有所需。

這設定動態更改透明度完全沒有任何問題,但是會存在其他同樣背景也會跟著改變 解決方法就是Drawable裡面的mutate()方法。 使用該方法,在可變的Drawable上使用此方法將無效。 Drawable drawable = getBackground().mutate(); drawable.setAlpha(0); 點選進入改方法裡面,裡面有介紹為什麼會導致其他同樣資源也會跟著改變