2015-08-14 178 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在某些情况下非常有用。