get Pressed Color Selector - Android Graphics

Android examples for Graphics:Drawable

Description

get Pressed Color Selector

Demo Code


//package com.java2s;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;

public class Main {
    public static Drawable getPressedColorSelector(final int colorValue) {
        return getPressedColorSelector(colorValue, new ColorDrawable(
                colorValue));/*from w ww.  ja  v  a2  s.c  o m*/
    }

    public static Drawable getPressedColorSelector(int colorValue,
            Drawable mask) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return new RippleDrawable(new ColorStateList(
                    new int[][] { {} }, new int[] { colorValue }), null,
                    mask);
        } else {
            final StateListDrawable sld = new StateListDrawable();
            sld.addState(new int[] { android.R.attr.state_pressed }, mask);
            sld.setColorFilter(colorValue, PorterDuff.Mode.SRC_IN);
            return sld;
        }
    }
}

Related Tutorials