2015-08-14 176 views
2

無論如何設置繪圖坐在一個editText作爲drawableLeft阿爾法?Android:drawableLeft設置阿爾法

  <EditText 
       android:layout_width="match_parent" 
       android:layout_height="40dp" 
       android:background="@drawable/edittext_middle_bg" 
       android:id="@+id/birthday_overlay" 
       android:editable="false" 
       android:focusable="false" 
       android:padding="10dp" 
       android:hint="Birthday" 
       android:textColorHint="#bbbbbb" 
       android:drawablePadding="10dp" 
       android:drawableLeft="@drawable/ic_cake_black_24dp"/> 

birthdayIcon

我從由谷歌材料圖標庫的圖標。該圖標是阿爾法是1(如此全黑)的地方。例如,我想通過製作alpha 0.5來使它變得略帶灰色。

我想我可以使用GIMP/Photoshop更改alpha,但寧願使用android以編程方式執行此操作。

+0

你不能在xml中給它,但以編程方式使用setAlpha()。 –

+0

在@Simon下面爲我做了答案嗎? –

+0

嗨,我需要稍微修改一下代碼,然後才能測試它。目前我得到一個drawable:Drawable [] birthdayDrawables = birthday.getCompoundDrawables();因爲你有觀點,所以我無法將它傳遞給你的方法。 – Simon

回答

4

所以這最終是我做了什麼來實現我想要的結果。

Drawable[] birthdayDrawable = birthdayOverlay.getCompoundDrawables(); 
    birthdayDrawable[0].setAlpha(128); //set it at 128 so that it has 50% opacity. The opactiy here ranges from 0 to 255. 
1
@SuppressLint("NewApi") 
public static void setAlpha(View view, float alpha){ 
    if (Build.VERSION.SDK_INT < 11) { 
     final AlphaAnimation animation = new AlphaAnimation(alpha, alpha); 
     animation.setDuration(0); 
     animation.setFillAfter(true); 
     view.startAnimation(animation); 
    } 
    else 
     view.setAlpha(alpha); 
} 

使用此方法。從EditText傳遞參數作爲drawableLeft。這適用於< = API 16和> API 16.

1
Drawable drawable = getResources().getDrawable(R.drawable.yourdrawable); 

// set opacity 
drawable.setAlpha(10); 

//Set it 
button.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0); 
+1

我的回答說明瞭不推薦使用的setAlpha方法。這種方式已被棄用<= API 16. –

+0

酷:D很高興知道 – Ben

0

我搜索到很多,發現這種獨特的 - 我的研究 - 解決方案:

我做了CountDownTimer,做工不錯。

Drawable[] compundDrawables = textview_with_drawableLef_Image.getCompoundDrawables(); 
Drawable leftCompoundDrawable = compundDrawables[0]; 

而且在countdowntimer後,我眨眼leftCompoundDrawable設置:

leftCompoundDrawable.setApha(AlphaNumber); 

工作好,需要一點調整countDownInterval和Alpha值的步驟中,檢查時,alpha爲未成年人0以上到255(Drawables中的Alpha從0到255),並將步驟alpha更改爲+或 - ,但容易調整以獲得良好的外觀。

CountDownTimer在某些情況下非常有用。