我有一個Draw方法是如下:凡初始化點變量/繪製方法給出警告
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
a = new Point(0, 0);
b = new Point(canvas.getWidth(), canvas.getHeight()/2);
c = new Point(canvas.getWidth(),0);
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
canvas.drawPath(path, paint);
canvas.rotate(36);
paint.setColor(Color.WHITE);
canvas.drawText(text_to_disp, (canvas.getHeight()/4),0- 15, paint_text);
}
這裏的爭用點變量的初始化,我得到了日食警告:Avoid object allocations during draw/layout operations (preallocate and reuse instead)
但是我沒有在這個類的任何其他方法中獲得對canvas的參考,我如何在其他地方初始化這些變量?
我也有這樣的方法:
public void init(){
paint = new Paint();
paint.setStrokeWidth(4);
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
paint.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
setLayerType(LAYER_TYPE_SOFTWARE, paint);
path = new Path();
path.setFillType(FillType.EVEN_ODD);
paint_text = new Paint();
helv_light = Typeface.createFromAsset(MainAct_Demo.con.getAssets(), pathHelv_light);
paint_text.setTypeface(helv_light);
paint_text.setTextSize(32);
paint_text.setAntiAlias(true);
paint_text.setColor(android.graphics.Color.WHITE);
}
擺脫'點'一起。而不是'path.moveTo(ax,ay);'只是使用'path.moveTo(0,0);'而不是'path.lineTo(bx,by);'只需使用'path.lineTo(canvas.getWidth (),canvas.getHeight()/ 2);'等等。現在你不必每次都創建3個新對象,每次調用onDraw(),這可能是很多。 – csmckelvey 2014-11-24 04:10:57
是的,因爲我在列表視圖中使用它,我可以感覺到滯後。 – User3 2014-11-24 04:12:26
您基本上正在創建3個對象,將數據插入它們,然後立即使用剛插入的數據。您可以刪除大部分流程,直接使用數據。 – csmckelvey 2014-11-24 04:13:11