tint Button Drawable - Android Graphics

Android examples for Graphics:Drawable Tint

Description

tint Button Drawable

Demo Code

/**/*from w  w  w .j  a  v  a 2  s  .c  om*/
 * UmbalaApp
 *
 * Created by Hoang Anh on 8/10/15.
 * Copyright (c) 2015 Umbala. All rights reserved.
 */
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.widget.Button;
import android.widget.ImageView;

public class Main{
    public static void tintButtonDrawable(Button button, int tintColor) {
        Drawable[] drawables = button.getCompoundDrawables();
        for (int i = 0; i < drawables.length; i++) {
            if (drawables[i] == null)
                continue;
            drawables[i] = tintingDrawable(drawables[i], tintColor);
        }
        button.setCompoundDrawables(drawables[0], drawables[1],
                drawables[2], drawables[3]);
    }
    public static Drawable tintingDrawable(Drawable drawable, int tintColor) {
        // Wrap the drawable so that future tinting calls work
        // on pre-v21 devices. Always use the returned drawable.
        drawable = DrawableCompat.wrap(drawable);

        // We can now set a tint
        DrawableCompat.setTint(drawable, tintColor);
        //        // ...and a different tint mode
        //        DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

        return drawable;
    }
}

Related Tutorials