如何在小部件Android中更改图像的背景颜色

时间:2019-03-11 08:42:06

标签: android widget background-image

我在小部件中更改图像的背景色时遇到问题。 现在我正在这样使用:

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上运行? 谢谢。

1 个答案:

答案 0 :(得分:1)

另一个对我有用的解决方案是将图像源放在xml布局android:src="@mipmap/myImage"和代码remoteViews.setInt(R.id.myImageView,"setColorFilter", myColor);