Search in sources :

Example 6 with Attributes

use of com.cengalabs.flatui.Attributes in project FlatUI by eluleci.

the class FlatEditText method init.

private void init(AttributeSet attrs) {
    if (attributes == null)
        attributes = new Attributes(this, getResources());
    if (attrs != null) {
        // getting android default tags for textColor and textColorHint
        hasOwnTextColor = attrs.getAttributeValue(FlatUI.androidStyleNameSpace, "textColor") != null;
        hasOwnHintColor = attrs.getAttributeValue(FlatUI.androidStyleNameSpace, "textColorHint") != null;
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.fl_FlatEditText);
        // getting common attributes
        int customTheme = a.getResourceId(R.styleable.fl_FlatEditText_fl_theme, Attributes.DEFAULT_THEME);
        attributes.setThemeSilent(customTheme, getResources());
        attributes.setFontFamily(a.getString(R.styleable.fl_FlatEditText_fl_fontFamily));
        attributes.setFontWeight(a.getString(R.styleable.fl_FlatEditText_fl_fontWeight));
        attributes.setFontExtension(a.getString(R.styleable.fl_FlatEditText_fl_fontExtension));
        attributes.setTextAppearance(a.getInt(R.styleable.fl_FlatEditText_fl_textAppearance, Attributes.DEFAULT_TEXT_APPEARANCE));
        attributes.setRadius(a.getDimensionPixelSize(R.styleable.fl_FlatEditText_fl_cornerRadius, Attributes.DEFAULT_RADIUS_PX));
        attributes.setBorderWidth(a.getDimensionPixelSize(R.styleable.fl_FlatEditText_fl_borderWidth, Attributes.DEFAULT_BORDER_WIDTH_PX));
        // getting view specific attributes
        style = a.getInt(R.styleable.fl_FlatEditText_fl_fieldStyle, 0);
        a.recycle();
    }
    GradientDrawable backgroundDrawable = new GradientDrawable();
    backgroundDrawable.setCornerRadius(attributes.getRadius());
    if (style == 0) {
        // flat
        if (!hasOwnTextColor)
            setTextColor(attributes.getColor(3));
        backgroundDrawable.setColor(attributes.getColor(2));
        backgroundDrawable.setStroke(0, attributes.getColor(2));
    } else if (style == 1) {
        // box
        if (!hasOwnTextColor)
            setTextColor(attributes.getColor(2));
        backgroundDrawable.setColor(Color.WHITE);
        backgroundDrawable.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
    } else if (style == 2) {
        // transparent
        if (!hasOwnTextColor)
            setTextColor(attributes.getColor(1));
        backgroundDrawable.setColor(Color.TRANSPARENT);
        backgroundDrawable.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
    }
    setBackgroundDrawable(backgroundDrawable);
    if (!hasOwnHintColor)
        setHintTextColor(attributes.getColor(3));
    if (attributes.getTextAppearance() == 1)
        setTextColor(attributes.getColor(0));
    else if (attributes.getTextAppearance() == 2)
        setTextColor(attributes.getColor(3));
    // 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) Attributes(com.cengalabs.flatui.Attributes) GradientDrawable(android.graphics.drawable.GradientDrawable)

Example 7 with Attributes

use of com.cengalabs.flatui.Attributes 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 8 with Attributes

use of com.cengalabs.flatui.Attributes 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)

Aggregations

TypedArray (android.content.res.TypedArray)8 Attributes (com.cengalabs.flatui.Attributes)8 Typeface (android.graphics.Typeface)6 GradientDrawable (android.graphics.drawable.GradientDrawable)5 LayerDrawable (android.graphics.drawable.LayerDrawable)5 Drawable (android.graphics.drawable.Drawable)4 StateListDrawable (android.graphics.drawable.StateListDrawable)4 InsetDrawable (android.graphics.drawable.InsetDrawable)3 PaintDrawable (android.graphics.drawable.PaintDrawable)3 ShapeDrawable (android.graphics.drawable.ShapeDrawable)2 RoundRectShape (android.graphics.drawable.shapes.RoundRectShape)2 ClipDrawable (android.graphics.drawable.ClipDrawable)1 TouchEffectAnimator (com.cengalabs.flatui.TouchEffectAnimator)1