android - 通过 RemoteView 设置 GradientDrawable

标签 android drawable

这是我想做的:我有一个小部件,我想根据用户选择的颜色设置它的背景。它必须是一个渐变。背景是通过设置linearLayout的背景来设置的。为了测试,我做了一个虚拟背景:

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

我看过这个问题:Call setImageDrawable from RemoteViews但我无法理解如何实现。我什至找不到setXYZ()如那里所述。这是我到目前为止所尝试的:
  • 动态绘制渐变。在这种方法中,我无法设置背景,因为 AFAIK 所有方法都采用可绘制对象的 id 并且我有一个可绘制对象。
  • 尝试将 ImageView 作为背景(在 LinearLayout 之前)。它没有为小部件提供适当的边距。由于小部件文本是动态的,因此有时它会超出我想要的 imageView
  • 制作我拥有的 bg.xml:
    <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>
    

  • 现在我完全困惑和卡住了。有人可以尽快提供帮助( 可能更多的代码和更少的链接 )?另外,请不要像已经问过的那样关闭这个问题 .

    最佳答案

    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



    我不完全确定您的意思,但是如果您使用 FrameLayout/RelativeLayout 作为根布局,然后将 ImageView 与填充父级一起放入,您的图像应该与小部件的大小完全相同。
    <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>
    

    此外,这就是我正在做的动态更改圆角渐变背景的颜色和 alpha 的方法。然后使用 setImageViewBitmap( ) 应用到 imageview。可能有更好的方法。
    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;
        }
    }
    

    关于android - 通过 RemoteView 设置 GradientDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464535/

    相关文章:

    android - 如何调整可绘制复选框的大小以使其正确适合?

    java - 空指针异常和 getMapAsync 错误

    android - 在 Android 中查找和替换字符串

    android - Android中如何制作连续运行的TransitionDrawable动画?

    android - 无法将不同的 Drawables 设置为 recyclerview 项目

    android - 有关 Android 可绘制文件夹的问题

    java - 为什么我在使用realm.executeTransactionAsync()方法时遇到编译时错误?

    java - Android 应用程序启动时占用近 400 MB 内存

    android - 如何在 Android Print Framework 中将默认的 "Portrait Orientation"更改为 Landscape?

    android - 如何在 Android 中设置按钮内可绘制矢量的大小?