decode Resource - Android Graphics

Android examples for Graphics:Bitmap File

Description

decode Resource

Demo Code


//package com.book2s;

import java.io.InputStream;
import android.app.Activity;
import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.Rect;

import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;

public class Main {
    private static Activity activity;

    public static Bitmap decodeResource(Context paramContext, int assetsId) {
        BitmapFactory.Options localOptions = new BitmapFactory.Options();
        localOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
        localOptions.inPurgeable = true;
        localOptions.inInputShareable = true;
        return BitmapFactory.decodeStream(paramContext.getResources()
                .openRawResource(assetsId), null, localOptions);
    }/* w w  w.j  a  v a 2  s . com*/

    public static Bitmap decodeResource(Context paramContext, int paramInt,
            Rect paramRect, boolean paramBoolean) {
        BitmapFactory.Options localOptions = new BitmapFactory.Options();
        localOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
        localOptions.inPurgeable = true;
        localOptions.inInputShareable = true;
        TypedValue localTypedValue = new TypedValue();
        paramContext.getResources().getValue(paramInt, localTypedValue,
                true);
        InputStream localInputStream = paramContext.getResources()
                .openRawResource(paramInt);
        Display localDisplay = activity.getWindowManager()
                .getDefaultDisplay();
        DisplayMetrics localDisplayMetrics = new DisplayMetrics();
        localDisplay.getMetrics(localDisplayMetrics);
        Bitmap localBitmap = BitmapFactory.decodeStream(localInputStream,
                paramRect, localOptions);
        if (paramBoolean) {
            if (localTypedValue.string.toString().contains("sw600")) {
                localBitmap.setDensity(640 * localDisplayMetrics.densityDpi
                        / localDisplayMetrics.heightPixels);
            } else {
                localBitmap.setDensity(480 * localDisplayMetrics.densityDpi
                        / localDisplayMetrics.heightPixels);
            }
        }
        return localBitmap;
    }

    public static Bitmap decodeResource(Context paramContext, int paramInt,
            boolean paramBoolean) {
        return decodeResource(paramContext, paramInt, null, paramBoolean);
    }
}

Related Tutorials