[英]How to change background color at image in widget android
我在小部件中更改圖像的背景色時遇到問題。 現在我正在使用這樣的:
try {
Class c = Class.forName("android.widget.RemoteViews");
Method m = c.getMethod("setDrawableParameters", int.class, boolean.class, int.class, int.class, PorterDuff.Mode.class, int.class);
m.invoke(remoteViews, R.id.myImage, true, -1, Color.HSVToColor(color), PorterDuff.Mode.SRC_OVER, -1);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
一切正常,直到我在android 9上測試,在這里我收到一個錯誤:
java.lang.NoSuchMethodException:setDrawableParameters [int,boolean,int,int,類android.graphics.PorterDuff $ Mode,int]
你有什么想法我也可以使其在android 9上工作嗎? 謝謝。
setDrawableParameters(..)
在Android 9.0中稱為setDrawableTint(..)
。
這是Android 9.0中可用的新方法setDrawableTint(..)
的源代碼,我想它的作用與Android早期版本中的setDrawableParameters(..)
相同,但您必須在項目中進行嘗試。
/**
* Equivalent to calling
* {@link Drawable#setColorFilter(int, android.graphics.PorterDuff.Mode)},
* on the {@link Drawable} of a given view.
* <p>
* The operation will be performed on the {@link Drawable} returned by the
* target {@link View#getBackground()} by default. If targetBackground is false,
* we assume the target is an {@link ImageView} and try applying the operations
* to {@link ImageView#getDrawable()}.
* <p>
*/
private class SetDrawableTint extends Action {
SetDrawableTint(int id, boolean targetBackground,
int colorFilter, @NonNull PorterDuff.Mode mode) {
this.viewId = id;
this.targetBackground = targetBackground;
this.colorFilter = colorFilter;
this.filterMode = mode;
}
SetDrawableTint(Parcel parcel) {
viewId = parcel.readInt();
targetBackground = parcel.readInt() != 0;
colorFilter = parcel.readInt();
filterMode = PorterDuff.intToMode(parcel.readInt());
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(viewId);
dest.writeInt(targetBackground ? 1 : 0);
dest.writeInt(colorFilter);
dest.writeInt(PorterDuff.modeToInt(filterMode));
}
@Override
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
final View target = root.findViewById(viewId);
if (target == null) return;
// Pick the correct drawable to modify for this view
Drawable targetDrawable = null;
if (targetBackground) {
targetDrawable = target.getBackground();
} else if (target instanceof ImageView) {
ImageView imageView = (ImageView) target;
targetDrawable = imageView.getDrawable();
}
if (targetDrawable != null) {
targetDrawable.mutate().setColorFilter(colorFilter, filterMode);
}
}
@Override
public int getActionTag() {
return SET_DRAWABLE_TINT_TAG;
}
boolean targetBackground;
int colorFilter;
PorterDuff.Mode filterMode;
}
您可以檢查新方法的工作方式以及需要哪些參數,然后在項目中添加以下內容:
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.P){
// Your actual implementation with "setDrawableParameters"
changeImageBackgroundColorBeforeAndroidPIE()
} else{
// Your new implementation with "setDrawableTint"
changeImageBackgroundColorAndroidPIEAndLater()
}
另一個解決方案可能是:
正如我看到的setDrawableParameters(..)
方法是隱藏的,因此不應以這種方式使用它,我想它不是解決您問題的最佳解決方案。 它看起來像一個駭人聽聞的解決方案,但是一旦他們更改了它,或者一旦您認為它無法正常運行,它就會變得更糟。
讓我們從頭開始,您想更改小部件中圖像的背景顏色,更確切地說,在RemoteView
,可能可以將drawable
轉換為bitmap
,然后調用RemoteViews.setImageBitmap()
更新:作者剛剛發現的另一個解決方案是
對我有用的另一個解決方案是在xml布局android:src =“ @ mipmap / myImage”和代碼remoteViews.setInt(R.id.myImageView,“ setColorFilter”,myColor)中設置圖像源;
這是作者的答案: https : //stackoverflow.com/a/55123126/3564632
對我remoteViews.setInt(R.id.myImageView,"setColorFilter", myColor);
另一個解決方案是在xml布局android:src="@mipmap/myImage"
和代碼remoteViews.setInt(R.id.myImageView,"setColorFilter", myColor);
設置圖像源remoteViews.setInt(R.id.myImageView,"setColorFilter", myColor);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.