ANDROID: OpenGL White Screen Weirdness

For some reason the G1 was displaying a white screen in GLSurfaceView when the NexusOne was displaying images just fine.

Note that my GLSurfaceView is initialized to be ARGB 8888 with DEPTH BUFFER 16
Also, it is set with pixelformat.Translucent

Turns out, this is caused by texture loading, not the EGL. textures in GLES 1.0 needs to be in powers of 2! NexusOne solves that issue by using GLES 2.0, without letting the users know about it.

So, convert your bitmaps to powers of 2, before using the GLUtils method to create your textures!!!

ANDROID: Overriding OnMeasure

When generating a custom layout view, you will often need to override the onMeasure method. sometimes, you just want the width or height to 'fill_parent'... in this case we cant simply do:

setMeasuredDimension(fill_parent, fill_parent);

NOR can we pass in the width & height values received by onMeasure like so:

setMeasuredDimension(width, height);

INSTEAD, here's what you need to do:

super.onMeasure(w, h);
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());

This will get you the parent's width and height, assuming you've set its layout-params to be (w=fill_parent, h=fill_parent)

ANDROID: Alpha Masking

Lets say we had 2 bg images, and wanted to use an alpha mask (black = invisible pixels, transparent = visible pixels) to mask those 2 bg images composite together...


(1) create default paint
(2) set filter bitmap to false
(3) draw bg #1
(4) draw bg #2
(5) set paint's setXfermode to dstOut
(6) draw mask
(7) set paint's setXfermode to null

however, if you are putting this View object on top of another view, the mask will ALSO mask out the pixels in the PARENT VIEW... this is obviously not wanted behavior. to resolve this, you could simply render everything to a mutable-bitmap, and in onDraw, just draw that bitmap instead. See below:


inside public void onDraw(Canvas onDrawCanvas);

mutable = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
canvas = new Canvas(mutable);
paint = new Paint();
paint.setFilterBitmap(false);

canvas.drawBitmap(bg1, 0, 0, paint);
canvas.drawBitmap(bg2, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
canvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);

onDrawCanvas.drawBitmap(bitmap, 0, 0, paint);

ANDROID: Image Scaling

I was encountering a very bizarre error where images were all being displayed larger than actual pixelxpixel size.

Finally I figured out it was because anything placed in /drawable directory gets scaled automatically by the density value!

So the fix are as follows:

- Use drawable-ldpi, -mdpi, -hdpi directories for drawables instead!
- Use BitmapFactory.Options(); options.inScaled = false;
- Place any assets that should not be scaled in drawable-nodpi

ANDROID: Starting another Activity

From an activity...

Intent i = new Intent();
i.setClass(this, NewClass.class);
startActivity(i);

Where this is the main context...

ANDROID: Camera

When using the camera, you might see a weird error where in portrait, the camera preview shows 2 screens instead of one. This is because the camera orientation is LANDSCAPE by default. To fix this issue, you can do one of 2 things...

(1) Force your application to be in landscape by using the following in the manifest.xml



(2) Alter the camera parameters to account for the orientation differences:

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);

NOTE: If your application is not of fixed-orientation, you will have to detect the orientation of the application and adjust the 2nd parameter string accordingly between "portrait" and "landscape"