Android中關於在onDrow或者onMeasure中創建對象提示Avoid object allocations during draw/layout operations (preallocate and reuse instead) 問題


     在實際開發中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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM