Set Transparent Background of an Imageview on Android

Set transparent background of an imageview on Android

In your XML set the Background attribute to any colour, White(#FFFFFF) shade or Black(#000000) shade. If you want transparency, just put 80 before the actual hash code:

#80000000

This will change any colour you want to a transparent one.. :)

How to make a background 20% transparent on Android

Make the color have 80% in the alpha channel. For example, for red use #CCFF0000:

<TextView
...
android:background="#CCFF0000" />

In the example, CC is the hexadecimal number for 255 * 0.8 = 204. Note that the first two hexadecimal digits are for the alpha channel. The format is #AARRGGBB, where AA is the alpha channel, RR is the red channel, GG is the green channel and BB is the blue channel.

I'm assuming that 20% transparent means 80% opaque. If you meant the other way, instead of CC use 33 which is the hexadecimal for 255 * 0.2 = 51.

In order to calculate the proper value for an alpha transparency value you can follow this procedure:

  1. Given a transparency percentage, for example 20%, you know the opaque percentage value is 80% (this is 100-20=80)
  2. The range for the alpha channel is 8 bits (2^8=256), meaning the range goes from 0 to 255.
  3. Project the opaque percentage into the alpha range, that is, multiply the range (255) by the percentage. In this example 255 * 0.8 = 204. Round to the nearest integer if needed.
  4. Convert the value obtained in 3., which is in base 10, to hexadecimal (base 16). You can use Google for this or any calculator. Using Google, type "204 to hexa" and it will give you the hexadecimal value. In this case it is 0xCC.
  5. Prepend the value obtained in 4. to the desired color. For example, for red, which is FF0000, you will have CCFF0000.

You can take a look at the Android documentation for colors.

How can I make an image transparent on Android?

Try this:

ImageView myImage = (ImageView) findViewById(R.id.myImage);
myImage.setAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.

Note: setAlpha(int) is deprecated in favor of setAlpha(float) where 0 is fully transparent and 1 is fully opaque. Use it like: myImage.setAlpha(0.5f)

ANDROID - transparent background for imageview

Ok thanks for your answears but I have managed to solve my problem:
I gave a random color to the background of my layout (for example #FF34035544) and I changed the two first letters to 00 which made #0034035544.
It was finally very simple !

how i i can transparent background on android studio?

You can try like this if you want to adjust the transparent change the alpha in color I gave as #55000000 you can change the alpha value(55) whatever you want. if you wish black transparency you can use #55FFFFFF

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#55000000"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/ic_launcher"
/>

</RelativeLayout>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/ic_launcher"
/>

</RelativeLayout>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/ic_launcher"
/>

</RelativeLayout>
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/ic_launcher"
/>

</RelativeLayout>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/ic_launcher"
/>

</RelativeLayout>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/ic_launcher"
/>

</RelativeLayout>
</LinearLayout>
</LinearLayout>

</LinearLayout>

ImageView with Non-Transparent Background Image

Here is your problem solution.

  1. make your background image transparent like this.
    Sample Image

  2. set your main image as FrameLayout background image.

  3. and this background is ImageView background image.

    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:background="@drawable/randominage">

    <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rounded_background"
    android:scaleType="fitXY" />
    </FrameLayout>

Transparent View over ImageView

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".



Related Topics



Leave a reply



Submit