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>