通过参考http://developer.android.com/training/custom-views/optimizing-view.html#accelerate,我知道我可以onDraw
通过使用setLayerType(View.LAYER_TYPE_HARDWARE, null);
.
我在 Android 4 上进行了测试,方法是执行 home -> restore -> home。我意识到onDraw
只会被调用一次,如上述 android 文档中所述。
但是,如何在 Android 2.3 中实现类似的优化?由于 Android 2.3 不支持硬件优化。
public class PieChart extends View {
@SuppressLint("NewApi")
public PieChart(Context context) {
super(context);
mPiePaint = new Paint();
mPiePaint.setAntiAlias(true);
mPiePaint.setStyle(Paint.Style.FILL);
mPiePaint.setColor(0x88FF0000);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
if (!isInEditMode()) {
setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i("CHEOK", "Busy onDraw is called");
canvas.drawArc(mBounds,
0,
200,
true, mPiePaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mBounds = new RectF(0, 0, w, h);
}
private RectF mBounds;
private Paint mPiePaint;
}