Android SurfaceView Canvas displays wrong colors

Hi guys, so this is the first time in 7 years when I could not find the answer to the question about programming, and I was not close to what the problem could be in the first place.

Ok, let's start from the beginning. I followed some tutorials and examples on Android SurfaceView and how to draw on Canvas in a different thread. So far no problem, everything looks the way I expect. I am currently working on a scenario where I have an addition to my SurfaceView, which means that the background of the parent view (FrameLayout) is displayed around the aforementioned SurfaceView. Everything becomes interesting here, since I use the same color for the parent background as for cleaning the SurfaceView canvas.

The root (= parent) FrameLayout has its own background set in the subject, for example, <item name="android:windowBackground">@color/palette_primary_dark</item>

which is defined in colors.xml, for example, <color name="palette_primary_dark">#28252C</color>

and in my stream constructor I get the same color in the mClearColor global variable as mClearColor = ContextCompat.getColor(context, R.color.palette_primary_dark); where context is the context that I get from my SurfaceView.

The following code is a render loop that runs on my thread

 @Override public void run() { while (mShouldRun) { Canvas canvas = null; try { canvas = mSurfaceHolder.lockCanvas(); synchronized (mSurfaceHolder) { if (canvas != null) { canvas.drawColor(mClearColor); onDrawFrame(canvas); } } } finally { if (canvas != null) { mSurfaceHolder.unlockCanvasAndPost(canvas); } } } } 

So far so good. There are no errors and everything draws; the background is not black and it looks like the color I want. But here's the catch, this is NOT the color I want. This is from the hair. The color I defined in my XML is #28252C , but according to my GIMP color picker, it actually draws #292429 . Weird
Then I started looking further and found that the color that I use to draw the touch indicators (where my fingers are located on the screen) should be #FF1744 , but actually it is #FF1442 .

This left me scratching my head and desperately trying to use other ways to paint the right color on my canvas. I tried Canvas.drawColor() , Canvas.drawARGB() and Canvas.drawRGB() , but to no avail. In drawColor() I even tried different ways to get my color, for example mClearColor , Color.parseColor("#28252C") , Color.argb(255, 40, 37, 44) and so on. Nothing succeeded. Then I came across the fact that drawColor() uses PorterDuff.Mode.SRC_OVER by default, so I tried using PorterDuff.Mode.SRC to get a pure color, which I feed him, but not happy faces :(

Then I just started trying things, but nothing worked. When I tried setLayerType() in my SurfaceView, something changed. The following screenshot shows how it looked with the default value of LAYER_TYPE_NONE

LAYER_TYPE_NONE

Now when I change this to LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE , the same layout looks like this

LAYER_TYPE_SOFTWARE / HARDWARE

Now it’s strange that this is actually the right color and does what I want, it still does not draw lines and touch information. Calls to my onTouchListener are still made, which in turn should provide data for drawing lines on the screen. The latter does not work, but in fact it does not draw lines. I do not know what causes this strange problem that changes my colors ... Any help would be greatly appreciated :)

Edit # 1:

Today I did some more research and in accordance with this answer by Romain Gai and the next answer to the question by Adrian Lopez, setLayerType() actually does more harm than it solves the problems. Therefore, I have even less guidance on what may cause or solve my problem: / I will definitely edit the question when I find additional information and answer if I find a solution :)

+5
source share
1 answer

SurfaceView is configured to use RGB 565 by default.

In onCreate() set the color format of the surface to setFormat() to get RGB 8888 instead:

 mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT); 
+4
source

Source: https://habr.com/ru/post/1236064/


All Articles