Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class Main {

    public static Drawable toGreyDrawable(Drawable drawable) {
        int w = drawable.getMinimumWidth();
        int h = drawable.getMinimumHeight();
        if (w <= 0 || h <= 0) {
            return drawable;
        }
        Rect bounds = drawable.getBounds();
        Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(grey);
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        drawable.setColorFilter(new ColorMatrixColorFilter(cm));
        drawable.setBounds(0, 0, w, h);
        drawable.draw(c);
        drawable.clearColorFilter();
        drawable.setBounds(bounds);
        BitmapDrawable bd = new BitmapDrawable(grey);
        bd.setBounds(0, 0, w, h);
        return bd;
    }
}