android.support.graphics.drawable.VectorDrawableCompat Java Examples

The following examples show how to use android.support.graphics.drawable.VectorDrawableCompat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextUtils.java    From openlauncher with Apache License 2.0 6 votes vote down vote up
/**
 * Get a {@link Bitmap} out of a {@link Drawable}
 */
public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;
    if (drawable instanceof VectorDrawableCompat
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
            || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        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);
    } else if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }
    return bitmap;
}
 
Example #2
Source File: ContextUtils.java    From kimai-android with MIT License 6 votes vote down vote up
/**
 * Get a {@link Bitmap} out of a {@link Drawable}
 */
public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;
    if (drawable instanceof VectorDrawableCompat
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
            || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        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);
    } else if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }
    return bitmap;
}
 
Example #3
Source File: PickerBitmapView.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Initialization for the special tiles (camera/gallery icon).
 * @param bitmapDetails The details about the bitmap represented by this PickerBitmapView.
 */
public void initializeSpecialTile(PickerBitmap bitmapDetails) {
    int labelStringId;
    Drawable image;
    Resources resources = mContext.getResources();

    if (isCameraTile()) {
        image = ApiCompatibilityUtils.getDrawable(resources, R.drawable.ic_photo_camera);
        labelStringId = R.string.photo_picker_camera;
    } else {
        image = VectorDrawableCompat.create(
                resources, R.drawable.ic_collections_grey, mContext.getTheme());
        labelStringId = R.string.photo_picker_browse;
    }

    mSpecialTileIcon.setImageDrawable(image);
    mSpecialTileLabel.setText(labelStringId);

    // Reset visibility, since #initialize() sets mSpecialTile visibility to GONE.
    mSpecialTile.setVisibility(View.VISIBLE);
    mSpecialTileIcon.setVisibility(View.VISIBLE);
    mSpecialTileLabel.setVisibility(View.VISIBLE);
}
 
Example #4
Source File: ContextUtils.java    From Stringlate with MIT License 6 votes vote down vote up
/**
 * Get a {@link Bitmap} out of a {@link Drawable}
 */
public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;
    if (drawable instanceof VectorDrawableCompat
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
            || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        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);
    } else if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }
    return bitmap;
}
 
Example #5
Source File: ItemDetailActivity.java    From android-instant-apps-demo with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_detail);
    viewModel = ViewModelProviders.of(this).get(ItemDetailViewModel.class);
    viewModel.getCartId().observe(this, this::updatedCartId);

    fab = (FloatingActionButton) findViewById(R.id.fab);
    toolbar = (Toolbar) findViewById(R.id.toolbar);

    handleDeepLink();
    setupToolbar();

    fab.setImageDrawable(VectorDrawableCompat.create(getResources(), R.drawable.ic_add_shopping_cart_white_24dp, null));
    fab.setOnClickListener(this::addToCart);
}
 
Example #6
Source File: CartActivity.java    From android-instant-apps-demo with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    viewModel = ViewModelProviders.of(this).get(CartViewModel.class);

    fab = (FloatingActionButton) findViewById(R.id.fab);
    toolbar = (Toolbar) findViewById(R.id.toolbar);

    setupToolbar();
    handleDeepLink();

    fab.setImageDrawable(VectorDrawableCompat.create(getResources(), R.drawable.ic_share_white_24dp, null));
    fab.setOnClickListener(view -> {
        String cartId = viewModel.getCartId().getValue();
        ShareCompat.IntentBuilder.from(this)
                .setText(String.format(Locale.US, "Check out my shopping cart now using Android Instant Apps! \n%s/cart/%s", ROOT_ENDPOINT, cartId))
                .setType("text/plain")
                .setChooserTitle(share_cart)
                .startChooser();
    });
}
 
Example #7
Source File: VideoViewHolder.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
@Override
public void onSharedElementEnter() {
    final View view = itemView.findViewById(R.id.image);

    Resources res = itemView.getContext().getResources();
    final Drawable playOverlay = VectorDrawableCompat.create(res,
            R.drawable.play_indicator, itemView.getContext().getTheme());

    if (playOverlay == null) {
        return;
    }

    view.post(new Runnable() {
        @Override
        public void run() {
            int dimen = (int) view.getContext().getResources()
                    .getDimension(R.dimen.twenty_four_dp) * 2;

            int left = view.getWidth() / 2 - dimen / 2;
            int top = view.getHeight() / 2 - dimen / 2;

            playOverlay.setBounds(left, top, left + dimen, top + dimen);
            view.getOverlay().add(playOverlay);
        }
    });
}
 
Example #8
Source File: ContextUtils.java    From memetastic with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get a {@link Bitmap} out of a {@link Drawable}
 */
public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;
    if (drawable instanceof VectorDrawableCompat
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
            || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        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);
    } else if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }
    return bitmap;
}
 
Example #9
Source File: FingerprintIconView.java    From BiometricPromptCompat with Apache License 2.0 6 votes vote down vote up
public void setState(State state, boolean animate) {
    if (state == this.state) return;

    @DrawableRes int resId = getDrawable(this.state, state, animate);
    if (resId == 0) {
        setImageDrawable(null);
    } else {
        Drawable icon = null;
        if (animate) {
            icon = AnimatedVectorDrawableCompat.create(getContext(), resId);
        }
        if (icon == null) {
            icon = VectorDrawableCompat.create(getResources(), resId, getContext().getTheme());
        }
        setImageDrawable(icon);

        if (icon instanceof Animatable) {
            ((Animatable) icon).start();
        }
    }

    this.state = state;
}
 
Example #10
Source File: MineRowView.java    From FriendBook with GNU General Public License v3.0 6 votes vote down vote up
private void initAttributeSet(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MineRowView);
    int iconResId = typedArray.getResourceId(R.styleable.MineRowView_mine_icon, 0);
    if (iconResId != 0) {
        Drawable icon = VectorDrawableCompat.create(typedArray.getResources(),iconResId, null);
        ivIcon.setImageDrawable(icon);
    }

    String title = typedArray.getString(R.styleable.MineRowView_mine_title);
    tvTitle.setText(title);
    boolean dividerVisibility = typedArray.getBoolean(R.styleable.MineRowView_mine_divider_visibility, true);
    divider.setVisibility(dividerVisibility ? View.VISIBLE : View.GONE);
    boolean arrowVisibility = typedArray.getBoolean(R.styleable.MineRowView_mine_arrow_visibility, true);
    arrow.setVisibility(arrowVisibility ? View.VISIBLE : View.GONE);
    typedArray.recycle();


}
 
Example #11
Source File: VectorDrawableInflateImpl.java    From timecat with Apache License 2.0 5 votes vote down vote up
@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    ColorStateList colorFilter = getTintColorList(context, attrs, android.R.attr.tint);
    Drawable d;
    if (resId == 0) {
        d = VectorDrawableCompat.createFromXmlInner(context.getResources(), parser, attrs);
    } else {
        d = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
    }
    if (d != null && colorFilter != null) {
        DrawableCompat.setTintList(d, colorFilter);
    }
    return d;
}
 
Example #12
Source File: BitmapUtil.java    From FriendBook with GNU General Public License v3.0 5 votes vote down vote up
public static Bitmap vectorToBitmap(Context ctx, @DrawableRes int resVector) {
    Drawable drawable =VectorDrawableCompat.create(ctx.getResources(),resVector,null);
    //Drawable drawable = AppCompatDrawableManager.get().getDrawable(ctx, resVector);
    Bitmap b = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    drawable.setBounds(0, 0, c.getWidth(), c.getHeight());
    drawable.draw(c);
    return b;
}
 
Example #13
Source File: ImageUtils.java    From Pasta-Music with Apache License 2.0 5 votes vote down vote up
public static Drawable getVectorDrawable(Context context, int resId) {
    VectorDrawableCompat drawable;
    try {
        drawable = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
    } catch (Exception e) {
        e.printStackTrace();
        return new ColorDrawable(Color.TRANSPARENT);
    }

    if (drawable != null)
        return drawable.getCurrent();
    else
        return new ColorDrawable(Color.TRANSPARENT);
}
 
Example #14
Source File: App.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
public static Drawable getVecDrawable(Context context, @DrawableRes int id) {
    Drawable drawable = AppCompatResources.getDrawable(context, id);
    if (!(drawable instanceof VectorDrawableCompat || drawable instanceof VectorDrawable)) {
        throw new RuntimeException();
    }
    return drawable;
}
 
Example #15
Source File: TriangularIndicator.java    From TriangularCustomView with MIT License 5 votes vote down vote up
public void init(@Nullable AttributeSet set){

        resources = new int[noOfTabs];
        mRect = new RectF();
        mRect.left = 0;
        mRect.top = 0;
        mRect.bottom = 140;

        triangle = new Path();
        leftpt=new PointF();
        midpt=new PointF();
        rightpt=new PointF();


        mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mRectPaint.setStyle(Paint.Style.FILL);
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(Color.GREEN);
        if(set == null)
        {
            return;
        }

        TypedArray ta = getContext().obtainStyledAttributes(set,R.styleable.TriangularIndicator);
        noOfTabs = ta.getInteger(R.styleable.TriangularIndicator_no_of_sections,3);
        sectionColour = ta.getColor(R.styleable.TriangularIndicator_set_colour,Color.YELLOW);
        mRectPaint.setColor(sectionColour);
        mCirclePaint.setColor(Color.WHITE);
        imgRes = new Bitmap[noOfTabs];
        vds = new VectorDrawableCompat[noOfTabs];
        ta.recycle();

    }
 
Example #16
Source File: VectorDrawableInflateImpl.java    From MagicaSakura with Apache License 2.0 5 votes vote down vote up
@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    ColorStateList colorFilter = getTintColorList(context, attrs, android.R.attr.tint);
    Drawable d;
    if (resId == 0) {
        d = VectorDrawableCompat.createFromXmlInner(context.getResources(), parser, attrs);
    } else {
        d = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
    }
    if (d != null && colorFilter != null) {
        DrawableCompat.setTintList(d, colorFilter);
    }
    return d;
}
 
Example #17
Source File: SelectableListLayout.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the view shown when the selectable list is empty.
 *
 * @param emptyIconResId The icon to show when the selectable list is empty.
 * @param emptyStringResId The string to show when the selectable list is empty.
 */
public void initializeEmptyView(int emptyIconResId, int emptyStringResId) {
    mEmptyView.setCompoundDrawablesWithIntrinsicBounds(null,
            VectorDrawableCompat.create(getResources(), emptyIconResId,
                    getContext().getTheme()),
            null, null);
    mEmptyView.setText(emptyStringResId);
}
 
Example #18
Source File: ResourceUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
private static Bitmap getBitmap(VectorDrawableCompat vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #19
Source File: UpcomingWeekendsListAdapter.java    From Travel-Mate with MIT License 5 votes vote down vote up
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    UpcomingWeekendsViewHolder holder = (UpcomingWeekendsViewHolder) viewHolder;
    UpcomingWeekends uw = mUpcomingWeekends.get(i);

    holder.name.setText(uw.getmName());
    holder.timelineView.setMarker(VectorDrawableCompat
            .create(mContext.getResources(), R.drawable.ic_marker_inactive, mContext.getTheme()));
    holder.date.setText(uw.getmDate() + "-" + uw.getmMonth());
}
 
Example #20
Source File: Theme.java    From APlayer with GNU General Public License v3.0 5 votes vote down vote up
public static Drawable getVectorDrawable(@NonNull Resources res, @DrawableRes int resId,
    @Nullable Resources.Theme theme) {
  if (Build.VERSION.SDK_INT >= 21) {
    return res.getDrawable(resId, theme);
  }
  return VectorDrawableCompat.create(res, resId, theme);
}
 
Example #21
Source File: GridTimePickerDialog.java    From BottomSheetPickers with Apache License 2.0 5 votes vote down vote up
private void updateAmPmDisplay(int amOrPm) {
    int firstColor = amOrPm == HALF_DAY_1 ? mHalfDaySelectedColor : mHalfDayUnselectedColor;
    int secondColor = amOrPm == HALF_DAY_2 ? mHalfDaySelectedColor : mHalfDayUnselectedColor;

    if (mIs24HourMode) {
        final Drawable firstHalfDayToggle = mFirstHalfDayToggle.getDrawable();
        final Drawable secondHalfDayToggle = mSecondHalfDayToggle.getDrawable();
        if (Utils.checkApiLevel(Build.VERSION_CODES.LOLLIPOP)) {
            firstHalfDayToggle.setTint(firstColor);
            secondHalfDayToggle.setTint(secondColor);
        } else {
            // Ignore the Lint warning that says the casting is redundant;
            // it is in fact necessary.
            ((VectorDrawableCompat) firstHalfDayToggle).setTint(firstColor);
            ((VectorDrawableCompat) secondHalfDayToggle).setTint(secondColor);
        }
    } else {
        mAmTextView.setTextColor(firstColor);
        mPmTextView.setTextColor(secondColor);
    }

    if (amOrPm == HALF_DAY_1) {
        Utils.tryAccessibilityAnnounce(mTimePicker, mAmText);
        mAmPmHitspace.setContentDescription(mAmText);
    } else if (amOrPm == HALF_DAY_2) {
        Utils.tryAccessibilityAnnounce(mTimePicker, mPmText);
        mAmPmHitspace.setContentDescription(mPmText);
    } else {
        mAmTextView.setText(mDoublePlaceholderText);
    }
}
 
Example #22
Source File: ResourceUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
private static Bitmap getBitmap(VectorDrawableCompat vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #23
Source File: ResourceUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableResId);
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof VectorDrawableCompat) {
        return getBitmap((VectorDrawableCompat) drawable);
    } else if (drawable instanceof VectorDrawable) {
        return getBitmap((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("Unsupported drawable type");
    }
}
 
Example #24
Source File: NavigationIcons.java    From navigation-widgets with MIT License 5 votes vote down vote up
public Drawable iconDrawable(@NonNull Context context) {
    if (drawable != null) {
        return drawable;
    } else {
        VectorDrawableCompat navIcon = create(context.getResources(), drawableRes, context.getTheme());
        setTint(navIcon, color(context, android.R.color.white));
        return navIcon;
    }
}
 
Example #25
Source File: ImageUtils.java    From Pasta-for-Spotify with Apache License 2.0 5 votes vote down vote up
public static Drawable getVectorDrawable(Context context, int resId) {
    VectorDrawableCompat drawable;
    try {
        drawable = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
    } catch (Exception e) {
        e.printStackTrace();
        return new ColorDrawable(Color.TRANSPARENT);
    }

    if (drawable != null)
        return drawable.getCurrent();
    else
        return new ColorDrawable(Color.TRANSPARENT);
}
 
Example #26
Source File: DrawableManager.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
public static Drawable getVectorDrawable(@NonNull Resources res,
        @DrawableRes int resId, @Nullable Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return res.getDrawable(resId, theme);
    } else {
        return VectorDrawableCompat.create(res, resId, theme);
    }
}
 
Example #27
Source File: HistoryItemView.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void setItem(HistoryItem item) {
    if (getItem() == item) {
        // If the item is being set again, it means the HistoryAdapter contents have likely
        // changed. This item may have changed group positions, so the background should be
        // updated.
        setBackgroundResourceForGroupPosition();
        return;
    }

    super.setItem(item);

    mTitle.setText(item.getTitle());
    mDomain.setText(item.getDomain());
    mIsItemRemoved = false;

    if (item.wasBlockedVisit()) {
        if (mBlockedVisitDrawable == null) {
            mBlockedVisitDrawable = VectorDrawableCompat.create(
                    getContext().getResources(), R.drawable.ic_block_red,
                    getContext().getTheme());
        }
        mIconImageView.setImageDrawable(mBlockedVisitDrawable);
        mTitle.setTextColor(
                ApiCompatibilityUtils.getColor(getResources(), R.color.google_red_700));
    } else {
        mIconImageView.setImageResource(R.drawable.default_favicon);
        if (mHistoryManager != null) requestIcon();

        mTitle.setTextColor(
                ApiCompatibilityUtils.getColor(getResources(), R.color.default_text_color));
    }

    setBackgroundResourceForGroupPosition();
}
 
Example #28
Source File: BluetoothChooserDialog.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private Drawable getIconWithRowIconColorStateList(int icon) {
    Resources res = mActivity.getResources();

    Drawable drawable = VectorDrawableCompat.create(res, icon, mActivity.getTheme());
    DrawableCompat.setTintList(drawable,
            ApiCompatibilityUtils.getColorStateList(res, R.color.item_chooser_row_icon_color));
    return drawable;
}
 
Example #29
Source File: MdVectorDrawableCompat.java    From android-md-core with Apache License 2.0 5 votes vote down vote up
@Nullable
public static VectorDrawableCompat getFromAttribute(@NonNull Context context, AttributeSet attrs, int attrId) {
  VectorDrawableCompat drawable = null;
  TypedArray a = context.getTheme().obtainStyledAttributes(attrs, new int[]{attrId}, 0, 0);
  try {
    int resId = a.getResourceId(0, 0);
    if (resId > 0) {
      drawable = MdVectorDrawableCompat.create(context, resId);
    }
  } catch (Exception ignore) {
  }
  a.recycle();
  return drawable;
}
 
Example #30
Source File: VectorDrawableUtils.java    From Timeline with Apache License 2.0 5 votes vote down vote up
public static Drawable getDrawable(Context context, int drawableResId) {
    Drawable drawable;

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        drawable = context.getResources().getDrawable(drawableResId, context.getTheme());
    } else {
        drawable = VectorDrawableCompat.create(context.getResources(), drawableResId, context.getTheme());
    }

    return drawable;
}