Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SurfaceHolder.setFormat(PixelFormat.RGBA_8888) fails on some devices and not others

I have a game app with the following Views structure. First I have an empty FrameLayout like so:

FrameLayout game_frame_layout = new FrameLayout(getApplicationContext());

Then I add two views to it like so:

game_frame_layout.addView(customView);
game_frame_layout.addView(butView);

The customView is for displaying all sorts of moving game graphics while the butView displays some ImageButtons on top of the moving grapics. The customView is an instance of a class CustomView which extends SurfaceView.

CustomView includes the following code

    void updateView() 
    {
        final SurfaceHolder holder = getHolder();
        holder.setFormat(PixelFormat.RGBA_8888);

        try 
        {
                Canvas canvas = holder.lockCanvas();
                if (canvas != null) 
                {
                    onDraw(canvas);

                    holder.unlockCanvasAndPost(canvas);
                }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

The line holder.setFormat(PixelFormat.RGBA_8888); is a recent addition (see here). Without that line, my animated graphics appear to be in a format with too few colours (by experiment I deduced it was "RGB_565"), so I get some banding artifacts. When I added the setFormat line, the graphics appear perfectly (without banding) on my Samsung Galaxy Tab 10.1 (Android 3.1)... but on three other devices: a Samsung GT-l9100 (4.1.2), a Nexus 7 ME370T 4.4.2 and a HTC One X 4.2.2 I only see the buttons corresponding to butView against an entirely black background. There is no indication in the logs that the program has crashed.

Any ideas?

like image 867
Mick Avatar asked Jan 13 '14 13:01

Mick


3 Answers

Probably not all android devices support a 32/24 bit pixel format so that's why 565 is always working while 888 might fail. You might try to understand if it's a problem with the hardware acceleration trying to disable it

like image 123
Pasquale Anatriello Avatar answered Nov 16 '22 11:11

Pasquale Anatriello


What happens when you change the order you add them to the game_frame_layout? First the butView and then the customView:

game_frame_layout.addView(butView);
game_frame_layout.addView(customView);

I assume the butView won't be visible, but i'm curious whether the customView renders its content properly.

like image 2
Delblanco Avatar answered Nov 16 '22 10:11

Delblanco


Your application doesn't seems to be working post ICS. ICS saw major revisions in the graphics department, the most significant being introduction of TextureView which is designed to address the shortcomings of SurfaceView. Try extending TextureView instead of SurfaceView for your CustomView class. For more info visit Android 4.0 Graphics and Animation

like image 1
Shakti Avatar answered Nov 16 '22 11:11

Shakti