Search in sources :

Example 56 with ShapeDrawable

use of android.graphics.drawable.ShapeDrawable in project RecyclerRefreshLayout by dinuscxj.

the class MaterialRefreshView method initBackground.

private void initBackground(float radius) {
    final float density = getContext().getResources().getDisplayMetrics().density;
    final int diameter = (int) (radius * density * 2);
    final int shadowYOffset = (int) (density * Y_OFFSET);
    final int shadowXOffset = (int) (density * X_OFFSET);
    final int color = Color.parseColor("#FFFAFAFA");
    mShadowRadius = (int) (density * SHADOW_RADIUS);
    ShapeDrawable circle;
    if (elevationSupported()) {
        circle = new ShapeDrawable(new OvalShape());
        ViewCompat.setElevation(this, SHADOW_ELEVATION * density);
    } else {
        OvalShape oval = new OvalShadow(mShadowRadius, diameter);
        circle = new ShapeDrawable(oval);
        ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, circle.getPaint());
        circle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset, KEY_SHADOW_COLOR);
        final int padding = mShadowRadius;
        // set padding so the inner image sits correctly within the shadow.
        setPadding(padding, padding, padding, padding);
    }
    circle.getPaint().setColor(color);
    setBackgroundDrawable(circle);
}
Also used : ShapeDrawable(android.graphics.drawable.ShapeDrawable) OvalShape(android.graphics.drawable.shapes.OvalShape) Paint(android.graphics.Paint)

Example 57 with ShapeDrawable

use of android.graphics.drawable.ShapeDrawable 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 58 with ShapeDrawable

use of android.graphics.drawable.ShapeDrawable 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 59 with ShapeDrawable

use of android.graphics.drawable.ShapeDrawable 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 60 with ShapeDrawable

use of android.graphics.drawable.ShapeDrawable in project cw-omnibus by commonsguy.

the class IcsProgressBar 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) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];
        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i), (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }
        LayerDrawable newBg = new LayerDrawable(outDrawables);
        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }
        return newBg;
    } else /* else if (drawable instanceof StateListDrawable) {
            StateListDrawable in = (StateListDrawable) drawable;
            StateListDrawable out = new StateListDrawable();
            int numStates = in.getStateCount();
            for (int i = 0; i < numStates; i++) {
                out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
            }
            return out;

        }*/
    if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }
        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
    }
    return drawable;
}
Also used : Bitmap(android.graphics.Bitmap) LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) AnimationDrawable(android.graphics.drawable.AnimationDrawable) Drawable(android.graphics.drawable.Drawable) ClipDrawable(android.graphics.drawable.ClipDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BitmapShader(android.graphics.BitmapShader) ClipDrawable(android.graphics.drawable.ClipDrawable)

Aggregations

ShapeDrawable (android.graphics.drawable.ShapeDrawable)96 OvalShape (android.graphics.drawable.shapes.OvalShape)44 Paint (android.graphics.Paint)32 Drawable (android.graphics.drawable.Drawable)24 LayerDrawable (android.graphics.drawable.LayerDrawable)24 StateListDrawable (android.graphics.drawable.StateListDrawable)17 SuppressLint (android.annotation.SuppressLint)16 ClipDrawable (android.graphics.drawable.ClipDrawable)16 Bitmap (android.graphics.Bitmap)15 BitmapDrawable (android.graphics.drawable.BitmapDrawable)15 BitmapShader (android.graphics.BitmapShader)14 RoundRectShape (android.graphics.drawable.shapes.RoundRectShape)14 AnimationDrawable (android.graphics.drawable.AnimationDrawable)13 ColorDrawable (android.graphics.drawable.ColorDrawable)9 RectShape (android.graphics.drawable.shapes.RectShape)8 Shape (android.graphics.drawable.shapes.Shape)7 Canvas (android.graphics.Canvas)6 Resources (android.content.res.Resources)5 Shader (android.graphics.Shader)5 TypedArray (android.content.res.TypedArray)4