Setting GradientDrawable through RemoteView

不羁岁月 提交于 2019-12-02 00:10:45

问题


Here is what I want to do: I have a widget and I want to set its background depending upon users choice of colors. It has to be a gradient. The backgound is to be set by setting background of the linearLayout. For testing, I did it for a dummy-background as:

remoteViews.setInt(R.id.layout, "setBackgroundResource", R.drawable.widget_background);

I have seen this question: Call setImageDrawable from RemoteViews but I am not able to understand how to implement. I even can't find setXYZ() as mentioned there. Here is what I have tried until now:

  1. Making a gradient drawable dynamically. In this approach, I am not able to set the background beacause AFAIK all the methods take id of the drawable and I have a drawable object.
  2. Tried ImageView as a background (before LinearLayout). It does not provide proper margin to widget. Since the widget text is dynamic, sometimes it goes out of the imageView which is not what I want

  3. Making a bg.xml in which I have:

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
           <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />
           <corners
                android:bottomLeftRadius="7dp"
                android:bottomRightRadius="7dp"
                android:topLeftRadius="7dp"
                android:topRightRadius="7dp" />
    </shape>
    

Now I am totally confused and stuck. Can someone help(probably more of code and less of links) ASAP? Also, please don't close this question as already asked.


回答1:


Tried ImageView as a background (before LinearLayout). It does not provide proper margin to widget. Since the widget text is dynamic, sometimes it goes out of the imageView which is not what I want

I'm not entirely sure what you mean, but if you use a FrameLayout / RelativeLayout for your root layout, and then put the ImageView inside with fill parent, your image should be exactly the size of your widget.

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="6dp" >

    <ImageView
        android:id="@+id/widgetBg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    // Other views

</FrameLayout>

Also, this is what I'm doing to dynamically change the color & alpha of a rounded corner gradient background. Then use setImageViewBitmap( ) to apply to the imageview. Probably there is a better way.

public static Bitmap getBackground(int bgColor, int width, int height, Context context) {
    try {
        // convert to HSV to lighten and darken
        int alpha = Color.alpha(bgColor);
        float[] hsv = new float[3];
        Color.colorToHSV(bgColor, hsv);
        hsv[2] -= .1;
        int darker = Color.HSVToColor(alpha, hsv);
        hsv[2] += .3;
        int lighter = Color.HSVToColor(alpha, hsv);

        // create gradient useng lighter and darker colors
        GradientDrawable gd = new GradientDrawable(
                GradientDrawable.Orientation.LEFT_RIGHT,new int[] { darker, lighter});
        gd.setGradientType(GradientDrawable.RECTANGLE);
        // set corner size
        gd.setCornerRadii(new float[] {4,4,4,4,4,4,4,4});

        // get density to scale bitmap for device
        float dp = context.getResources().getDisplayMetrics().density;

        // create bitmap based on width and height of widget
        Bitmap bitmap = Bitmap.createBitmap(Math.round(width * dp), Math.round(height * dp),
                Bitmap.Config.ARGB_8888);
        Canvas canvas =  new Canvas(bitmap);
        gd.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        gd.draw(canvas);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}


来源:https://stackoverflow.com/questions/12464535/setting-gradientdrawable-through-remoteview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!