Example usage for android.support.v4.content ContextCompat getDrawable

List of usage examples for android.support.v4.content ContextCompat getDrawable

Introduction

In this page you can find the example usage for android.support.v4.content ContextCompat getDrawable.

Prototype

public static final Drawable getDrawable(Context context, int i) 

Source Link

Usage

From source file:Main.java

public static Drawable getDrawable(Context context, int id) {
    return ContextCompat.getDrawable(context, id);
}

From source file:Main.java

public static Drawable getDrawableIcon(Context context, int drawableIcon, int size) {
    Drawable drawable;//from www . j  ava2 s  . c o m
    drawable = ContextCompat.getDrawable(context, drawableIcon);
    if (drawable != null)
        drawable.setBounds(0, 0, size, size);
    return drawable;
}

From source file:Main.java

public static Drawable getDrawable(Context context, @DrawableRes int id) {
    return ContextCompat.getDrawable(context, id);
}

From source file:Main.java

public static Drawable getDrawable(@NonNull Context context, @DrawableRes int id) {
    return ContextCompat.getDrawable(context, id);
}

From source file:Main.java

public static final Drawable getDrawable(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= Build.VERSION_CODES.LOLLIPOP) { // 21
        return ContextCompat.getDrawable(context, id);
    } else {/*from ww  w  .  ja  v a2 s  . c  om*/
        return context.getResources().getDrawable(id);
    }
}

From source file:Main.java

public static void startVDAnimation(ImageView imageView, @DrawableRes int inactiveResId,
        @DrawableRes int activeResId, int duration) {
    int drawableResId = imageView.isSelected() ? activeResId : inactiveResId;
    Drawable drawable = ContextCompat.getDrawable(imageView.getContext(), drawableResId);
    imageView.setImageDrawable(drawable);

    if (drawable instanceof Animatable) {
        Animatable animatable = (Animatable) drawable;
        if (animatable.isRunning()) {
            animatable.stop();//w w w .  j ava  2s  .  c  o m
        }

        animatable.start();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            imageView.setSelected(!imageView.isSelected());
        } else {
            imageView.postDelayed(() -> {
                imageView.setSelected(!imageView.isSelected());
                int nextDrawableResId = imageView.isSelected() ? activeResId : inactiveResId;
                Drawable nextDrawable = ContextCompat.getDrawable(imageView.getContext(), nextDrawableResId);
                imageView.setImageDrawable(nextDrawable);
            }, duration);
        }
    }
}

From source file:Main.java

public static Drawable getDrawable(Context c, @AttrRes int attr) {
    return ContextCompat.getDrawable(c, getDrawableRes(c, attr));
}

From source file:Main.java

/**
 * Equivalent {@link Context#getResources()#getDrawable(Context, int)}
 *///from  ww  w . j  ava2s . co m
public static Drawable getDrawable(Context context, @DrawableRes int res) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return context.getResources().getDrawable(res, null);
    }
    return ContextCompat.getDrawable(context, res);
}

From source file:Main.java

/**
 * Set the drawable to a specific color and return it
 * @param drawableId the int ID of the drawable to change
 * @param colorToSet The color to set it to
 * @return Drawable/*w w w  .  ja v a2  s  . co  m*/
 * @throws NullPointerException, if it fails, throws a null pointer
 */
public static Drawable colorDrawable(int drawableId, int colorToSet, Context context) {
    try {
        Drawable drawable = ContextCompat.getDrawable(context, drawableId);
        drawable.mutate().setColorFilter(colorToSet, PorterDuff.Mode.MULTIPLY);
        return drawable;
    } catch (Exception e) {
        e.printStackTrace();
        throw new NullPointerException();
    }
}

From source file:arun.com.popularmovies.util.Utility.java

public static Drawable getPlaceholderImage(Context context) {
    if (placeHolder == null) {
        // tint the placeholder image based on app's material accent color
        placeHolder = ContextCompat.getDrawable(context, R.drawable.ic_movie_black_48dp);
        Drawable wrapDrawable = DrawableCompat.wrap(placeHolder);
        DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(context, R.color.colorAccent));
    }/*from www . j  a  v a  2 s. co m*/
    return placeHolder;
}