Android: drawableLeft 设置 alpha
Android: drawableLeft setting alpha
是否可以在 editText 中的 drawable 上将 alpha 设置为 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"/>
我从 Google 的 material 图标库中获得了图标。图标是 alpha 为 1 的地方(所以全黑)。例如,我想通过将 alpha 设置为 0.5 来使其略带灰色。
我想我可以使用 GIMP / Photoshop 来更改 alpha,但我更愿意使用 android 以编程方式执行此操作。
@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。
Drawable drawable = getResources().getDrawable(R.drawable.yourdrawable);
// set opacity
drawable.setAlpha(10);
//Set it
button.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0);
所以这就是我最终为实现我想要的结果所做的。
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.
我搜索了很多并发现这个独特的 - 对于我的研究 - 解决方案:
我用 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 在某些情况下非常有用。
是否可以在 editText 中的 drawable 上将 alpha 设置为 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"/>
我从 Google 的 material 图标库中获得了图标。图标是 alpha 为 1 的地方(所以全黑)。例如,我想通过将 alpha 设置为 0.5 来使其略带灰色。
我想我可以使用 GIMP / Photoshop 来更改 alpha,但我更愿意使用 android 以编程方式执行此操作。
@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。
Drawable drawable = getResources().getDrawable(R.drawable.yourdrawable);
// set opacity
drawable.setAlpha(10);
//Set it
button.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0);
所以这就是我最终为实现我想要的结果所做的。
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.
我搜索了很多并发现这个独特的 - 对于我的研究 - 解决方案:
我用 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 在某些情况下非常有用。