我正在尝试在onDraw()方法中保存Canvas对象。
这是因为我在onDraw方法中使用foreach循环导致:
canvas.DrawText(textitem,x,y,textpaint);
(我必须这样做,因为我在蒙面区域周围绘制文字)
@Override
public void onDraw(Canvas canvas)
{
if (hasrun = false)
{
for(CustomTextViewDrawItem item : drawItemList)
{
canvas.drawText(item.Text, item.X, item.Y, textPaint);
}
if (eLabel.backgroundGradient != null)
{
canvas.drawPath(path, fillPaint);
}
canvas.save();
savedCanvas = canvas ;
}
else
{
canvas = savedCanvas;
}
hasrun = true;
super.onDraw(canvas);
}
调试时我看到它看起来不错,但是空出来了。 什么是让这个工作的最佳方式?
答案 0 :(得分:2)
也许是因为这个:
if (hasrun = false)
我想你打算这样做:
if (hasrun == false)
答案 1 :(得分:0)
您可以尝试保存位图:(我认为最好调用super.onDraw(canvas);在方法的开头,因为您的视图相关绘图将位于顶部)
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (savedBitmap==null){
savedBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
Canvas canvasToSave = new Canvas(savedBitmap)
for(CustomTextViewDrawItem item : drawItemList){
canvasToSave.drawText(item.Text, item.X, item.Y, textPaint);
}
if (eLabel.backgroundGradient != null){
canvasToSave.drawPath(path, fillPaint);
}
}
canvas.drawBitmap(savedBitmap, 0, 0, new Paint());
}