Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;

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

import android.util.TypedValue;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static Map<Integer, Bitmap> loadBitmaps(int arrayId, Resources resources) {
        Map<Integer, Bitmap> bitmapsMaps = new HashMap<>();
        int[] bitmapIds = getIntArray(arrayId, resources);
        for (int i = 0; i < bitmapIds.length; i++) {
            Drawable backgroundDrawable = resources.getDrawable(bitmapIds[i]);
            Bitmap bitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
            bitmapsMaps.put(bitmapIds[i], bitmap);
        }
        return bitmapsMaps;
    }

    public static int[] getIntArray(int resId, Resources mResources) {
        TypedArray array = mResources.obtainTypedArray(resId);
        int[] rc = new int[array.length()];
        TypedValue value = new TypedValue();
        for (int i = 0; i < array.length(); i++) {
            array.getValue(i, value);
            rc[i] = value.resourceId;
        }
        return rc;
    }

    public static Bitmap getBitmap(Drawable drawable, boolean scaleBitmap, int width, int height) {
        Bitmap bitmap;
        if (drawable instanceof BitmapDrawable) {
            bitmap = ((BitmapDrawable) drawable).getBitmap();
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
        }
        if (scaleBitmap) {
            bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
        }
        return bitmap;
    }
}