0

I have MySurfaceView that extends SurfaceView. I draw a rectangle as background in MySurfaceView.

Paint p = new Paint();
p.setColor(0xfff3f3f3);
canvas.drawRect(0f, 0f, bgWidth, bgHeight, p);

fff3f3f3 is background_holo_light. I want the background color of MySurfaceView to be equal to the background color of my other UI components.

However, the background color of MySurfaceView is actually fff7f3f7 in Nexus 5. :(

How would I know this? I make a screenshot through DDMS, open GIMP, pick up color.

Then I try a few other values. Both go up and down.


UP:

p.setColor(0xfff3f3f3);

turns out to be f7f3f7. :(

p.setColor(0xfff4f4f4);

turns out to be f7f7f7. :(

p.setColor(0xfff6f6f6);

turns out to be f7f7f7. :(

p.setColor(0xfff7f7f7);

turns out to be f7f7f7.


DOWN:

p.setColor(0xfff0f0f0);

turns out to be f7f3f7. :(

p.setColor(0xffefefef);

turns out to be efefef.


Any ideas why?? I just simply want the color of background_holo_light or f3f3f3.


Edit 1 - problem related to screenshot?

My other UI component, e.g. RelativeLayout, have the attribute "android:background" to be background_holo_light (f3f3f3). These UI components show the correct color (f3f3f3) in the screenshot of Nexus 5. By the way, the screenshots are captured by DDMS, in PNG format.

In other words, I feel that the way Android draw RelativeLayout is different from the way Android draw rectangle in SurfaceView.

buzouxun
  • 23
  • 7
  • @FrankN.Stein The device is Nexus 5 as I said in my question. My other UI component, e.g. RelativeLayout, have the attribute "android:background" to be background_holo_light(f3f3f3). These UI components show the correct color in Nexus 5. I am wondering what bits Android.Graphics.Paint actually supports? – buzouxun Jul 09 '14 at 17:58

4 Answers4

0

If you want f3f3f3 you should simply put:

p.setColor(0xfff3f3f3);

In this page you can see the values:

http://encycolorpedia.com/f3f3f3

  • Yes I did that. However the color shown in Nexus 5 is actually f7f3f7. I explained that in my question. Thanks – buzouxun Jul 09 '14 at 17:55
  • @user3820896 Yes, sorry. Are you sure that you haven't applied any other attribute to your surfaceview? Maybe you could try to implement this (I don't know if your result will change): http://stackoverflow.com/a/12899457/2528167 –  Jul 09 '14 at 18:04
  • Getting a color by referencing from resource is equvilavent to the hard-coded integers. I didn't change attributes of my surfaceView. My guess is that this problem is related to Android.Graphics.Color and/or Android.Graphics.Paint. – buzouxun Jul 09 '14 at 18:19
  • @user3820896 Have you tried to run the app in another device emulator? If you obtain the same result you are doing something wrong in your code –  Jul 09 '14 at 18:38
  • Same for device emulator. I am looking into Android.Graphics API. Then I'll post what I find if I find. Thanks for your suggestion. – buzouxun Jul 09 '14 at 18:52
0

Try:

p.setColor(Color.parseColor("#fff3f3f3"))
wiz
  • 321
  • 2
  • 4
0

Your color have an alpha value(transparency). when you take a screenshot you save an image (jpg) without an alpha value.

That's mean that at your application you got the real color bat at screenshot you got different color.

wiz
  • 321
  • 2
  • 4
  • The screenshots are captured by DDMS and I believe they work fine. Please see the "edit 1" I just added on more details of screenshots. Thank you for your idea – buzouxun Jul 09 '14 at 20:33
  • When you see your view at the screen, and your view contain transparency value, the final view color depending on what you have under your view. exmple: color:#000000AA is 'Black' with tranparency. if the view under that color is white' you get grey, if the view under that color is black you get black. – wiz Jul 10 '14 at 07:26
  • True, but this does not apply to my problem. I am drawing on SurfaceView, so by default SurfaceView is a hole there. The background color of the ViewGroup/View that contains SurfaceView will not affect SurfaceView. One example on drawing on SurfaceView is [here](http://android-coding.blogspot.com/2011/05/drawing-on-surfaceview.html). – buzouxun Jul 10 '14 at 13:04
  • To be more clear, I did not run "setZOrderOnTop(true)" in my SurfaceView. "setZOrderOnTop(true)" will give a transparent background to SurfaceView, [e.g. discussion](http://stackoverflow.com/questions/9174712/android-surfaceview). By default, SurfaceView does not have a background, as I mentioned it's a hole. – buzouxun Jul 10 '14 at 13:08
  • In addition, I also tried "p.setColor(0xf3f3f3)" to draw rectangle but the result still is f7f3f7. To sum up, the alpha value seems not a problem here in my opinion. – buzouxun Jul 10 '14 at 13:08
0

setDither(true) solved my problem. Ref: http://developer.android.com/reference/android/graphics/Paint.html#setDither(boolean).

Thanks everyone in this post.

buzouxun
  • 23
  • 7