How to change the opacity of an image. - android

I have an background image set to an ImageView, Now i want to change the opacity of an image without, for that i write this code to change the opacity of ImageView, but when i do so it remove the background image from the image view, So my question is how can i change the opacity of ImageView without removing background image form it.
Code i used is:
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setBackgroundResource(R.drawable.theme1_page_header); // Set background image
int opacity = 100; // from 0 to 255
imageView.setBackgroundColor(opacity * 0x1000000); // change opacity of image

the most important part of alpha is that the value has to be decimal
0 = transparent and 1 = visible
so 0.5 is half way visible
in the XML you can do
<ImageView
android:layout_width="30dp"
android:layout_height="35dp"
android:id="#+id/imageView"
android:alpha="0.4" // <----------------- this is the fun part
android:layout_alignParentRight="false"
android:background="#drawable/imagename"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:layout_marginLeft="100dp"
android:layout_alignParentBottom="false"
android:layout_alignParentStart="false"
android:layout_alignTop="#+id/bar"
android:layout_marginTop="30dp"/>

You can use
imageView.setAlpha(yourValue); // some value 0-255 where 0 is fully transparent and 255 is fully opaque
See the documentation

ImageView imageView = (ImageView) findViewById(R.id.image_view);
Drawable dPage_header= getResources().getDrawable(R.drawable.theme1_page_header);
// setting the opacity (alpha)
dPage_header.setAlpha(10);
// setting the images on the ImageViews
imageView.setImageDrawable(dPage_header);

For Api >=16 . Use setImageAlpha as your practice because setAlpha will be deprecated in coming future. `
ImageView.setAlpha(int) has been renamed to ImageView.setImageAlpha(int) to avoid confusion. See Detail Explanation here

Use this option in layout xml for opacity as well as to turn the Android Material icons from Black to Grey.
android:alpha=0.5

In Kotlin we can change alpha in code like this:
myImageView.alpha = 0.5f
Change alpha value to whatever you want between 0.0f to 1f

Option 1
Use imageView.setAlpha(100).
If you're using Android 2.3 you have to do a horrible hack with a zero-length animation using nineolddroids.
Option 2
Subclass ImageView and override its onDraw() method to draw the image transparently.
Option 3
Actually modify the image pixels using get/setPixel(). This'll be very slow though; there's probably a faster way to do this (e.g. using renderscript).

Related

How to control background image transparency in android app?

I use this tag to set background
android:background="#drawable/image_name"
how to make that?
There are two ways you can go about changing the transparency of your background image. The first is by modifying the XML layout and the second is programmically.
You can do it in XML by using the code:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image_name"
android:alpha=".75"/>
Where android:alpha="" is your transparency with 1 as fully solid and 0 is completely transparent. More information is at the Android documentation.
(This will also work with the background of your view if that's what you want)
You can also change the transparency of it programmically (assuming you are using an ImageView).
To do this you can use the following code for the background of your view
int transparency = 100;
mView.getBackground().setAlpha(transparency);
The transparency runs from 0 to 225 where 0 is transparent and 225 is solid.
int transparency = 51 //0-255 (51 is 20% transparent)
nameOfYourView.getBackground().setAlpha(transparency);

Android: 9-patch image not stretching as expected

The actual image is this:
the red boxex are where i made 1 px black likes. Even the preview shows fine. So no problem with 9-patch
but the image i get for the following layout is:
<ImageView
android:id="#+id/image_landing"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/loginheader"
android:contentDescription="#string/login_header"
/>
I Expected, the logo to be on the left and black dots on the right and the rest of the space between them is filled with grey color i selected on the top
Thank You
Is your ImageView really bigger than your 9patch?
If not, you need to change scaleType as defaut is FIT_CENTER.
Use android:scaleType="fitXY" or android:adjustViewBounds="true".
It looks like your 9 patch is larger than the ImageView that you are trying to put it in. Try setting the ImageView to wrap_content to see if it fixes the problem. If it does, try making the 9 patch smaller, the ImageView bigger or set the scaleType as pcans mentioned.

Button + image - any way to specify width/height of image?

I defined a button with an image:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:drawableTop="#drawable/foo" />
Is there a way to specify the width and height, in dip, of the drawable being used? The rules for how the drawable scales aren't clear to me.
Thanks
Try using ImageButton instead of Button. In ImageButton you can use android:scaleType tag with value as "fitXY" or other, as per your need to keep a check on scaling of image.
Udayan is correct and also make sure you put image in button background, image may stretch,
better put in src. you can avoid scaling of image.

How can I make an image transparent on Android?

I am using a linear layout and frame layout. In the linear layout I keep an image as background and in the frame layout I keep an imageView. In that imageView I give an image.
Now I want to make the second image (that is in the imageView) transparent. How can I do this?
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:alpha does this in XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/blah"
android:alpha=".75"/>
Set an id attribute on the ImageView:
<ImageView android:id="#+id/myImage"
In your code where you wish to hide the image, you'll need the following code.
First, you'll need a reference to the ImageView:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
Then, set Visibility to GONE:
myImage.setVisibility(View.GONE);
If you want to have code elsewhere that makes it visible again, just set it to Visible the same way:
myImage.setVisibility(View.VISIBLE);
If you mean "fully transparent", the above code works. If you mean "partially transparent", use the following method:
int alphaAmount = 128; // Some value 0-255 where 0 is fully transparent and 255 is fully opaque
myImage.setAlpha(alphaAmount);
If you are in an XML file, use the following to make your imageview transparent!
android:background="#null"
On newer versions of Android (post Android 4.2 (Jelly Bean) at least), the setAlpha(int value) method is depreciated. Instead, use the setAlpha(float value) method that takes a float between 0 and 1 where 0 is complete transparency and 1 is no transparency.
Set transparency using setAlpha(float alpha). The below code works for me were I used an alpha value in float, 0 - 1.
0: Full Transparent
0.5 - 50%: Transparent
1: Full Opaque
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
imageView.setAlpha(.80f);
As setAlpha int has been deprecated, setImageAlpha (int) can be used
ImageView img = (ImageView) findViewById(R.id.img_image);
img.setImageAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
The method setAlpha(int) from the type ImageView is deprecated.
Instead of
image.setImageAlpha(127);
//value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
In XML, use:
android:background="#android:color/transparent"
Image alpha sets just opacity to ImageView which makes Image blurry, try adding tint attribute in ImageView
android:tint="#66000000"
It can also be done programatically :
imageView.setColorFilter(R.color.transparent);
where you need to define transparent color in your colors.xml
<color name="transparent">#66000000</color>
For 20% transparency, this worked for me:
Button bu = (Button)findViewById(R.id.button1);
bu.getBackground().setAlpha(204);
Use:
ImageView image = (ImageView) findViewById(R.id.image);
image.setAlpha(150); // Value: [0-255]. Where 0 is fully transparent
// and 255 is fully opaque. Set the value according
// to your choice, and you can also use seekbar to
// maintain the transparency.
You can easily do this in your XML file by this code:
android:alpha="0.85"
It will make your image 15% transparent. 0 is fully transparent and 1 is fully opaque

Opacity on a background Drawable image in View (using XML Layout)

I was just wondering if there was a way to change the opacity of the background image for a View (ie. TextView, etc.).
I know that I can set the background image like this:
android:background="#drawable/my_drawable_image"
Or I can set a specific background colour with an alpha setting like this:
android:background="#10f7f7f7"
Is there a way I can control the opacity (set the alpha) if I'm setting the background as a drawable image? And I want to do this in the XML Layout. I already know that I could grab the Drawable object and programmatically set the alpha, but I want to see if I can do it in the layout.
I ended up just going with the programmatical solution, since it doesn't look like it can be done via the XML layouts.
Drawable rightArrow = getResources().getDrawable(R.drawable.green_arrow_right_small);
// setting the opacity (alpha)
rightArrow.setAlpha(10);
// setting the images on the ImageViews
rightImage.setImageDrawable(rightArrow);
This might make your Work simpler
View backgroundimage = findViewById(R.id.background);
Drawable background = backgroundimage.getBackground();
background.setAlpha(80);
Alpha Values 0-255, 0 means fully transparent, and 255 means fully opaque
from: This Answer
You can also use XML to change the transparency:
android:alpha = "0.7"
The value of alpha ranges from 0 to 1
You can embed the image in xml, so you'll be able to see it in the Graphical Layout
<LinearLayout
style="#style/LoginFormContainer"
android:id="#+id/login_layout"
android:orientation="vertical"
android:background="#drawable/signuphead">
And change the code like this to make it transparent:
Drawable loginActivityBackground = findViewById(R.id.login_layout).getBackground();
loginActivityBackground.setAlpha(127);
The answer you gave didn't exactly answer the question you asked. Here's what I did.
Drawable login_activity_top_background = getResources().getDrawable(R.drawable.login_activity_top_background);
login_activity_top_background.setAlpha(127);
LinearLayout login_activity_top = (LinearLayout) findViewById(R.id.login_activity_top);
login_activity_top.setBackgroundDrawable(login_activity_top_background);

Categories

Resources