Android:drawableLeft设置alpha

时间:2015-08-14 16:41:58

标签: android android-edittext

无论如何要将editText中的drawable坐标设置为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

我从Google的材料图标库中获取了图标。图标是alpha为1(如此全黑)。我想通过制作alpha 0.5来使它略显灰色。

我想我可以使用GIMP / Photoshop更改alpha,但更喜欢使用android来以编程方式执行此操作。

4 个答案:

答案 0 :(得分: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 :(得分: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); 
}

使用此方法。将参数作为您的drawableLeft从EditText传递。这适用于&lt; = API 16和&gt; API 16.

答案 2 :(得分:1)

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

// set opacity
drawable.setAlpha(10);

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

答案 3 :(得分:0)

我搜索了很多,并发现这个独特的 - 对于我的研究 - 解决方案:

我用CountDownTimer制作,效果很好。

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

在倒数计时器之后,我将leftCompoundDrawable设置为:

leftCompoundDrawable.setApha(AlphaNumber);

工作正常,需要稍微调整countDownInterval和Alpha Value Step,检查alpha小于0或更大到255(Drawables中的Alpha是0到255)并将步骤alpha更改为+或 - ,但是易于调整以获得良好的外观。

在某些情况下,CountDownTimer非常有用。