0

I'm drawing a rect in the onDraw() of a custom view class. I want the color of the rect to be transparent (eg 50%) so that the background shines through.

Here's my layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background"  >

    <MyCustomView
        android:id="@+id/myCustomView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

The background is set in the LinearLayout as you see.

colors.xml:

<color name="my_tranparent_color">#77FFFFFF</color>

somewhere in MyCustomView's onDraw() method:

Paint p = new Paint();
p.setColor(getResources().getColor(R.color.my_tranparent_color));
canvas.drawRect(new Rect(x, y, x + 20, y + 20), p);

The result isn't transparent, it's only kind of gray.

I get simular results if I set the alpha value inside the onDraw() method:

p.setAlpha(51);

and

<color name="my_tranparent_color">#FFF</color>
Armin
  • 351
  • 1
  • 4
  • 29
  • Are you sure? `#8fff` is semi-transparent white. `#8000` is semi-transparent black. – Phantômaxx May 14 '15 at 11:17
  • #77FFFFFF - This must work....try changing the alpha value in it from 77 to much lower one (for eg 22) in order to test – Narendra Singh May 14 '15 at 11:18
  • 1
    You can refer below link for get transparency. http://stackoverflow.com/a/17239853/3374189 – Hasmukh Barochiya May 14 '15 at 11:19
  • @DroidWormNarendra already tried that. It's like the alpha cahnel is used for grayscaling in my case... – Armin May 14 '15 at 11:20
  • You are **wrong**. For **sure**. – Phantômaxx May 14 '15 at 11:21
  • 2
    For 50% transparency, in your xml, try adding this attribute to your MyCustomView: `android:alpha="0.5"` – Prince May 14 '15 at 11:30
  • @Prince this actually works. But it sets the alpha channel for my whole view. I only want some elements of it to be transparent. – Armin May 14 '15 at 11:36
  • In that case, you can apply it directly to only those children views inside the `MyCustomView` that you want to be semi-transparent. You can also set it in code for your views like `yourToBeSemiTransparentView.setAlpha(floatValue);` – Prince May 14 '15 at 11:41
  • @Prince inside the class? I've tried that earlier as you see in my question `p.setAlpha(51);` – Armin May 14 '15 at 11:46
  • Apply it only to your _children Views_ that you mentioned are a few elements inside the _MyCustomView_ class rather than applying to _MyCustomView_ class as a whole. Also, #80FFFFFF will give you 50% transparency on White color. – Prince May 14 '15 at 11:51
  • @Prince those elements are rects that I draw inside the `MyCustomView` class. Can you give me an concrete example for that? – Armin May 14 '15 at 11:55
  • 1
    I see, in that case try setting a paintStyle: Some sample code- Paint p = new Paint(); p.setColor(Color.parseColor("#FFFFFF")); p.setAlpha(50); p.setStyle(Paint.Style.STROKE); RectF draw = new RectF(); draw.set(100, 100, 100, 100); canvas.drawRect(draw, p); – Prince May 14 '15 at 12:06

1 Answers1

0

Stupid me! The problem was that I was drawing black rect behind the white one. I didn't remember that it was filled with black, because I used it to get a border for the white rect. So the background for the white rect was a black rect, which explains the gray result. Thanks for your help :/

Armin
  • 351
  • 1
  • 4
  • 29