美文网首页
Android 自定义View 卡顿优化

Android 自定义View 卡顿优化

作者: 钰大人 | 来源:发表于2018-12-14 17:08 被阅读0次

不要在onDraw的时候新建对象. 若需用到建议在初始化的位置赋值.

void initialize() {
  ...;
  mArrowPath = new Path();
}

// onDraw
if (mSecondTrianglePath.isEmpty()) {
    mSecondTrianglePath.reset();
    ...;
}
// 此时用AndroidStudio的Proifer分析的时候Native的内存基本是无变化的,不会出现丢帧的情况. 否则,内存开销会引发JVM的一些事情会阻塞UI线程的执行.

mTextPaint.getTextBounds("12", 0, "12".length(), mTextRect);
canvas.drawText("12", mCenterX, mCenterY-mOutSideRadius+mTextRect.height()/2, mTextPaint);

mTextPaint.getTextBounds("3", 0, "3".length(), mTextRect);
canvas.drawText("3", mCenterX+mOutSideRadius, mCenterY+mTextRect.height()/2, mTextPaint);
canvas.drawText("6", mCenterX, mCenterY+mOutSideRadius+mTextRect.height()/2, mTextPaint);
canvas.drawText("9", mCenterX-mOutSideRadius, mCenterY+mTextRect.height()/2, mTextPaint);
// 对象重用性质

对象的引用计数

void onDraw(Canvas) {
   Paint paint = drawable.getPaint();// 此处只是增加了引用计数.
}

相关文章

网友评论

      本文标题:Android 自定义View 卡顿优化

      本文链接:https://www.haomeiwen.com/subject/efwghqtx.html