我在Activity中有一個可繪製的對象。用於繪製的setAlpha()
以api級別1引入,但getAlpha()
以api級別19(Kitkat)引入。是否有任何替代API或支持庫來獲取drawable的alpha/opacity值。如何獲取api的Drawable的alpha值小於11
而且我也有NineOldAndroids庫,但getAlpha()
只適用於VIEWS。
我在Activity中有一個可繪製的對象。用於繪製的setAlpha()
以api級別1引入,但getAlpha()
以api級別19(Kitkat)引入。是否有任何替代API或支持庫來獲取drawable的alpha/opacity值。如何獲取api的Drawable的alpha值小於11
而且我也有NineOldAndroids庫,但getAlpha()
只適用於VIEWS。
試試這種方式,希望這會幫助你解決你的問題。
if(Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB){
v.setAlpha((Math.round(alpha*255)));
}else{
v.setAlpha(alpha);
}
有越來越被拉伸前的API的Alpha值19 總之,根據你有什麼樣的可繪製的沒有通用的方法,你可以查看源代碼的解決方法,扣除阿爾法。
例如,查看ColorDrawable源代碼,很容易看出您可以在Kitkat之前移植實現。
@Override
public int getAlpha() {
return mColorState.mUseColor >>> 24;
}
所以drawable.getAlpha()
成爲drawble.getColor() >>> 24
編輯:
這裏是一個uncomplete嘗試以進行compat的方法,我會嘗試用時間來更新這個:
public static int getAlphaCompat(Drawable drawable) {
if(drawable instanceof ColorDrawable) {
return ((ColorDrawable) drawable).getColor() >>> 24;
} else if(drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getPaint().getAlpha();
} else if(drawable instanceof RotateDrawable) {
return getAlphaCompat(((RotateDrawable) drawable).getDrawable());
} else if(drawable instanceof ScaleDrawable) {
return getAlphaCompat(((ScaleDrawable) drawable).getDrawable());
} else if(drawable instanceof ClipDrawable) {
//TODO: possible with reflection
} else if(drawable instanceof ShapeDrawable) {
//TODO: possible with reflection
} else if(drawable instanceof DrawableContainer) {
//TODO: possible with reflection
} else if(drawable instanceof GradientDrawable) {
//TODO: possible with reflection
}
return -1;
}
將此庫添加到build.gradle中ndencies部分:
com.android.support:support-v4:24.0.0
下一個方法變得可用於API級別4以上:
DrawableCompat.getAlpha(Drawable d)
[編輯] 注意DrawableCompat.getAlpha
只是返回0
下面19. See source code
這也適用於支持23。 – John61590 2016-07-27 20:38:54
針對pre-19/KitKat SDK的這個實現只是簡單的返回0; - 注意這不會在KitKat之前返回實際的alpha值。 – 2016-10-24 13:54:19
我有版本要求獲取alpha值,但沒有設置它。 – 2014-09-02 17:05:35
'setAlpha'可從API 1獲得。 – weston 2017-02-01 15:03:40