Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent View over ImageView

I'm blocked there. I'm trying to put a transparent View over a background. I've tried several methods.

throught XML with:

android:background="@color/transparent"

or

android:color="#80000000"  

or putting a reference to color.xml file as so

<resources>
    <color name="transp">#80000000</color>
</resources>

with my layout.xml like this

android:background="@color/transp"

I've also tried to do it by generated code

myView.getBackground().setAlpha(45);

or

myViewm.setBackgroundResource(R.color.trans);

I've seen some posts related, but none of the answers worked.

Besides which is even stranger is that all of these solutions seems to wrok fine on the GraphicalLayout in Eclipse. But when I launch my device, the screen remains not transparent.I've drawn a line on that view to make sure that something happens; and the line does show.

here is my layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

      <ImageView
        android:id="@+id/backgroundview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/space_bg"
        android:contentDescription="@string/desc" />

      <View 
        android:id="@+id/tileview"        
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/><!-- 
        android:background="@color/transp"/>-->

</RelativeLayout>

and my code

private ImageView bg;
    MyView tV;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        tV = new MyView(this);

setContentView(tV);

}

and the myView onDraw

@Override
    protected void onDraw(Canvas canvas)    
    {

        super.onDraw(canvas);
        this.setBackgroundResource(R.color.transp);
        canvas.drawLine(10,20,30,40, paint);

    }

So, where am I wrong? Thanks!!!

like image 506
Antoine Avatar asked Aug 19 '13 18:08

Antoine


People also ask

How do I make a transparent view?

setAlpha(45); You may not be using this correctly. id="@+id/tileview" will be transparent and over id="@+id/backgroundview" .

Which method from the ImageView class allows you to adjust the transparency of an image?

ImageView myImage = (ImageView) findViewById(R. id. myImage); myImage. setAlpha(127); //value: [0-255].

What is transparent color in Android?

TRANSPARENT (which represents the same thing as @android:color/transparent ) is equal to 0 . The hex representation of 0 is #00000000 , which means that Color. TRANSPARENT is essentially a completely transparent Color.


1 Answers

android:background="@color/transparent"

You can use the transparent color provided in android resources:

android:background="@android:color/transparent"

android:color="#80000000"  

<resources>
    <color name="transp">#80000000</color>
</resources>

myViewm.setBackgroundResource(R.color.trans);

This will give you a very dark shade of gray. Alpha value of 80 is translucent at best.


myView.getBackground().setAlpha(45);

You may not be using this correctly.


private ImageView bg;
MyView tV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

    tV = new MyView(this);

    setContentView(tV);
}

This basically replaces the View inflated from R.layout.activity_main(which contains the ImageView and other widgets) with MyView. I don't think this is quite what you want. Try the following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/backgroundview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/space_bg"
        android:contentDescription="@string/desc" />

    <RelativeLayout 
        android:id="@+id/tileview"        
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/some_drawable_smaller_than_screen_size"
            android:layout_centerInParent="true" />

    </RelativeLayout>

</RelativeLayout>

How to inflate this xml:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);
}

id="@+id/tileview" will be transparent and over id="@+id/backgroundview".

like image 52
Vikram Avatar answered Jan 04 '23 09:01

Vikram