I would like to change (map) white color to blue color. How can I do this on ImageView in Android? I tried setColorFilter by PorterDuff / LightingColorFilter / ColorMatrixColorFilter but I can't figure out how to set it up. There is a transparent background around the image.
Try to use Background Tint color function in xml or java code.
or
I would suggest another way. Take on frame layout and place view and then image view.
white portion in image should be transparent use PNG image.
and you can give color to base view color which ever you want.
I have an png image with black color and set it to image view. And I have a color hex #EFA78E. Then, I convert hex string to colorInt by
int color = Color.parseColor(#EFA78E);
After that I set the color to imageview
imageView.setColorFilter(color);
And it show a transparent image. I already try with all mode of PorterDuff.Mode and it doesn't work.
You can use
imageView.setColorFilter(Color.parseColor("#EFA78E"), PorterDuff.Mode.SRC_IN);
imageView.setImageResource(yourImage);
If it dosn't work, you can also use the android:tint attribute in xml to acheive the same.
<ImageView
...
android:tint="#EFA78E"/>
I have an image in a resource file.
Drawable draw = getResources().getDrawable(R.drawable.my_icon);
The image has a transparent background.
Is there a way to programmatically set the background color to the Drawable before using the end product further in my code?
I think Drawing with PorterduffXferMode may help you in your case. This way you can merge two images (your image and a overlay completly in your color you want to replace the transparent pixels with) in many different ways.
Different porterduff modes explaned:
http://www.ibm.com/developerworks/java/library/j-mer0918/
Android example:
http://www.vogella.com/code/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html
This way you draw the result inside a new Bitmap. (SRC_OVER should work in your case if your image is the src and the background is used as the dst)
setColorFilter() with Porterduff SRC will break the transparent of drawable.
I used this in my code, and it work
disabledIcon = ContextCompat.getDrawable(getContext(), resId);
disabledIcon = DrawableCompat.wrap(disabledIcon);
disabledIcon.mutate(); // to not share its state with any other drawable
DrawableCompat.setTint(disabledIcon, ContextCompat.getColor(getContext(), R.color.button_text_disabled));
I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src
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