无法从Canvas位图将setImageBitmap设置到ImageView上

时间:2012-09-17 19:49:15

标签: android imageview android-canvas

我正在尝试将画布绘制的位图渲染到App Widget中的ImageView上。无法实现这一点,我正在尝试一个更简单的例子,没有RemoteViews对象,所以只是尝试将一些矩形渲染到画布中,然后在ImageView上设置它。我刚看到一个空白的活动。这是代码示例:

LinearLayout layout = new LinearLayout (this);
layout.setDrawingCacheEnabled(true);
try
{
    Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap);
    Paint red = new Paint ();
    red.setColor(0xCC0000);
    Paint blue = new Paint ();
    blue.setColor (0x0099CC);
    canvas.drawRect(0, 0, 100, 100, red);
    int percent = 55;
    canvas.drawRect(0, 0, percent, 100, blue);
    ImageView imageView = new ImageView (this);
    imageView.setDrawingCacheEnabled(true);
    imageView.setImageBitmap(bitmap);
    imageView.setAdjustViewBounds(true);
    imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
              LayoutParams.WRAP_CONTENT));
    layout.addView(imageView);

}
catch(Exception e)
{
    Log.e("ElectionForecastWidgetProvider", "error with bar", e);
}
this.setContentView(layout);

我一直在调整各种各样的部分,比如尝试使用setDrawingCacheEnabled方法,在堆栈overlow和其他地方搜索之后。任何人都可以提供任何见解吗?谢谢!

1 个答案:

答案 0 :(得分:1)

颜色以ARGB格式显示,请尝试将其更改为;

...
red.setColor(0xFFCC0000);
...
blue.setColor (0xFF0099CC);
...