我已经实现了组件的绘图,如下图所示:
@Override
protected void onDraw(Canvas canvas) {
int w = canvas.getWidth();
int h = canvas.getHeight();
if (mFinalBitmap == null) {
mFinalBitmap = Bitmap.createBitmap(w, h,
Bitmap.Config.ARGB_8888);
}
if (mTempCanvas == null) {
mTempCanvas = new Canvas(mFinalBitmap);
}
if (mBackgroundBitmap == null) {
mBackgroundBitmap = createBitmap(R.drawable.rounded_background,
w, h);
}
if (mBackgroundImage == null) {
mBackgroundImage = createBitmap(R.drawable.image_for_background, w, h);
}
mTempCanvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
mTempCanvas.drawBitmap(mBackgroundImage, 0, 0, mPaint);
canvas.drawBitmap(mFinalBitmap, 0, 0, null);
}
private Bitmap createBitmap(int id, int width, int height) {
Bitmap bitmap = BitmapFactory.decodeResource(getContext()
.getResources(), id);
return Bitmap.createScaledBitmap(bitmap, width, height, false);
}
mPaint 在哪里
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
我想知道代码是否很好,或者可以针对相同的结果进行优化,它使用大量内存并且是OutOfMemoryError
.
谢谢。