Scale Bitmap to Grid - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

Scale Bitmap to Grid

Demo Code


//package com.book2s;

import android.graphics.Bitmap;

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

public class Main {
    public static Bitmap Scale9Grid(Bitmap paramBitmap, Rect paramRect,
            int paramInt1, int paramInt2) {
        Bitmap localBitmap = Bitmap.createBitmap(paramInt1, paramInt2,
                Bitmap.Config.ARGB_8888);
        Canvas localCanvas = new Canvas(localBitmap);
        int i = paramBitmap.getWidth();
        int j = paramBitmap.getHeight();
        int k = paramRect.left;
        int m = i - paramRect.right;
        int n = paramRect.top;
        int i1 = j - paramRect.bottom;
        int i2 = paramInt1 - m;
        int i3 = paramInt2 - i1;
        localCanvas.drawBitmap(paramBitmap, new Rect(0, 0, k, n), new Rect(
                0, 0, k, n), null);//  w  ww.  j ava 2s . c om
        localCanvas.drawBitmap(paramBitmap, new Rect(paramRect.right, 0, i,
                n), new Rect(i2, 0, paramInt1, n), null);
        localCanvas.drawBitmap(paramBitmap, new Rect(0, paramRect.bottom,
                k, j), new Rect(0, i3, k, paramInt2), null);
        localCanvas.drawBitmap(paramBitmap, new Rect(paramRect.right,
                paramRect.bottom, i, j), new Rect(i2, i3, paramInt1,
                paramInt2), null);
        localCanvas.drawBitmap(paramBitmap, new Rect(k, 0, paramRect.right,
                n), new Rect(k, 0, i2, n), null);
        localCanvas.drawBitmap(paramBitmap, new Rect(k, paramRect.bottom,
                paramRect.right, j), new Rect(k, i3, i2, paramInt2), null);
        localCanvas.drawBitmap(paramBitmap, new Rect(0, n, k,
                paramRect.bottom), new Rect(0, n, k, i3), null);
        localCanvas.drawBitmap(paramBitmap, new Rect(paramRect.right, n, i,
                paramRect.bottom), new Rect(i2, n, paramInt1, i3), null);
        localCanvas.drawBitmap(paramBitmap, paramRect, new Rect(
                paramRect.left, paramRect.top, i2, i3), null);
        return localBitmap;
    }

    public static Drawable Scale9Grid(Drawable paramDrawable,
            Rect paramRect, int paramInt1, int paramInt2) {
        return BitmapToDrawable(Scale9Grid(DrawableToBitmap(paramDrawable),
                paramRect, paramInt1, paramInt2));
    }

    public static BitmapDrawable BitmapToDrawable(Bitmap paramBitmap) {
        return new BitmapDrawable(paramBitmap);
    }

    public static Bitmap DrawableToBitmap(Drawable drawable) {
        int i = drawable.getIntrinsicWidth();
        int j = drawable.getIntrinsicHeight();
        android.graphics.Bitmap.Config config;
        Bitmap bitmap;
        Canvas canvas;
        if (drawable.getOpacity() != -1)
            config = android.graphics.Bitmap.Config.ARGB_8888;
        else
            config = android.graphics.Bitmap.Config.RGB_565;
        bitmap = Bitmap.createBitmap(i, j, config);
        canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}

Related Tutorials