Search in sources :

Example 11 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project platform_frameworks_base by android.

the class ProgressBar method tileify.

/**
     * Converts a drawable to a tiled version of itself. It will recursively
     * traverse layer and state list drawables.
     */
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof LayerDrawable) {
        final LayerDrawable orig = (LayerDrawable) drawable;
        final int N = orig.getNumberOfLayers();
        final Drawable[] outDrawables = new Drawable[N];
        for (int i = 0; i < N; i++) {
            final int id = orig.getId(i);
            outDrawables[i] = tileify(orig.getDrawable(i), (id == R.id.progress || id == R.id.secondaryProgress));
        }
        final LayerDrawable clone = new LayerDrawable(outDrawables);
        for (int i = 0; i < N; i++) {
            clone.setId(i, orig.getId(i));
            clone.setLayerGravity(i, orig.getLayerGravity(i));
            clone.setLayerWidth(i, orig.getLayerWidth(i));
            clone.setLayerHeight(i, orig.getLayerHeight(i));
            clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i));
            clone.setLayerInsetRight(i, orig.getLayerInsetRight(i));
            clone.setLayerInsetTop(i, orig.getLayerInsetTop(i));
            clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i));
            clone.setLayerInsetStart(i, orig.getLayerInsetStart(i));
            clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i));
        }
        return clone;
    }
    if (drawable instanceof StateListDrawable) {
        final StateListDrawable in = (StateListDrawable) drawable;
        final StateListDrawable out = new StateListDrawable();
        final int N = in.getStateCount();
        for (int i = 0; i < N; i++) {
            out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
        }
        return out;
    }
    if (drawable instanceof BitmapDrawable) {
        final Drawable.ConstantState cs = drawable.getConstantState();
        final BitmapDrawable clone = (BitmapDrawable) cs.newDrawable(getResources());
        clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        if (mSampleWidth <= 0) {
            mSampleWidth = clone.getIntrinsicWidth();
        }
        if (clip) {
            return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL);
        } else {
            return clone;
        }
    }
    return drawable;
}
Also used : LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) AnimationDrawable(android.graphics.drawable.AnimationDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ClipDrawable(android.graphics.drawable.ClipDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ClipDrawable(android.graphics.drawable.ClipDrawable)

Example 12 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project material-calendarview by prolificinteractive.

the class DayView method generateBackground.

private static Drawable generateBackground(int color, int fadeTime, Rect bounds) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.setExitFadeDuration(fadeTime);
    drawable.addState(new int[] { android.R.attr.state_checked }, generateCircleDrawable(color));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        drawable.addState(new int[] { android.R.attr.state_pressed }, generateRippleDrawable(color, bounds));
    } else {
        drawable.addState(new int[] { android.R.attr.state_pressed }, generateCircleDrawable(color));
    }
    drawable.addState(new int[] {}, generateCircleDrawable(Color.TRANSPARENT));
    return drawable;
}
Also used : StateListDrawable(android.graphics.drawable.StateListDrawable)

Example 13 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project robolectric by robolectric.

the class ShadowStateListDrawableTest method testAddDrawableWithWildCardState.

@Test
public void testAddDrawableWithWildCardState() {
    Drawable drawable = ShadowDrawable.createFromPath("/foo");
    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(StateSet.WILD_CARD, drawable);
    ShadowStateListDrawable shadow = shadowOf(stateListDrawable);
    Drawable drawableForState = shadow.getDrawableForState(StateSet.WILD_CARD);
    assertNotNull(drawableForState);
    assertThat(((ShadowBitmapDrawable) shadowOf(drawableForState)).getPath()).isEqualTo("/foo");
}
Also used : StateListDrawable(android.graphics.drawable.StateListDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) Test(org.junit.Test)

Example 14 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project FlatUI by eluleci.

the class FlatCheckBox method init.

private void init(AttributeSet attrs) {
    if (attributes == null)
        attributes = new Attributes(this, getResources());
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.fl_FlatCheckBox);
        // getting common attributes
        int customTheme = a.getResourceId(R.styleable.fl_FlatCheckBox_fl_theme, Attributes.DEFAULT_THEME);
        attributes.setThemeSilent(customTheme, getResources());
        attributes.setFontFamily(a.getString(R.styleable.fl_FlatCheckBox_fl_fontFamily));
        attributes.setFontWeight(a.getString(R.styleable.fl_FlatCheckBox_fl_fontWeight));
        attributes.setFontExtension(a.getString(R.styleable.fl_FlatCheckBox_fl_fontExtension));
        attributes.setSize(a.getDimensionPixelSize(R.styleable.fl_FlatCheckBox_fl_size, Attributes.DEFAULT_SIZE_PX));
        attributes.setRadius(a.getDimensionPixelSize(R.styleable.fl_FlatCheckBox_fl_cornerRadius, Attributes.DEFAULT_RADIUS_PX));
        attributes.setBorderWidth(attributes.getSize() / 10);
        // getting view specific attributes
        dotMargin = a.getDimensionPixelSize(R.styleable.fl_FlatCheckBox_fl_dotMargin, dotMargin);
        a.recycle();
    }
    // creating unchecked-enabled state drawable
    GradientDrawable uncheckedEnabled = new GradientDrawable();
    uncheckedEnabled.setCornerRadius(attributes.getRadius());
    uncheckedEnabled.setSize(attributes.getSize(), attributes.getSize());
    uncheckedEnabled.setColor(Color.TRANSPARENT);
    uncheckedEnabled.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
    // creating checked-enabled state drawable
    GradientDrawable checkedOutside = new GradientDrawable();
    checkedOutside.setCornerRadius(attributes.getRadius());
    checkedOutside.setSize(attributes.getSize(), attributes.getSize());
    checkedOutside.setColor(Color.TRANSPARENT);
    checkedOutside.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
    PaintDrawable checkedCore = new PaintDrawable(attributes.getColor(2));
    checkedCore.setCornerRadius(attributes.getRadius());
    checkedCore.setIntrinsicHeight(attributes.getSize());
    checkedCore.setIntrinsicWidth(attributes.getSize());
    InsetDrawable checkedInside = new InsetDrawable(checkedCore, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
    Drawable[] checkedEnabledDrawable = { checkedOutside, checkedInside };
    LayerDrawable checkedEnabled = new LayerDrawable(checkedEnabledDrawable);
    // creating unchecked-enabled state drawable
    GradientDrawable uncheckedDisabled = new GradientDrawable();
    uncheckedDisabled.setCornerRadius(attributes.getRadius());
    uncheckedDisabled.setSize(attributes.getSize(), attributes.getSize());
    uncheckedDisabled.setColor(Color.TRANSPARENT);
    uncheckedDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
    // creating checked-disabled state drawable
    GradientDrawable checkedOutsideDisabled = new GradientDrawable();
    checkedOutsideDisabled.setCornerRadius(attributes.getRadius());
    checkedOutsideDisabled.setSize(attributes.getSize(), attributes.getSize());
    checkedOutsideDisabled.setColor(Color.TRANSPARENT);
    checkedOutsideDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
    PaintDrawable checkedCoreDisabled = new PaintDrawable(attributes.getColor(3));
    checkedCoreDisabled.setCornerRadius(attributes.getRadius());
    checkedCoreDisabled.setIntrinsicHeight(attributes.getSize());
    checkedCoreDisabled.setIntrinsicWidth(attributes.getSize());
    InsetDrawable checkedInsideDisabled = new InsetDrawable(checkedCoreDisabled, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
    Drawable[] checkedDisabledDrawable = { checkedOutsideDisabled, checkedInsideDisabled };
    LayerDrawable checkedDisabled = new LayerDrawable(checkedDisabledDrawable);
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { -android.R.attr.state_checked, android.R.attr.state_enabled }, uncheckedEnabled);
    states.addState(new int[] { android.R.attr.state_checked, android.R.attr.state_enabled }, checkedEnabled);
    states.addState(new int[] { -android.R.attr.state_checked, -android.R.attr.state_enabled }, uncheckedDisabled);
    states.addState(new int[] { android.R.attr.state_checked, -android.R.attr.state_enabled }, checkedDisabled);
    setButtonDrawable(states);
    // setting padding for avoiding text to appear on icon
    setPadding(attributes.getSize() / 4 * 5, 0, 0, 0);
    setTextColor(attributes.getColor(2));
    // check for IDE preview render
    if (!this.isInEditMode()) {
        Typeface typeface = FlatUI.getFont(getContext(), attributes);
        if (typeface != null)
            setTypeface(typeface);
    }
}
Also used : Typeface(android.graphics.Typeface) TypedArray(android.content.res.TypedArray) LayerDrawable(android.graphics.drawable.LayerDrawable) Attributes(com.cengalabs.flatui.Attributes) LayerDrawable(android.graphics.drawable.LayerDrawable) InsetDrawable(android.graphics.drawable.InsetDrawable) Drawable(android.graphics.drawable.Drawable) PaintDrawable(android.graphics.drawable.PaintDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) InsetDrawable(android.graphics.drawable.InsetDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable)

Example 15 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project FlatUI by eluleci.

the class FlatRadioButton method init.

private void init(AttributeSet attrs) {
    if (attributes == null)
        attributes = new Attributes(this, getResources());
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.fl_FlatRadioButton);
        // getting common attributes
        int customTheme = a.getResourceId(R.styleable.fl_FlatRadioButton_fl_theme, Attributes.DEFAULT_THEME);
        attributes.setThemeSilent(customTheme, getResources());
        attributes.setFontFamily(a.getString(R.styleable.fl_FlatRadioButton_fl_fontFamily));
        attributes.setFontWeight(a.getString(R.styleable.fl_FlatRadioButton_fl_fontWeight));
        attributes.setFontExtension(a.getString(R.styleable.fl_FlatRadioButton_fl_fontExtension));
        attributes.setSize(a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_size, Attributes.DEFAULT_SIZE_PX));
        attributes.setRadius(attributes.getSize() / 2);
        attributes.setBorderWidth(a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_borderWidth, Attributes.DEFAULT_BORDER_WIDTH_PX));
        // getting view specific attributes
        dotMargin = a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_dotMargin, dotMargin);
        a.recycle();
    }
    // creating unchecked-enabled state drawable
    GradientDrawable uncheckedEnabled = new GradientDrawable();
    uncheckedEnabled.setCornerRadius(attributes.getRadius());
    uncheckedEnabled.setSize(attributes.getSize(), attributes.getSize());
    uncheckedEnabled.setColor(Color.TRANSPARENT);
    uncheckedEnabled.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
    // creating checked-enabled state drawable
    GradientDrawable checkedOutside = new GradientDrawable();
    checkedOutside.setCornerRadius(attributes.getSize());
    checkedOutside.setSize(attributes.getSize(), attributes.getSize());
    checkedOutside.setColor(Color.TRANSPARENT);
    checkedOutside.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
    PaintDrawable checkedCore = new PaintDrawable(attributes.getColor(2));
    checkedCore.setCornerRadius(attributes.getSize());
    checkedCore.setIntrinsicHeight(attributes.getSize());
    checkedCore.setIntrinsicWidth(attributes.getSize());
    InsetDrawable checkedInside = new InsetDrawable(checkedCore, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
    Drawable[] checkedEnabledDrawable = { checkedOutside, checkedInside };
    LayerDrawable checkedEnabled = new LayerDrawable(checkedEnabledDrawable);
    // creating unchecked-enabled state drawable
    GradientDrawable uncheckedDisabled = new GradientDrawable();
    uncheckedDisabled.setCornerRadius(attributes.getRadius());
    uncheckedDisabled.setSize(attributes.getSize(), attributes.getSize());
    uncheckedDisabled.setColor(Color.TRANSPARENT);
    uncheckedDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
    // creating checked-disabled state drawable
    GradientDrawable checkedOutsideDisabled = new GradientDrawable();
    checkedOutsideDisabled.setCornerRadius(attributes.getRadius());
    checkedOutsideDisabled.setSize(attributes.getSize(), attributes.getSize());
    checkedOutsideDisabled.setColor(Color.TRANSPARENT);
    checkedOutsideDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
    PaintDrawable checkedCoreDisabled = new PaintDrawable(attributes.getColor(3));
    checkedCoreDisabled.setCornerRadius(attributes.getRadius());
    checkedCoreDisabled.setIntrinsicHeight(attributes.getSize());
    checkedCoreDisabled.setIntrinsicWidth(attributes.getSize());
    InsetDrawable checkedInsideDisabled = new InsetDrawable(checkedCoreDisabled, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
    Drawable[] checkedDisabledDrawable = { checkedOutsideDisabled, checkedInsideDisabled };
    LayerDrawable checkedDisabled = new LayerDrawable(checkedDisabledDrawable);
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { -android.R.attr.state_checked, android.R.attr.state_enabled }, uncheckedEnabled);
    states.addState(new int[] { android.R.attr.state_checked, android.R.attr.state_enabled }, checkedEnabled);
    states.addState(new int[] { -android.R.attr.state_checked, -android.R.attr.state_enabled }, uncheckedDisabled);
    states.addState(new int[] { android.R.attr.state_checked, -android.R.attr.state_enabled }, checkedDisabled);
    setButtonDrawable(states);
    // setting padding for avoiding text to be appear on icon
    setPadding(attributes.getSize() / 4 * 5, 0, 0, 0);
    setTextColor(attributes.getColor(2));
    // check for IDE preview render
    if (!this.isInEditMode()) {
        Typeface typeface = FlatUI.getFont(getContext(), attributes);
        if (typeface != null)
            setTypeface(typeface);
    }
}
Also used : Typeface(android.graphics.Typeface) TypedArray(android.content.res.TypedArray) LayerDrawable(android.graphics.drawable.LayerDrawable) Attributes(com.cengalabs.flatui.Attributes) LayerDrawable(android.graphics.drawable.LayerDrawable) InsetDrawable(android.graphics.drawable.InsetDrawable) Drawable(android.graphics.drawable.Drawable) PaintDrawable(android.graphics.drawable.PaintDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) InsetDrawable(android.graphics.drawable.InsetDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable)

Aggregations

StateListDrawable (android.graphics.drawable.StateListDrawable)166 Drawable (android.graphics.drawable.Drawable)43 LayerDrawable (android.graphics.drawable.LayerDrawable)24 BitmapDrawable (android.graphics.drawable.BitmapDrawable)21 GradientDrawable (android.graphics.drawable.GradientDrawable)21 ColorDrawable (android.graphics.drawable.ColorDrawable)17 Bitmap (android.graphics.Bitmap)13 TextView (android.widget.TextView)12 ShapeDrawable (android.graphics.drawable.ShapeDrawable)11 View (android.view.View)11 AnimationDrawable (android.graphics.drawable.AnimationDrawable)9 ClipDrawable (android.graphics.drawable.ClipDrawable)9 SuppressLint (android.annotation.SuppressLint)8 ColorStateList (android.content.res.ColorStateList)8 Paint (android.graphics.Paint)8 RippleDrawable (android.graphics.drawable.RippleDrawable)7 TargetApi (android.annotation.TargetApi)6 TypedArray (android.content.res.TypedArray)6 TextPaint (android.text.TextPaint)5 Button (android.widget.Button)5