How to change Bitmap image color in android?
I got kind of solution.
Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath);
float[] colorTransform = {
0, 1f, 0, 0, 0,
0, 0, 0f, 0, 0,
0, 0, 0, 0f, 0,
0, 0, 0, 1f, 0};
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0f); //Remove Colour
colorMatrix.set(colorTransform); //Apply the Red
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Display display = getWindowManager().getDefaultDisplay();
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));
image.setImageBitmap(resultBitmap);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, paint);
How to Change the Color of bitmap and retaining shades in bitmap?
Try the bellow link
How to change Bitmap image color in android?
Or
http://www.codeproject.com/Articles/17162/Fast-Color-Depth-Change-for-Bitmaps
OR
how to change the color of certain pixels in bitmap android
This may help You.
Android change color of ImageView / Bitmap
This is how I solved this issue :
- Declare an
ImageView
withsrc="@drawable/button"
- Create a
Drawable
and setColorFilter
to it and after that use it as src to your declaredImageView
like this :
>
Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLUE, Color.BLUE );
myIcon.setColorFilter(filter);
color.setImageDrawable(myIcon);
change color of bitmap
You just need to extract alpha and re-apply it after transformation. And use ARGB_8888;
Edited your code to include alpha:
public Bitmap colorize(Bitmap srcBmp, int dstColor) {
int width = srcBmp.getWidth();
int height = srcBmp.getHeight();
float srcHSV[] = new float[3];
float dstHSV[] = new float[3];
Bitmap dstBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
int pixel = srcBmp.getPixel(col, row);
int alpha = Color.alpha(pixel);
Color.colorToHSV(pixel, srcHSV);
Color.colorToHSV(dstColor, dstHSV);
// If it area to be painted set only value of original image
dstHSV[2] = srcHSV[2]; // value
dstBitmap.setPixel(col, row, Color.HSVToColor(alpha, dstHSV));
}
}
return dstBitmap;
}
Replace black color in bitmap with red
Get all the pixels in the bitmap using this:
int [] allpixels = new int [myBitmap.getHeight() * myBitmap.getWidth()];
myBitmap.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
for(int i = 0; i < allpixels.length; i++)
{
if(allpixels[i] == Color.BLACK)
{
allpixels[i] = Color.RED;
}
}
myBitmap.setPixels(allpixels,0,myBitmap.getWidth(),0, 0, myBitmap.getWidth(),myBitmap.getHeight());
Change the bitmap color
I have used this method to change the color of the bitmap runtime. Give it a try.
public Bitmap replaceColor(Bitmap src,int fromColor, int targetColor) {
if(src == null) {
return null;
}
// Source image size
int width = src.getWidth();
int height = src.getHeight();
int[] pixels = new int[width * height];
//get pixels
src.getPixels(pixels, 0, width, 0, 0, width, height);
for(int x = 0; x < pixels.length; ++x) {
pixels[x] = (pixels[x] == fromColor) ? targetColor : pixels[x];
}
// create result bitmap output
Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
//set pixels
result.setPixels(pixels, 0, width, 0, 0, width, height);
return result;
}
}
Define from and to color in the colors.xml
<color name="red">#FB0000</color>
<color name="yell">#FFC953</color>
and use the above method as below.
iv.setImageBitmap(replaceColor(bitmap,getResources().getColor(R.color.red),getResources().getColor(R.color.yell)));
How to tint a Bitmap to a solid color
I solved this by using a PorterDuffColorFilter
Paint paint = new Paint();
paint.setColorFilter(new PorterDuffColorFilter(targetColor, PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(resource, matrix, paint);
Change Bitmap Background Color
The Problem get Resolved by changing Imageview Color by Getting the Pixel Value, and Only Changing the Pixels that's color is rather than the white..
this Get Achieved by this Method.
var bitmap = (imageviewgradient.drawable as BitmapDrawable).bitmap
var newBitmap = replaceColor(bitmap, color)
The Above Code is Converting Image to Bitmap
private fun replaceColor(src: Bitmap?, targetColor: Int): Bitmap? {
if (src == null) {
return null
}
// Source image size
val width = src.width
val height = src.height
val pixels = IntArray(width * height)
//get pixels
src.getPixels(pixels, 0, width, 0, 0, width, height)
for (x in pixels.indices) {
if(pixels[x]!=-1) {
pixels[x] = targetColor
}
}
// create result bitmap output
val result = Bitmap.createBitmap(width, height, src.config)
//set pixels
result.setPixels(pixels, 0, width, 0, 0, width, height)
return result
}
The Main Working is Just Done by a Simple Condition i.e
if(pixels[x]!=-1) {
pixels[x] = targetColor
}
Related Topics
Using Android Intent.Action_Send for Sending Email
How to Get the Position Selected in a Recyclerview
Popupwindow $Badtokenexception: Unable to Add Window -- Token Null Is Not Valid
Sharing a Png Image in Drawable Folder
How to Detect Power Connected State
How to Delete SQLite Database from Android Programmatically
Custom Seekbar (Thumb Size, Color and Background)
Google Maps API V2 Supportmapfragment Inside Scrollview - Users Cannot Scroll the Map Vertically
Textinputlayout Not Showing Edittext Hint Before User Focus on It
How to Programmatically Enable/Disable Accessibility Service in Android
Viewpager Detect When User Is Trying to Swipe Out of Bounds
How to Enable Usb Debugging in Android
Fragment Transaction Animation: Slide in and Slide Out
The Following Classes Could Not Be Found: Android.Support.V7.Internal.App.Windowdecoractionbar