简体   繁体   中英

Unable to setImageBitmap onto ImageView from Canvas bitmap

I am trying to render a canvas drawn bitmap onto an ImageView in an App Widget. Unable to get that working, I am trying a simpler example, without the RemoteViews object, so just trying to render some rectangles into a canvas, and then set that on the ImageView. I just see a blank activity. Here is the code sample:

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);

I've been tweaking various parts, such as trying out the setDrawingCacheEnabled method, after searching on stack overlow and in other places. Can anyone offer any insight here? Thanks!

Colors are presented in ARGB format, try changing them to;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM