get Drawable from resource by ID - Android Graphics

Android examples for Graphics:Drawable Read

Description

get Drawable from resource by ID

Demo Code


//package com.java2s;
import android.annotation.SuppressLint;
import android.content.Context;

import android.graphics.drawable.Drawable;

import android.os.Build;

public class Main {
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public static Drawable getDrawable(Context context, int resourceId) {

        if (Build.VERSION.SDK_INT >= 21) {
            return context.getDrawable(resourceId);
        } else {/*from   w w  w  . ja  v  a 2s .c  o m*/
            return context.getResources().getDrawable(resourceId);
        }
    }

    public static Drawable getDrawable(Context context, int resourceId,
            int convertToColor) {
        Drawable drawable = getDrawable(context, resourceId);
        return changeDrawableColor(drawable, convertToColor);

    }

    public static Drawable changeDrawableColor(Drawable drawable,
            int destinationColor) {
        drawable.setColorFilter(destinationColor,
                android.graphics.PorterDuff.Mode.MULTIPLY);
        return drawable;
    }
}

Related Tutorials