2017-10-04 146 views
2

我想在圖像頂部添加透明黑色並使其更暗。使ImageView具有黑暗的透明度

enter image description here

  <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:id="@+id/rest_image" 
        android:layout_width="match_parent" 
        android:layout_height="150dp" 
        android:adjustViewBounds="true" 
        android:scaleType="centerCrop" 
        /> 
      </RelativeLayout> 

我可以設置阿爾法參數,但顏色變化爲白色。 我想製作像這樣的較暗的圖像。 如何在xml或Java代碼中執行此操作。我會根據情況設置它。?

謝謝。

+0

您可以創建一個較暗的「形狀」,並用作「FrameLayout」的背景。 –

+1

或者你可以谷歌'android colormatrix darken' –

回答

6

你需要什麼叫做着色。套用色彩到您的ImageView

<ImageView 
    ... 
    android:tint="#6F000000" 
    /> 
0

最簡單的/最快的解決辦法是在XML

您的ImageView頂部添加的第二層(可以是一個視圖,並不一定是一個ImageView的) ,具有所需的顏色/ alpha。在需要時顯示/隱藏它。

  <ImageView 
       android:id="@+id/rest_image" 
       android:layout_width="match_parent" 
       android:layout_height="150dp" 
       android:adjustViewBounds="true" 
       android:scaleType="centerCrop" 
       /> 

      <View 
       android:id="@+id/overlay_image" 
       android:layout_width="match_parent" 
       android:layout_height="150dp" 
       android:background=「@color/yourColorWithAlpha" 
       /> 
     </RelativeLayout> 
+0

謝謝,這工作 –

0

嘗試此第二imageview的將設置透明的顏色。根據您的需要調整高度。

500000 - 最後4位代表黑色,前兩位代表你想要設置的alpha。

     <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 

         <ImageView 
          android:id="@+id/rest_image" 
          android:layout_width="match_parent" 
          android:layout_height="150dp" 
          android:adjustViewBounds="true" 
          android:scaleType="centerCrop" 
          /> 

          <ImageView 
          android:layout_width="match_parent" 
          android:layout_height="150dp" 
          android:background="#500000" 
          /> 
        </RelativeLayout>