Change image fill color in Android - android

I have an ImageView with the source being an ImageAsset.
My image is a circle with a plus and I am looking to colour the inside of the circle only. How do I do that?
Using setBackgroundColor colours the whole of the view thus giving a square background like this:
.

You can apply a color tint to your ImageView. This will not affect your background if it's transparent, just the colored part (which is the circle and the plus)
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR),
android.graphics.PorterDuff.Mode.MULTIPLY);
Update
Using android.graphics.PorterDuff.Mode.SRC_IN will also work (if you don't need to multiply the source and destination pixels)
Update 2
Since the destination part is not colored than the best solution is to use VectorDrawable.
You can use VectorChildFinder to find inner parts of your SVG resource and change its color.
VectorChildFinder vector = new VectorChildFinder(this, R.drawable.my_vector, imageView);
VectorDrawableCompat.VFullPath path1 = vector.findPathByName("path1");
path1.setFillColor(Color.RED);
imageView.invalidate();
to create your SVG, follow these steps :
Just click right button on folder(drawable for ex.) and choose:
then choose:

Related

How to replace color by another on ImageView in Android?

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.

How to make a Blur background depends on the Image color that you are loading in Android

is there any systemical possible way (i mean in code) to make a blur background depends on the image user open, the color must be similar to the image that will open.
for example, the background on this page is grey.
You will need to get the dominant color from the image you are using then create a gradient drawable between a starting color and your dominant color. There are multiple ways to find dominant colors which you can read up on here: Finding the dominant color of an image in an Android #drawable
From there you create a drawable and set the background of your view to that drawable:
// get the drawable from the image view
// could also be pulled from resources if available
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
int color = getDominantColor(bm);
GradientDrawable gradient = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFFF,color});
gradient.setCornerRadius(15f);
contentView.setBackground(gradient);
See this library to make blur image https://github.com/wasabeef/Blurry

How to modify the color of a 9patch image Android ?

I don't really understand how to create a 9patch image, but I found an image which is working on my fragment. The problem is that the color outside the border is not the color of the background. I tried changing the color of the pixels from the image to the color of the background but the resulting image doesn't work anymore.
This is the image which is working but has the wrong color:
http://i.stack.imgur.com/cJBfV.png
How can I change the color of the pixels that are outside the border, or how can I create a new 9patch image that looks like that ?
You can use DrawableCompat with supprot v4.
The next code shows how you can change Toast color.
As you know, toast background is a 9patch Drawable named toast_frame.9.png (you can find it in your sdk dir).
Toast toast = Toast.makeText(this, content, Toast.LENGTH_SHORT);
toastView.findViewById(android.R.id.message);
View toastView = toast.getView();
Drawable toastBg = toastView.getBackground();
Drawable drawable = tintDrawable(toastBg, ColorStateList.valueOf(Color.RED));
toastView.setBackground(drawable);
toast.setView(toastView);
toast.show();
public Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrappedDrawable, colors);
return wrappedDrawable;
}
If you like to learn more, click this:
http://www.race604.com/tint-drawable
You can edit the 9 patch using the Draw 9-patch tool that is provided with the Android SDK. However as your image already includes the 9 patch stretchable area you can just edit the colors in an image editor, such as GIMP or Photoshop. Ensure you rename your image to use the .9.png extension to allow it to be recognized as a 9 patch image.
http://developer.android.com/tools/help/draw9patch.html
Nine-patch are normal png files. They are just interpreted differently by any software able to display nine-patch. In other words, you can edit them with any png editor.
There is also some dedicated tools (for instance: in android sdk /tools/draw9patch) that display the result in different sizes.
How are they interpreted then ?
The first and last rows + the first and last columns of pixels in a nine-patch image contains only black or transparent pixels (and those first/last rows/columns aren't displayed).
The (intersection of) black pixels on left and top edges defines stretchable areas. (you can have more than one)
The (intersection of) black pixels on right and bottom edges define the content area. (you can only have one)

Programmatically set the Background color of a Drawable in Android?

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));

Replace TRANSPARENT part of image set in imageview with another image?

How to change TRANSPARENT part of image set in imageview with another image?
Below is the main image, there is TRANSPARENT portion(here looks white), i want to set another image withing that portion of image.
any idea how to do it?
Question:
How to find TRANSPARENT portion starting point LEFT(x,y), RIGHT (x,y), BOTTOM LEFT (x,y), BOTTOM RIGHT(x,y) ? for image replacement.
How to process bitmap in runtime to add another image to make changes in imageview?
I've tried this to find transparent part of image.
You have a bitmap (B1) and there is only one rectangle transparent zone somewhere. And you want to place another bitmap (B2) inside it.
use monte-carlo method to find any transparent pixel on B1. You know
it's coordinates now.
go [left/right/top/bottom] from transparent pixel and find
first solid pixel. Now you know transparent rectangle coorditates.
There are several ways to put something inside transparent area. You can:
place second imageview (with B2) under the first one (with B1). Set B2 padding inside imageview accordingly transparent zone coordinates.
create new image from B1 and B2 and set it to imageview.
do it some other way...
try this example in this crop image with transparent part it will use full for you.
https://github.com/ketanpatel25/Image-Cropping-In-Transparent-Area

Categories

Resources