1

I work on simple app with animation. I use standard View. In onDraw metod my app paints background (stored as a Bitmap) and a couple of sprites. In the main loop I need to modify the background bitmap (code attached). I use for that setPixel() metod. From time to time when I try to change bitmap I receive in the LogCat such message: "Cannot generate texture from the bitmap". After that bacground still appears on the screen but I can't use neither getPixel() nor setPixel() methods. I found some hint that it is because bitmap was recycled but I added some controls in my app and after this "crash" bitmap.isRecycled() is still false and bitmap.isMutable() is still true. So, I have no idea what's the problem.

public void drawOnBmp (float xF, float yF, float r) {       
    float[][] kolo;
    float xP, yP;
    float xStart, yStart;

    xStart = xF - r / 2;
    yStart = yF - r / 2;
    kolo = new float[(int)r + 1][(int)r + 1];
    for (int i = 0; i < 360; i++) {
        xP = r / 2 + (float)(r / 2 * Math.cos(2 * Math.PI * (double)i / 360));
        yP = r / 2 + (float)(r / 2 * Math.sin(2 * Math.PI * (double)i / 360));
        kolo[(int)xP][(int)yP] = 1;
    }
    boolean start;
    for (int i = 0; i < (int)r + 1; i++) {
        start = false;
        for (int j = 0; j < (int)r + 1; j++) {
            if (kolo[i][j] == 1)
                if (start == false) start = true;
                else if (j >= r / 2) break;
            if (start == true) kolo[i][j] = 1;
        }
    }
    // obraz is a bitmap with background
    for (int i = 0; i < (int)r; i++) {
        for (int j = 0; j < (int)r; j++) {
            if(kolo[i][j] == 1 && (int)xStart + i >= 0 && (int)yStart + j >= 0 && 
               (int)xStart + i < obraz.getWidth() && (int)yStart + j < obraz.getHeight())
                obraz.setPixel((int)xStart + i, (int)yStart + j, 0);
        }
    }
}

}

Qbek
  • 11
  • 2

1 Answers1

0

I run into the same problem. Didn't solve it yet but this may be usefull for anybody.

Some hints are coming from google.group's thread

What's interesting is that it seems to be related to the Hardware rendering introduced with Android 3.x. Setting the layer type to software for the respective View that does the drawing of the bitmap, fixes the issue.

So setting

view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

really worked for me, but in my app it impacted performance so I have to useView.LAYER_TYPE_HARDWARE

I'm still looking for the solution and roots of the issue. Will update if found any. Also any additional information appreciated.

vir us
  • 9,920
  • 6
  • 57
  • 66