Android examples for Graphics:Bitmap Resource
get Bitmaps from resource
//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; public class Main { public static Bitmap[] getBitmaps(Context context, int resourseId, int row, int col, float multiple) { Bitmap bitmaps[] = new Bitmap[row * col]; Bitmap source = decodeResource(context, resourseId); int temp = 0; for (int i = 1; i <= row; i++) { for (int j = 1; j <= col; j++) { bitmaps[temp] = getImage(context, source, i, j, row, col, multiple, false); temp++;/*from www .jav a 2s . com*/ } } if (source != null && !source.isRecycled()) { source.recycle(); source = null; } return bitmaps; } public static Bitmap[] getBitmaps(Context context, String resName, int row, int col, float multiple) { Bitmap bitmaps[] = new Bitmap[row * col]; Bitmap source = decodeBitmapFromAssets(context, resName); int temp = 0; for (int i = 1; i <= row; i++) { for (int j = 1; j <= col; j++) { bitmaps[temp] = getImage(context, source, i, j, row, col, multiple, false); temp++; } } if (source != null && !source.isRecycled()) { source.recycle(); source = null; } return bitmaps; } @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 Bitmap getImage(Context context, Bitmap source, int row, int col, int rowTotal, int colTotal, float multiple, boolean isRecycle) { Bitmap temp = getBitmap(source, (col - 1) * source.getWidth() / colTotal, (row - 1) * source.getHeight() / rowTotal, source.getWidth() / colTotal, source.getHeight() / rowTotal); if (isRecycle) { recycleBitmap(source); } if (multiple != 1.0) { Matrix matrix = new Matrix(); matrix.postScale(multiple, multiple); temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), matrix, true); } return temp; } @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; } public static void recycleBitmap(Bitmap b) { if (b != null && !b.isRecycled()) { b.recycle(); b = null; } } }