cn.bingoogolapple.scaffolding.util.DrawableUtil.java Source code

Java tutorial

Introduction

Here is the source code for cn.bingoogolapple.scaffolding.util.DrawableUtil.java

Source

/**
 * Copyright 2016 bingoogolapple
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 cn.bingoogolapple.scaffolding.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.widget.ImageView;

/**
 * : :bingoogolapple@gmail.com
 * :16/8/12 ?11:45
 * ??:
 */
public class DrawableUtil {

    private DrawableUtil() {
    }

    public static Drawable tintDrawable(Context context, Drawable drawable, @ColorRes int color) {
        if (drawable == null) {
            return null;
        }

        final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(wrappedDrawable, context.getResources().getColor(color));
        return wrappedDrawable;
    }

    public static void tintPressedIndicator(ImageView imageView, @DrawableRes int normalResId,
            @DrawableRes int pressedResId, @ColorRes int colorResId) {
        Drawable normal = imageView.getResources().getDrawable(normalResId);
        Drawable pressed = imageView.getResources().getDrawable(pressedResId);
        pressed = tintDrawable(imageView.getContext(), pressed, colorResId);
        imageView.setImageDrawable(getPressedSelectorDrawable(normal, pressed));
    }

    public static StateListDrawable getPressedSelectorDrawable(Drawable normal, Drawable pressed) {
        StateListDrawable bg = new StateListDrawable();
        bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
        bg.addState(new int[] { android.R.attr.state_enabled }, normal);
        bg.addState(new int[] {}, normal);
        return bg;
    }

    public static void tintBackground(View view, @ColorRes int colorResId) {
        ViewCompat.setBackground(view, tintDrawable(view.getContext(), view.getBackground(), colorResId));
    }
}