Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

import android.util.Log;
import android.widget.ImageView;

public class Main {
    static public void setImageColor(ImageView view, Bitmap sourceBitmap, int rgbcolor)// ,Bitmap sourceBitmap)
    {
        if (sourceBitmap != null) {
            float R = Color.red(rgbcolor);
            float G = Color.green(rgbcolor);
            float B = Color.blue(rgbcolor);

            Log.v("R:G:B", R + ":" + G + ":" + B); //

            float[] colorTransform = { R / 255f, 0, 0, 0, 0, // R color
                    0, G / 255f, 0, 0, 0 // G color
                    , 0, 0, B / 255f, 0, 0 // B color
                    , 0, 0, 0, 1f, 0f };

            ColorMatrix colorMatrix = new ColorMatrix();
            colorMatrix.setSaturation(0f); // Remove Colour

            colorMatrix.set(colorTransform);
            ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
            Paint paint = new Paint();
            paint.setColorFilter(colorFilter);

            Bitmap mutableBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);
            view.setImageBitmap(mutableBitmap);
            Canvas canvas = new Canvas(mutableBitmap);
            canvas.drawBitmap(mutableBitmap, 0, 0, paint);

        }
    }
}