get Drawable from resource id - Android Graphics

Android examples for Graphics:Drawable

Description

get Drawable from resource id

Demo Code


//package com.book2s;

import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;

public class Main {
    private static Activity activity;

    public static Drawable getDrawable(Context paramContext, int paramInt) {
        Resources localResources = paramContext.getResources();
        BitmapFactory.Options localOptions = new BitmapFactory.Options();
        localOptions.inPurgeable = true;
        localOptions.inInputShareable = true;
        Rect localRect = new Rect();
        Bitmap localBitmap = decodeResource(paramContext, paramInt,
                localRect, true);//ww w  .  j a v a2  s .c  om
        if ((localBitmap.getNinePatchChunk() == null)
                || (localRect == null))
            return new BitmapDrawable(localResources, localBitmap);
        return new NinePatchDrawable(localResources, localBitmap,
                localBitmap.getNinePatchChunk(), localRect,
                String.valueOf(paramInt));
    }

    public static Drawable getDrawable(Context paramContext, int paramInt,
            Rect paramRect) {
        Resources localResources = paramContext.getResources();
        BitmapFactory.Options localOptions = new BitmapFactory.Options();
        localOptions.inPurgeable = true;
        localOptions.inInputShareable = true;
        Bitmap localBitmap = decodeResource(paramContext, paramInt, true);
        if ((localBitmap.getNinePatchChunk() == null)
                || (paramRect == null))
            return new BitmapDrawable(localResources, localBitmap);
        return new NinePatchDrawable(localResources, localBitmap,
                localBitmap.getNinePatchChunk(), paramRect,
                String.valueOf(paramInt));
    }

    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);
    }

    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) {
            // ???????sw600
            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