Search in sources :

Example 76 with LayerDrawable

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

the class FlatToggleButton 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_FlatToggleButton);
        // getting common attributes
        int customTheme = a.getResourceId(R.styleable.fl_FlatToggleButton_fl_theme, Attributes.DEFAULT_THEME);
        attributes.setThemeSilent(customTheme, getResources());
        attributes.setRadius(a.getDimensionPixelSize(R.styleable.fl_FlatToggleButton_fl_cornerRadius, Attributes.DEFAULT_RADIUS_PX));
        space = a.getDimensionPixelSize(R.styleable.fl_FlatToggleButton_fl_space, space);
        padding = space / 10;
        a.recycle();
    }
    // creating unchecked-enabled state drawable
    ShapeDrawable uncheckedEnabledFrontCore = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    uncheckedEnabledFrontCore.getPaint().setColor(attributes.getColor(2));
    InsetDrawable uncheckedEnabledFront = new InsetDrawable(uncheckedEnabledFrontCore, padding);
    ShapeDrawable uncheckedEnabledBack = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    uncheckedEnabledBack.getPaint().setColor(Color.parseColor("#f2f2f2"));
    uncheckedEnabledBack.setIntrinsicWidth(space / 2 * 5);
    uncheckedEnabledBack.setIntrinsicHeight(space);
    uncheckedEnabledBack.setPadding(0, 0, space / 2 * 5, 0);
    Drawable[] d1 = { uncheckedEnabledBack, uncheckedEnabledFront };
    LayerDrawable uncheckedEnabled = new LayerDrawable(d1);
    // creating checked-enabled state drawable
    ShapeDrawable checkedEnabledFrontCore = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    checkedEnabledFrontCore.getPaint().setColor(attributes.getColor(2));
    InsetDrawable checkedEnabledFront = new InsetDrawable(checkedEnabledFrontCore, padding);
    ShapeDrawable checkedEnabledBack = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    checkedEnabledBack.getPaint().setColor(attributes.getColor(3));
    checkedEnabledBack.setPadding(space / 2 * 5, 0, 0, 0);
    Drawable[] d2 = { checkedEnabledBack, checkedEnabledFront };
    LayerDrawable checkedEnabled = new LayerDrawable(d2);
    // creating unchecked-disabled state drawable
    ShapeDrawable uncheckedDisabledFrontCore = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    uncheckedDisabledFrontCore.getPaint().setColor(Color.parseColor("#d2d2d2"));
    InsetDrawable uncheckedDisabledFront = new InsetDrawable(uncheckedDisabledFrontCore, padding);
    ShapeDrawable uncheckedDisabledBack = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    uncheckedDisabledBack.getPaint().setColor(Color.parseColor("#f2f2f2"));
    uncheckedDisabledBack.setPadding(0, 0, space / 2 * 5, 0);
    Drawable[] d3 = { uncheckedDisabledBack, uncheckedDisabledFront };
    LayerDrawable uncheckedDisabled = new LayerDrawable(d3);
    // creating checked-disabled state drawable
    ShapeDrawable checkedDisabledFrontCore = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    checkedDisabledFrontCore.getPaint().setColor(attributes.getColor(3));
    InsetDrawable checkedDisabledFront = new InsetDrawable(checkedDisabledFrontCore, padding);
    ShapeDrawable checkedDisabledBack = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    checkedDisabledBack.getPaint().setColor(Color.parseColor("#f2f2f2"));
    checkedDisabledBack.setPadding(space / 2 * 5, 0, 0, 0);
    Drawable[] d4 = { checkedDisabledBack, checkedDisabledFront };
    LayerDrawable checkedDisabled = new LayerDrawable(d4);
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { -android.R.attr.state_checked, android.R.attr.state_enabled }, new InsetDrawable(uncheckedEnabled, padding * 2));
    states.addState(new int[] { android.R.attr.state_checked, android.R.attr.state_enabled }, new InsetDrawable(checkedEnabled, padding * 2));
    states.addState(new int[] { -android.R.attr.state_checked, -android.R.attr.state_enabled }, new InsetDrawable(uncheckedDisabled, padding * 2));
    states.addState(new int[] { android.R.attr.state_checked, -android.R.attr.state_enabled }, new InsetDrawable(checkedDisabled, padding * 2));
    setBackgroundDrawable(states);
    setText("");
    setTextOff("");
    setTextOn("");
    setTextSize(0);
}
Also used : RoundRectShape(android.graphics.drawable.shapes.RoundRectShape) TypedArray(android.content.res.TypedArray) LayerDrawable(android.graphics.drawable.LayerDrawable) Attributes(com.cengalabs.flatui.Attributes) LayerDrawable(android.graphics.drawable.LayerDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) InsetDrawable(android.graphics.drawable.InsetDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) InsetDrawable(android.graphics.drawable.InsetDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable)

Example 77 with LayerDrawable

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

the class FlatButton method init.

private void init(AttributeSet attrs) {
    // saving padding values for using them after setting background drawable
    final int paddingTop = getPaddingTop();
    final int paddingRight = getPaddingRight();
    final int paddingLeft = getPaddingLeft();
    final int paddingBottom = getPaddingBottom();
    if (attributes == null)
        attributes = new Attributes(this, getResources());
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.fl_FlatButton);
        // getting common attributes
        int customTheme = a.getResourceId(R.styleable.fl_FlatButton_fl_theme, Attributes.DEFAULT_THEME);
        attributes.setThemeSilent(customTheme, getResources());
        attributes.setTouchEffect(a.getInt(R.styleable.fl_FlatButton_fl_touchEffect, Attributes.DEFAULT_TOUCH_EFFECT));
        attributes.setFontFamily(a.getString(R.styleable.fl_FlatButton_fl_fontFamily));
        attributes.setFontWeight(a.getString(R.styleable.fl_FlatButton_fl_fontWeight));
        attributes.setFontExtension(a.getString(R.styleable.fl_FlatButton_fl_fontExtension));
        attributes.setTextAppearance(a.getInt(R.styleable.fl_FlatButton_fl_textAppearance, Attributes.DEFAULT_TEXT_APPEARANCE));
        attributes.setRadius(a.getDimensionPixelSize(R.styleable.fl_FlatButton_fl_cornerRadius, Attributes.DEFAULT_RADIUS_PX));
        // getting view specific attributes
        bottom = a.getDimensionPixelSize(R.styleable.fl_FlatButton_fl_blockButtonEffectHeight, bottom);
        a.recycle();
    }
    if (attributes.hasTouchEffect()) {
        boolean hasRippleEffect = attributes.getTouchEffect() == Attributes.RIPPLE_TOUCH_EFFECT;
        touchEffectAnimator = new TouchEffectAnimator(this);
        touchEffectAnimator.setHasRippleEffect(hasRippleEffect);
        touchEffectAnimator.setEffectColor(attributes.getColor(1));
        touchEffectAnimator.setClipRadius(attributes.getRadius());
    }
    /*mPaint = new Paint();
        mPaint.setColor(attributes.getColor(1));
        mPaint.setAlpha(mAlpha);*/
    // creating normal state drawable
    ShapeDrawable normalFront = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    normalFront.getPaint().setColor(attributes.getColor(2));
    ShapeDrawable normalBack = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    normalBack.getPaint().setColor(attributes.getColor(1));
    normalBack.setPadding(0, 0, 0, bottom);
    Drawable[] d = { normalBack, normalFront };
    LayerDrawable normal = new LayerDrawable(d);
    // creating pressed state drawable
    ShapeDrawable pressedFront = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    pressedFront.getPaint().setColor(attributes.getColor(1));
    ShapeDrawable pressedBack = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    pressedBack.getPaint().setColor(attributes.getColor(0));
    if (bottom != 0)
        pressedBack.setPadding(0, 0, 0, bottom / 2);
    Drawable[] d2 = { pressedBack, pressedFront };
    LayerDrawable pressed = new LayerDrawable(d2);
    // creating disabled state drawable
    ShapeDrawable disabledFront = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    disabledFront.getPaint().setColor(attributes.getColor(3));
    disabledFront.getPaint().setAlpha(0xA0);
    ShapeDrawable disabledBack = new ShapeDrawable(new RoundRectShape(attributes.getOuterRadius(), null, null));
    disabledBack.getPaint().setColor(attributes.getColor(2));
    Drawable[] d3 = { disabledBack, disabledFront };
    LayerDrawable disabled = new LayerDrawable(d3);
    StateListDrawable states = new StateListDrawable();
    if (!attributes.hasTouchEffect())
        states.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
    states.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_enabled }, pressed);
    states.addState(new int[] { android.R.attr.state_enabled }, normal);
    states.addState(new int[] { -android.R.attr.state_enabled }, disabled);
    setBackgroundDrawable(states);
    setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    if (attributes.getTextAppearance() == 1)
        setTextColor(attributes.getColor(0));
    else if (attributes.getTextAppearance() == 2)
        setTextColor(attributes.getColor(3));
    else
        setTextColor(Color.WHITE);
    // check for IDE preview render
    if (!this.isInEditMode()) {
        Typeface typeface = FlatUI.getFont(getContext(), attributes);
        if (typeface != null)
            setTypeface(typeface);
    }
}
Also used : RoundRectShape(android.graphics.drawable.shapes.RoundRectShape) TouchEffectAnimator(com.cengalabs.flatui.TouchEffectAnimator) Typeface(android.graphics.Typeface) TypedArray(android.content.res.TypedArray) LayerDrawable(android.graphics.drawable.LayerDrawable) Attributes(com.cengalabs.flatui.Attributes) LayerDrawable(android.graphics.drawable.LayerDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable)

Example 78 with LayerDrawable

use of android.graphics.drawable.LayerDrawable in project FloatingActionButton by makovkastar.

the class FloatingActionButton method createDrawable.

private Drawable createDrawable(int color) {
    OvalShape ovalShape = new OvalShape();
    ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
    shapeDrawable.getPaint().setColor(color);
    if (mShadow && !hasLollipopApi()) {
        Drawable shadowDrawable = getResources().getDrawable(mType == TYPE_NORMAL ? R.drawable.fab_shadow : R.drawable.fab_shadow_mini);
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { shadowDrawable, shapeDrawable });
        layerDrawable.setLayerInset(1, mShadowSize, mShadowSize, mShadowSize, mShadowSize);
        return layerDrawable;
    } else {
        return shapeDrawable;
    }
}
Also used : LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) RippleDrawable(android.graphics.drawable.RippleDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) OvalShape(android.graphics.drawable.shapes.OvalShape)

Example 79 with LayerDrawable

use of android.graphics.drawable.LayerDrawable in project android-process-button by dmytrodanylyk.

the class FlatButton method createNormalDrawable.

private LayerDrawable createNormalDrawable(TypedArray attr) {
    LayerDrawable drawableNormal = (LayerDrawable) getDrawable(R.drawable.rect_normal).mutate();
    GradientDrawable drawableTop = (GradientDrawable) drawableNormal.getDrawable(0).mutate();
    drawableTop.setCornerRadius(getCornerRadius());
    int blueDark = getColor(R.color.blue_pressed);
    int colorPressed = attr.getColor(R.styleable.FlatButton_pb_colorPressed, blueDark);
    drawableTop.setColor(colorPressed);
    GradientDrawable drawableBottom = (GradientDrawable) drawableNormal.getDrawable(1).mutate();
    drawableBottom.setCornerRadius(getCornerRadius());
    int blueNormal = getColor(R.color.blue_normal);
    int colorNormal = attr.getColor(R.styleable.FlatButton_pb_colorNormal, blueNormal);
    drawableBottom.setColor(colorNormal);
    return drawableNormal;
}
Also used : LayerDrawable(android.graphics.drawable.LayerDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable) SuppressLint(android.annotation.SuppressLint)

Example 80 with LayerDrawable

use of android.graphics.drawable.LayerDrawable in project UltimateRecyclerView by cymcsg.

the class FloatingActionButton method updateBackground.

protected void updateBackground() {
    float circleLeft = mShadowRadius;
    float circleTop = mShadowRadius - mShadowOffset;
    final RectF circleRect = new RectF(circleLeft, circleTop, circleLeft + mCircleSize, circleTop + mCircleSize);
    final LayerDrawable layerDrawable = generateFinalDrawables(circleRect);
    float iconOffset = (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2f;
    int iconInsetHorizontal = (int) (mShadowRadius + iconOffset);
    int iconInsetTop = (int) (circleTop + iconOffset);
    int iconInsetBottom = (int) (mShadowRadius + mShadowOffset + iconOffset);
    layerDrawable.setLayerInset(layerDrawable.getNumberOfLayers() - 1, iconInsetHorizontal, iconInsetTop, iconInsetHorizontal, iconInsetBottom);
    setBackgroundCompat(layerDrawable);
}
Also used : RectF(android.graphics.RectF) LayerDrawable(android.graphics.drawable.LayerDrawable) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) Paint(android.graphics.Paint)

Aggregations

LayerDrawable (android.graphics.drawable.LayerDrawable)133 Drawable (android.graphics.drawable.Drawable)91 BitmapDrawable (android.graphics.drawable.BitmapDrawable)61 StateListDrawable (android.graphics.drawable.StateListDrawable)45 AnimationDrawable (android.graphics.drawable.AnimationDrawable)42 ClipDrawable (android.graphics.drawable.ClipDrawable)42 ShapeDrawable (android.graphics.drawable.ShapeDrawable)39 Paint (android.graphics.Paint)25 SuppressLint (android.annotation.SuppressLint)21 GradientDrawable (android.graphics.drawable.GradientDrawable)19 Bitmap (android.graphics.Bitmap)14 ColorDrawable (android.graphics.drawable.ColorDrawable)14 BitmapShader (android.graphics.BitmapShader)13 AnimatedVectorDrawable (android.graphics.drawable.AnimatedVectorDrawable)8 OvalShape (android.graphics.drawable.shapes.OvalShape)8 TypedArray (android.content.res.TypedArray)7 Nullable (android.annotation.Nullable)5 Resources (android.content.res.Resources)5 InsetDrawable (android.graphics.drawable.InsetDrawable)5 TransitionDrawable (android.graphics.drawable.TransitionDrawable)5