我正在尝试更改appWidget形状的颜色。我有一个带角的矩形,我的目标是改变背景(纯色)颜色和边框颜色。
我的小部件布局是一个RelativeLayout,背景为ImageView,此ImageView的src属性为此形状:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:radius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp" />
<stroke
android:width="1dp"
android:color="@android:color/white" />
<solid android:color="@color/transparent"/>
</shape>
以下是我尝试更改背景颜色时尝试做的事情:
RoundRectShape sd = new RoundRectShape(new float[]{30,30,30,30,30,30,30,30}, null, null);
bmp_bg = Bitmap.createBitmap(200, 20, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp_bg);
Paint p = new Paint();
p.setColor(0xFF0000FF);
sd.draw(c, p);
remoteViews.setImageViewBitmap(R.id.imageClock, bmp_bg);
但背景不会改变。这是一个很好的方法吗?还是我完全错了? THX
编辑: 我试过这个也没有任何影响:
CustomShapeClockWidget cscw = new CustomShapeClockWidget(0xFF0000FF, 0xFFFF0000);
bmp_bg = Bitmap.createBitmap(200, 20, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp_bg);
cscw.draw(c);
remoteViews.setImageViewBitmap(R.id.imageClock, bmp_bg);
类CustomShapeClockWidget为:
public class CustomShapeClockWidget extends ShapeDrawable {
private int bgColor, strokeColor;
private Paint fillPaint, strokePaint;
public CustomShapeClockWidget(int bgColor, int strokeColor){
this.bgColor = bgColor;
this.strokeColor = strokeColor;
}
@Override
protected void onDraw(Shape shape, Canvas canvas, Paint paint){
fillPaint = this.getPaint();
strokePaint = new Paint(fillPaint);
fillPaint.setColor(bgColor);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(1.0f);
strokePaint.setColor(strokeColor);
shape.draw(canvas, fillPaint);
shape.draw(canvas, strokePaint);
}
}
答案 0 :(得分:0)
(迟到的答案,希望它可以帮助任何在像我这样的appwidgets中挣扎的人)
您编写的第一个代码几乎已完成,只是在resize(float, float)
之前缺少对draw(Canvas, Paint)
的调用。无需创建自定义形状对象。
SDK documentation对RoundRectShape
和其他形状说明了这一点:
使用提供的Paint将此形状绘制到提供的Canvas中。 在调用之前,您必须调用resize(float,float)。