我在互联网上搜索并了解这是由于内存问题。
我使用以下代码更新ImageView。
private void update(Context context) {
Log.d("Configure", "updatestart");
remoteViews.setImageViewBitmap(R.id.clockview,
buildUpdate("CURRENTTIME", c));
awm.updateAppWidget(awID, remoteViews);
}
buildUpdate函数:
public Bitmap buildUpdate(String time, Context context) {
Log.d("ConfigureApp","Buildupdate_fn ---" + "start");
date = new Date();
sec = (float) date.getSeconds();
min = (float) date.getMinutes();
hour = (float) date.getHours() + min / 60.0f;
bitmap = Bitmap.createBitmap(200, 200, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(0xFFFF0000);
p.setStrokeWidth(2);
canvas.drawLine(
x,
y,
(float) (x + (r - 15)
* Math.cos(Math
.toRadians((hour / 12.0f * 360.0f) - 90f))),
(float) (y + (r - 10)
* Math.sin(Math
.toRadians((hour / 12.0f * 360.0f) - 90f))), p);
canvas.save();
p.setColor(0xFF0000FF);
canvas.drawLine(
x,
y,
(float) (x + r
* Math.cos(Math.toRadians((min / 60.0f * 360.0f) - 90f))),
(float) (y + r
* Math.sin(Math.toRadians((min / 60.0f * 360.0f) - 90f))),
p);
canvas.save();
p.setColor(0xFFA2BC13);
canvas.drawLine(
x,
y,
(float) (x + (r + 10)
* Math.cos(Math.toRadians((sec / 60.0f * 360.0f) - 90f))),
(float) (y + (r + 15)
* Math.sin(Math.toRadians((sec / 60.0f * 360.0f) - 90f))),
p);
remoteViews.setTextViewText(R.id.mycity, Float.toString(x));
return bitmap;
}
请告诉我如何避免这个错误。:(有没有其他方法可以做同样的事情?
提前致谢,
答案 0 :(得分:2)
如果这是内存错误,那么尝试重用位图,而不是每次调用buildUpdate()
时都创建新的。
此外,AFAIK,canvas.save()
仅在您实际拨打canvas.restore()
时才有用。