2013-12-12 136 views
1

我正在嘗試更改appWidget形狀中的顏色。我有一個帶角的矩形,我的目標是改變背景(實心)顏色和邊框顏色。更改appWidget中的形狀

我的窗口小部件佈局與背景的ImageView的一個RelativeLayout的,這個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

(很晚的評論,但是)您是否在調用remoteViews.setImageViewBitmap後更新appwidget? –

回答

0

(遲到的回答,希望它可以幫助任何人在appwidgets形狀掙扎像我一樣)

你寫的第一個代碼差不多完成了,它只是缺少對的調用在draw(Canvas, Paint)之前需要。不需要創建自定義形狀對象。

SDK documentation說這對於RoundRectShape和其他形狀:

繪製該形狀到提供帆布,與所提供的畫圖。 在調用此函數之前,您必須調用resize(float,float)