簡體   English   中英

無法從Canvas位圖將ImageBitmap設置到ImageView上

[英]Unable to setImageBitmap onto ImageView from Canvas bitmap

我正在嘗試在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方法。 有人可以在這里提供任何見解嗎? 謝謝!

顏色以ARGB格式顯示,請嘗試將其更改為;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM