Java RemoteView中的动态GradientDrawable

Java RemoteView中的动态GradientDrawable,java,android,Java,Android,我无法为我的小部件设置动态背景: 我的首选项返回用户选择的颜色,我希望将其应用于小部件,但具有渐变效果。这就是我此刻的处境: My widget.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget_layout" android:layout_wi

我无法为我的小部件设置动态背景:

我的首选项返回用户选择的颜色,我希望将其应用于小部件,但具有渐变效果。这就是我此刻的处境:

My widget.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/style_widget"
> ...
如果我这样做:

remoteViews.setInt(R.id.widget_layout, "setBackgroundColor", color1);

我更改了背景颜色,但由于gradientDrawable不是int,如何通过RemoteView将其应用于背景

我在这里找到了答案:

我创建了一个填充整个小部件的Imageview,然后使用gradientDrawable创建了一个位图,然后将位图设置为Imageview:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/style_widget"
android:padding="0dip" >

<ImageView
        android:id="@+id/bck_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
    />
(我对Android非常陌生,所以我不知道代码是否格式良好,但对我来说,它可以工作,以防它能帮助任何人。)

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/style_widget"
android:padding="0dip" >

<ImageView
        android:id="@+id/bck_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
    />
GradientDrawable gradientDrawable = new GradientDrawable(
                    GradientDrawable.Orientation.LEFT_RIGHT, colors);               

float dpi = getBaseContext().getResources().getDisplayMetrics().xdpi;
float dp = getBaseContext().getResources().getDisplayMetrics().density;

Bitmap bitmap = Bitmap.createBitmap(Math.round(288 * dp), Math.round(72 * dp), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap); 
gradientDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
gradientDrawable.setCornerRadius(5 * (dpi/160));
gradientDrawable.draw(canvas);
remoteViews.setImageViewBitmap(R.id.bck_image, bitmap);