天天看點

Android中關于在onDrow或者onMeasure中建立對象提示Avoid object allocations during draw/layout operations (prealloca...

     在實際開發中Android中自帶的控件有時無法滿足我們的需求,這時就需要我們重寫控件來實作我們想要的功能。

還有個關于UI體驗的問題,就是在onDraw()函數中最好不要去建立對象,否則就提示下面的警告資訊:因為onDraw()調用頻繁,不斷進行建立和垃圾回收會影響UI顯示的性能

例如:

protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  Paint mpatin = new Paint();
  mpatin.setTextAlign(Align.CENTER);
  mpatin.setColor(mcolor);
  mpatin.setTextSize(mtextsize);
  canvas.drawText(mtext, canvas.getWidth() / 2,
  (canvas.getHeight() / 2) + 6, mpatin);
}