通过远程视窗设置GradientDrawable [英] Setting GradientDrawable through RemoteView
问题描述
这是我想做的事:我有一个小窗口,我想设置它的背景,这取决于用户选择的颜色。它必须是一个梯度。的backgound是通过设置的LinearLayout的背景来设定。为了测试,我做了一个假,背景为:
remoteViews.setInt(R.id.layout,setBackgroundResource,R.drawable.widget_background);
我已经看到了这个问题:呼叫setImageDrawable从RemoteViews 的,但我无法理解如何实现。我甚至无法找到 setXYZ()
提到那里。这里是我曾尝试到现在为止:
- 动态制作渐变绘制。在这种方法中,我不能够设置背景东阳AFAIK所有的方法都被拉伸的ID和我有一个可绘制对象。
-
试过的ImageView作为背景(LinearLayout中之前)。它不提供适当的余量小部件。由于该部件的文字是动态的,有时它会出来这不是ImageView的什么,我想
-
制作bg.xml中,我有:
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android> <填充 机器人:底部=1DP 机器人:左=1DP 机器人:右=1DP 机器人:顶部=1DP/> <角落 机器人:bottomLeftRadius =7DP 机器人:bottomRightRadius =7DP 机器人:topLeftRadius =7DP 机器人:topRightRadius =7DP/> < /形状>
现在我完全糊涂了,并卡住。有人可以帮(可能更多的code和更少的链接)尽快?另外,请不要关闭这个问题前面已经问
试过的ImageView作为背景(LinearLayout中之前)。它不提供适当的余量小部件。由于该部件的文字是动态的,有时它会出来这不是ImageView的什么,我想
我不完全知道你的意思,但如果你使用的FrameLayout / RelativeLayout的为你的根布局,然后把ImageView的内部,填充母,你的形象应该是完全相同的小部件的大小。
<的FrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_margin =6DP>
< ImageView的
机器人:ID =@ + ID / widgetBg
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:scaleType =fitXY/>
//其他意见
< /的FrameLayout>
此外,这是我在做什么,以动态地改变颜色和放大器;阿尔法的圆角渐变背景。然后使用setImageViewBitmap()申请到ImageView的。也许有一个更好的办法。
公共静态位图的getBackground(INT BGCOLOR,诠释的宽度,高度INT,上下文语境){
尝试 {
//转换为HSV,以减轻和变暗
INT阿尔法= Color.alpha(BGCOLOR);
浮动[] HSV =新的浮动[3];
Color.colorToHSV(BGCOLOR,HSV);
HSV [2] - = 0.1;
INT较暗= Color.HSVToColor(α,HSV);
HSV [2] + = 0.3;
INT打火机= Color.HSVToColor(α,HSV);
//创建渐变useng更轻,较暗的颜色
GradientDrawable GD =新GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,新的INT [] {较深,更轻});
gd.setGradientType(GradientDrawable.RECTANGLE);
//设置角落的大小
gd.setCornerRadii(新浮法[] {4,4,4,4,4,4,4,4});
//获取密度扩展位图的设备
浮DP = context.getResources()getDisplayMetrics()密度。
//基于宽度和窗口小部件的高度创建位图
点阵位图= Bitmap.createBitmap(Math.round(宽* DP),Math.round(高* DP)
Bitmap.Config.ARGB_8888);
帆布油画=新的Canvas(位);
gd.setBounds(0,0,canvas.getWidth(),canvas.getHeight());
gd.draw(画布);
返回的位图;
}赶上(例外五){
e.printStackTrace();
返回null;
}
}
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:
- 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.
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
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.
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;
}
}
这篇关于通过远程视窗设置GradientDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!