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:

  • How to send out pointer event in Android
  • Sending HTTP Post Request with Android
  • Is it possible to make CursorAdapter be set in recycleview, just like ListView?
  • How to center the content inside a linear layout?
  • Update tabs from a tab's activity in a Android TabHost
  • Make Android app show up as designed for tablets
  • 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?

    Related posts:

    Android heap size on different phones/devices and OS versions
    How to move main content with Drawer Layout left side
    Conditional won't trigger when given a 75: android
    How to make AdView “occupy” space even while requesting an ad? (Android)
    How to reconcile difference in aspect ratio between camera preview size and picture size
    Error:Execution failed for task ':app:mergeDebugResources'. > Some file cru...
  • Gujarati Font Rendering
  • Running an Android App in Eclipse Causes NullPointerException
  • How to control media volume?
  • Error:Execution failed for task ':app:processDebugResources'. > at index 4
  • EditText view with keyboard number only
  • ADB is not working and failed to initialize the monitor thread error occoured in the ddms in android
  • 5 Solutions collect form web for “SurfaceHolder.setFormat(PixelFormat.RGBA_8888) fails on some devices and not others”

    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

    Set the pixel format before the setContentView() on the activity:

    getWindow().setFormat(PixelFormat.RGBA_8888);
    

    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.

    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

    I’m curious if you set butView‘s background to transparent.

    butView.setBackgroundColor(Color.TRANSPARENT);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.