我根据this演示构建了一个可缩放的可滚动视图。我在每个单元格中都有文字。它工作正常,但当用户缩放时,动画滞后。我找到了罪魁祸首:它是Canvas.drawText
,没有它,Rect
图纸正在顺利运行。
这是绘图的代码片段,带有一些伪代码:
@Override
protected void onDraw(Canvas canvas) {
float scale = VISIBLE_NUM_COLUMNS / mCurrentViewport.width();
float rectWidth = 1.f / mFactor * scale - 1;
float strokeWidth = scale * 1.f < 1.f ? 0 : scale * 1.f;
mBorderPaint.setStrokeWidth(strokeWidth);
mFontPaint.setTextSize(scale * TEXT_SIZE);
for (int i = first_visible_row; i < all_rows && i <= last_row; ++i) {
for (int j = first_visiblecolumn; j < all_columns && j <= last_visible_column; ++j) {
float rectLeft = getDrawX(j);
float rectTop = getDrawY(i);
// drawing the rect
float rectCenterX = rectLeft + rectWidth / 2;
float rectCenterY = rectTop + rectWidth / 2;
canvas.drawText(actual_text, rectCenterX, rectCenterY + -(mFontMetricsBuffer.ascent), mFontPaint);
}
}
}
...
@Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
// calculate new viewport
if (!awakenScrollBars()) {
ViewCompat.postInvalidateOnAnimation(MyView.this);
}
}
有没有办法加快文字绘图的速度?