I'm experiencing some weird custom view behavior which I can't explain or fix.
I have a custom view, I'm using a Bitmap
to buffer the content for the onDraw()
call.
When I set the buffer image width to 2000 everything works, but when I increase it to 4000 the buffering process seems to work fine, but when the onDraw()
tries to draw it draws nothing. Either the Bitmap
is now being drawn, or the Canvas.drawBitmap()
doesn't really draw it.
This is not a shortage of memory issue.
Here are the core functions:
public class CustomView extends View {
int viewWidth;
int viewHeight;
Bitmap bmp;
int bmpWidth = 2000; // if I increase this nothing works
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
viewHeight = (int) MeasureSpec.getSize(heightMeasureSpec);
if(viewHeight > 0) {
if(bmp == null) {
try {
bmp = Bitmap.createBitmap(bmpWidth+extraWidth, viewHeight , bitmapCfg);
} catch(OutOfMemoryError E){
System.gc();
outOfMemory = true;
return;
}
viewWidth = bmpWidth + extraWidth;
createCanvas();
}
}
setMeasuredDimension((int)viewWidth,(int)viewHeight);
}
public void createCanvas() {
int pos = 0;
Canvas canvas = new Canvas(bmp);
canvas.drawRect(0, 0, bmpWidth, viewHeight, whiteColor);
canvas.drawText("TEST",pos,140,textColor);
pos += textColor.measureText("TEST");
this.viewWidth = (int) pos;
setMeasuredDimension(viewWidth,viewHeight);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(outOfMemory) {
return;
}
if(bmp == null) {
return;
}
canvas.drawBitmap(bmp, 0,0, paint); // I think this failes to draw.
}
}