Question

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.
}

}

Was it helpful?

Solution

Try turning off the hardware acceleration. You can find info about hardware acceleration here: http://developer.android.com/guide/topics/graphics/hardware-accel.html

When the hardware acceleration is turned on each bitmap is loaded into an OpenGL Texture before being drawn on the screen. Each device has a maximum permittable texture size, which you are exceeding. This solution might not work on some devices. So the safest way would be dividing bitmaps larger than 2048x2048 into smaller pieces and loading them into seprate smaller bitmaps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top