/* * Copyright 2015. Appsi Mobile * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.appsimobile.appsii; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.ShapeDrawable; import android.os.Build; /** * Created by nick on 21/01/15. */ public class DrawableCompat { public static void setTintColorCompat(Drawable drawable, int color) { drawable.mutate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { drawable.setTint(color); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (!handleShapeDrawable(drawable, color)) { drawable.setTint(color); } } else { if (!handleShapeDrawable(drawable, color)) { drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); } } } /** * Set the color of the drawable for the case where it is a shape drawable. * Shape drawables need custom handling on all platforms before lollipop mr2 * Returns true when the color was successfully applied, false otherwise */ private static boolean handleShapeDrawable(Drawable drawable, int color) { if (drawable instanceof ShapeDrawable) { ((ShapeDrawable) drawable).getPaint().setColor(color); return true; } else if (drawable instanceof GradientDrawable) { ((GradientDrawable) drawable).setColor(color); } return false; } }