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);
}
posted @ 2014-05-19 10:10  brave-sailor  阅读(3907)  评论(0编辑  收藏  举报