get Drawables from resource - Android Graphics

Android examples for Graphics:Drawable Read

Description

get Drawables from resource

Demo Code


//package com.java2s;

import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;

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

public class Main {
    public static Drawable[] getDrawables(Context context, int resourseId,
            int row, int col, float multiple) {
        Drawable drawables[] = new Drawable[row * col];

        Bitmap source = decodeResource(context, resourseId);

        int temp = 0;

        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= col; j++) {
                drawables[temp] = getDrawableImage(context, source, i, j,
                        row, col, multiple);
                temp++;//from ww  w  . j a  v  a  2s . com
            }
        }

        if (source != null && !source.isRecycled()) {
            source.recycle();
            source = null;
        }

        return drawables;
    }

    public static Drawable[] getDrawables(Context context, String resName,
            int row, int col, float multiple) {
        Drawable drawables[] = new Drawable[row * col];

        Bitmap source = decodeBitmapFromAssets(context, resName);

        int temp = 0;

        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= col; j++) {
                drawables[temp] = getDrawableImage(context, source, i, j,
                        row, col, multiple);
                temp++;
            }
        }

        if (source != null && !source.isRecycled()) {
            source.recycle();
            source = null;
        }

        return drawables;
    }

    @SuppressWarnings("deprecation")
    public static Bitmap decodeResource(Context context, int resourseId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
        opt.inPurgeable = true;
        opt.inInputShareable = true; // ?? inPurgeable???true????????

        // ?????????
        InputStream is = context.getResources().openRawResource(resourseId);

        return BitmapFactory.decodeStream(is, null, opt); // decodeStream????JNI>>nativeDecodeAsset()??????decode???????java??createBitmap???????java????
    }

    public static Drawable getDrawableImage(Context context, Bitmap source,
            int row, int col, int rowTotal, int colTotal, float multiple) {

        Bitmap temp = getBitmap(source, (col - 1) * source.getWidth()
                / colTotal, (row - 1) * source.getHeight() / rowTotal,
                source.getWidth() / colTotal, source.getHeight() / rowTotal);

        if (multiple != 1.0) {
            Matrix matrix = new Matrix();
            matrix.postScale(multiple, multiple);
            temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(),
                    temp.getHeight(), matrix, true);
        }

        Drawable d = new BitmapDrawable(context.getResources(), temp);

        return d;
    }

    @SuppressWarnings("deprecation")
    public static Bitmap decodeBitmapFromAssets(Context context,
            String resName) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inPurgeable = true;
        options.inInputShareable = true;

        InputStream in = null;

        try {
            // in = AssetsResourcesUtil.openResource(resName);
            in = context.getAssets().open(resName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return BitmapFactory.decodeStream(in, null, options);
    }

    public static Bitmap getBitmap(Bitmap source, int x, int y, int width,
            int height) {
        Bitmap bitmap = Bitmap.createBitmap(source, x, y, width, height);
        return bitmap;
    }
}

Related Tutorials