Android: no se puede dibujar un objeto de mapa de bits en una vista

I created a custom View and am trying to draw an array of ints (colors) onto it.

This draws a red screen:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int width = canvas.getWidth();
    int height = canvas.getHeight();

    int[] pixels = new int[width*height];
    for(int i=0; i<pixels.length; i++) {
        pixels[i] = 0xFF0000;
    }

    canvas.drawBitmap(pixels, 0, width, 0,0,width,height,false,null);
}

Esto no:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int width = canvas.getWidth();
    int height = canvas.getHeight();

    int[] pixels = new int[width*height];
    for(int i=0; i<pixels.length; i++) {
        pixels[i] = 0xFF0000;
    }

    Bitmap bmp = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
    canvas.drawBitmap(bmp, 0, 0, null);
}

The difference is in those last line(s). What's wrong with the second way of doing things? Am I creating the Bitmap object wrong somehow?

preguntado el 28 de agosto de 11 a las 03:08

1 Respuestas

In your second example try changing pixels[i] = 0xFF0000; a pixels[i] = 0xFFFF0000; you're creating a Bitmap with alpha channel so you have to specify your colors in AARRGGBB format to get them interpreted correctly.

Respondido 28 ago 11, 10:08

Ah, that's exactly it. I wasn't thinking about that alpha value because obviously when using the first method, I just passed 'false' into the hasAlpha parameter and it was fine. - Ricket

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.