Java Code Examples for android.graphics.drawable.ShapeDrawable#setPadding()

The following examples show how to use android.graphics.drawable.ShapeDrawable#setPadding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColorPickerAdapter.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
private void buildColorPickerView(View view, int colorCode) {
    view.setVisibility(View.VISIBLE);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(20);
    biggerCircle.setIntrinsicWidth(20);
    biggerCircle.setBounds(new Rect(0, 0, 20, 20));
    biggerCircle.getPaint().setColor(colorCode);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(5);
    smallerCircle.setIntrinsicWidth(5);
    smallerCircle.setBounds(new Rect(0, 0, 5, 5));
    smallerCircle.getPaint().setColor(Color.WHITE);
    smallerCircle.setPadding(10, 10, 10, 10);
    Drawable[] drawables = {smallerCircle, biggerCircle};

    LayerDrawable layerDrawable = new LayerDrawable(drawables);

    view.setBackgroundDrawable(layerDrawable);
}
 
Example 2
Source File: ColorPickerAdapter.java    From PhotoEditor with MIT License 6 votes vote down vote up
private void buildColorPickerView(View view, int colorCode) {
    view.setVisibility(View.VISIBLE);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(20);
    biggerCircle.setIntrinsicWidth(20);
    biggerCircle.setBounds(new Rect(0, 0, 20, 20));
    biggerCircle.getPaint().setColor(colorCode);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(5);
    smallerCircle.setIntrinsicWidth(5);
    smallerCircle.setBounds(new Rect(0, 0, 5, 5));
    smallerCircle.getPaint().setColor(Color.WHITE);
    smallerCircle.setPadding(10, 10, 10, 10);
    Drawable[] drawables = {smallerCircle, biggerCircle};

    LayerDrawable layerDrawable = new LayerDrawable(drawables);

    view.setBackgroundDrawable(layerDrawable);
}
 
Example 3
Source File: ColorPickerAdapter.java    From react-native-photo-editor with Apache License 2.0 6 votes vote down vote up
private void buildColorPickerView(View view, int colorCode) {
    view.setVisibility(View.VISIBLE);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(20);
    biggerCircle.setIntrinsicWidth(20);
    biggerCircle.setBounds(new Rect(0, 0, 20, 20));
    biggerCircle.getPaint().setColor(colorCode);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(5);
    smallerCircle.setIntrinsicWidth(5);
    smallerCircle.setBounds(new Rect(0, 0, 5, 5));
    smallerCircle.getPaint().setColor(Color.WHITE);
    smallerCircle.setPadding(10, 10, 10, 10);
    Drawable[] drawables = {smallerCircle, biggerCircle};

    LayerDrawable layerDrawable = new LayerDrawable(drawables);

    view.setBackgroundDrawable(layerDrawable);
}
 
Example 4
Source File: ColorPickerAdapter.java    From photo-editor-android with MIT License 6 votes vote down vote up
private void buildColorPickerView(View view, int colorCode) {
    view.setVisibility(View.VISIBLE);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(20);
    biggerCircle.setIntrinsicWidth(20);
    biggerCircle.setBounds(new Rect(0, 0, 20, 20));
    biggerCircle.getPaint().setColor(colorCode);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(5);
    smallerCircle.setIntrinsicWidth(5);
    smallerCircle.setBounds(new Rect(0, 0, 5, 5));
    smallerCircle.getPaint().setColor(Color.WHITE);
    smallerCircle.setPadding(10, 10, 10, 10);
    Drawable[] drawables = {smallerCircle, biggerCircle};

    LayerDrawable layerDrawable = new LayerDrawable(drawables);

    view.setBackgroundDrawable(layerDrawable);
}
 
Example 5
Source File: SUtils.java    From SublimePicker with Apache License 2.0 6 votes vote down vote up
private static Drawable createButtonShape(Context context, int color) {
    // Translation of Lollipop's xml button-bg definition to Java
    int paddingH = context.getResources()
            .getDimensionPixelSize(R.dimen.button_padding_horizontal_material);
    int paddingV = context.getResources()
            .getDimensionPixelSize(R.dimen.button_padding_vertical_material);
    int insetH = context.getResources()
            .getDimensionPixelSize(R.dimen.button_inset_horizontal_material);
    int insetV = context.getResources()
            .getDimensionPixelSize(R.dimen.button_inset_vertical_material);

    float[] outerRadii = new float[8];
    Arrays.fill(outerRadii, CORNER_RADIUS);

    RoundRectShape r = new RoundRectShape(outerRadii, null, null);

    ShapeDrawable shapeDrawable = new ShapeDrawable(r);
    shapeDrawable.getPaint().setColor(color);
    shapeDrawable.setPadding(paddingH, paddingV, paddingH, paddingV);

    return new InsetDrawable(shapeDrawable,
            insetH, insetV, insetH, insetV);
}
 
Example 6
Source File: SweetToast.java    From SweetTips with Apache License 2.0 5 votes vote down vote up
/**
 * 根据指定的背景色,获得当前SweetToast实例中mContentView的背景drawable实例
 * @param backgroundColor
 * @return
 */
private static Drawable getBackgroundDrawable(SweetToast sweetToast, @ColorInt int backgroundColor){
    try {
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        //获取当前设备的屏幕尺寸
        //实验发现不同的设备上面,Toast内容区域的padding值并不相同,根据屏幕的宽度分别进行处理,尽量接近设备原生Toast的体验
        int widthPixels = sweetToast.getContentView().getResources().getDisplayMetrics().widthPixels;
        int heightPixels = sweetToast.getContentView().getResources().getDisplayMetrics().heightPixels;
        float density = sweetToast.getContentView().getResources().getDisplayMetrics().density;
        if(widthPixels >= 1070){
            //例如小米5S:1920 x 1080
            shapeDrawable.setPadding((int)(density*13),(int)(density*12),(int)(density*13),(int)(density*12));
        }else {
            //例如红米2:1280x720
            shapeDrawable.setPadding((int)(density*14),(int)(density*13),(int)(density*14),(int)(density*13));
        }
        float radius = density*8;
        float[] outerRadii = new float[]{radius,radius,radius,radius,radius,radius,radius,radius};
        int width = sweetToast.getContentView().getWidth();
        int height = sweetToast.getContentView().getHeight();
        RectF rectF = new RectF(1,1,width-1,height-1);
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii,rectF,null);
        shapeDrawable.setShape(roundRectShape);
        //在Android 5.0以下,直接设置 DrawableCompat.setTint 未变色:DrawableCompat.setTint(shapeDrawable,backgroundColor);

        //解决:不使用DrawableCompat,直接使用 Drawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP),
        //经测试,在4.22(山寨机)和5.1(中兴)和6.0.1(小米5s)上颜色正常显示
        shapeDrawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
        return shapeDrawable;
    }catch (Exception e){
        Log.e("幻海流心","e:"+e.getLocalizedMessage());
    }
    return null;
}
 
Example 7
Source File: PointView.java    From AdPlayBanner with Apache License 2.0 5 votes vote down vote up
private void change() {
    float[] outerR = new float[]{mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2};
    Shape shape = new RoundRectShape(outerR, null, null);
    ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
    shapeDrawable.setIntrinsicHeight((int) mSize);
    shapeDrawable.setIntrinsicWidth((int) mSize);
    shapeDrawable.setPadding(0, 0, 0, 0);
    shapeDrawable.getPaint().setColor(mColor);
    shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
    setBackgroundDrawable(shapeDrawable);
    setHeight((int) mSize);
    setMinWidth((int) mSize);
}
 
Example 8
Source File: ExampleListActivity.java    From UILibrary with MIT License 5 votes vote down vote up
private void addArcShapeToList(){
    final ArcShape arcShape = new ArcShape(0, 0, 0);
    final ShapeDrawable shapeDrawable = new ShapeDrawable(arcShape);
    shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
    shapeDrawable.getPaint().setColor(0xFF0099CC);
    shapeDrawable.setPadding(30, 30, 30, 30);

    mExampleItemList.add(new ExampleItem("ArcShape", new ExampleDetailActivity.DetailContentProvider() {
        @Override
        public Object provideInstance() {
            return shapeDrawable;
        }

        @Override
        public void onGestureMove(float x, float y, int action) {
            if(x >= 0 && x <= 1){
                arcShape.setEndAngle(360 * x);
                shapeDrawable.invalidateSelf();
            }

            if(y >= 0 && y <= 1){
                arcShape.setStrokeWidth((int) (y * 100));
                shapeDrawable.invalidateSelf();
            }
        }

        @Override
        public void destroy() {

        }
    }));
}
 
Example 9
Source File: SweetToast.java    From SweetTips with Apache License 2.0 5 votes vote down vote up
/**
 * 根据指定的背景色,获得当前SweetToast实例中mContentView的背景drawable实例
 * @param backgroundColor
 * @return
 */
private static Drawable getBackgroundDrawable(SweetToast sweetToast, @ColorInt int backgroundColor){
    try {
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        //获取当前设备的屏幕尺寸
        //实验发现不同的设备上面,Toast内容区域的padding值并不相同,根据屏幕的宽度分别进行处理,尽量接近设备原生Toast的体验
        int widthPixels = sweetToast.getContentView().getResources().getDisplayMetrics().widthPixels;
        int heightPixels = sweetToast.getContentView().getResources().getDisplayMetrics().heightPixels;
        float density = sweetToast.getContentView().getResources().getDisplayMetrics().density;
        if(widthPixels >= 1070){
            //例如小米5S:1920 x 1080
            shapeDrawable.setPadding((int)(density*13),(int)(density*12),(int)(density*13),(int)(density*12));
        }else {
            //例如红米2:1280x720
            shapeDrawable.setPadding((int)(density*14),(int)(density*13),(int)(density*14),(int)(density*13));
        }
        float radius = density*8;
        float[] outerRadii = new float[]{radius,radius,radius,radius,radius,radius,radius,radius};
        int width = sweetToast.getContentView().getWidth();
        int height = sweetToast.getContentView().getHeight();
        RectF rectF = new RectF(1,1,width-1,height-1);
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii,rectF,null);
        shapeDrawable.setShape(roundRectShape);
        //在Android 5.0以下,直接设置 DrawableCompat.setTint 未变色:DrawableCompat.setTint(shapeDrawable,backgroundColor);

        //解决:不使用DrawableCompat,直接使用 Drawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP),
        //经测试,在4.22(山寨机)和5.1(中兴)和6.0.1(小米5s)上颜色正常显示
        shapeDrawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
        return shapeDrawable;
    }catch (Exception e){
        Log.e("幻海流心","e:"+e.getLocalizedMessage());
    }
    return null;
}
 
Example 10
Source File: iOSVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
protected static Drawable makeBackground(Resources res, final int color) {
    int padding = res.getDimensionPixelSize(R.dimen.activity_horizontal_margin);
    float corners = (3 * padding) / 4;
    float[] radii = new float[] { corners, corners, corners, corners, corners, corners, corners, corners };
    ShapeDrawable rect = new ShapeDrawable(new RoundRectShape(radii, null, null));
    rect.setPadding(padding / 4, padding / 4, padding / 4, padding / 4);
    rect.getPaint().setColor(color);
    return rect;
}
 
Example 11
Source File: iOSVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
protected static Drawable makeBackground(Resources res, final int color) {
    int padding = res.getDimensionPixelSize(R.dimen.activity_horizontal_margin);
    float corners = (3 * padding) / 4;
    float[] radii = new float[] { corners, corners, corners, corners, corners, corners, corners, corners };
    ShapeDrawable rect = new ShapeDrawable(new RoundRectShape(radii, null, null));
    rect.setPadding(padding / 4, padding / 4, padding / 4, padding / 4);
    rect.getPaint().setColor(color);
    return rect;
}
 
Example 12
Source File: ShadowUtils.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
public static Drawable generateBackgroundWithShadow(View view, @ColorRes int backgroundColor,
                                                    @DimenRes int cornerRadius,
                                                    @ColorRes int shadowColor,
                                                    @DimenRes int elevation,
                                                    int shadowGravity) {

  float cornerRadiusValue = view.getContext().getResources().getDimension(cornerRadius);
  int elevationValue = (int) view.getContext().getResources().getDimension(elevation);
  int shadowColorValue = ContextCompat.getColor(view.getContext(), shadowColor);
  int backgroundColorValue = ContextCompat.getColor(view.getContext(), backgroundColor);

  float[] outerRadius = {cornerRadiusValue, cornerRadiusValue, cornerRadiusValue,
    cornerRadiusValue, cornerRadiusValue, cornerRadiusValue, cornerRadiusValue,
    cornerRadiusValue};

  Rect shapeDrawablePadding = new Rect();
  shapeDrawablePadding.left = 0;
  shapeDrawablePadding.right = 0;

  int DY = 0;
  int DX = 0;

  switch (shadowGravity) {
    case Gravity.CENTER:
      shapeDrawablePadding.top = elevationValue;
      shapeDrawablePadding.bottom = elevationValue;
      DY = 0;
      break;

    case Gravity.RIGHT:
      shapeDrawablePadding.right = elevationValue * 2;
      shapeDrawablePadding.bottom = elevationValue * 2;
      DY = elevationValue / 3;
      DX = elevationValue / 3;
      break;

    case Gravity.TOP:
      shapeDrawablePadding.top = elevationValue * 2;
      shapeDrawablePadding.bottom = elevationValue;
      DY = -1 * elevationValue / 3;
      break;
    case Gravity.BOTTOM:
      shapeDrawablePadding.top = elevationValue;
      shapeDrawablePadding.bottom = elevationValue * 2;
      DY = elevationValue / 3;
      break;
  }

  ShapeDrawable shapeDrawable = new ShapeDrawable();
  shapeDrawable.setPadding(shapeDrawablePadding);

  shapeDrawable.getPaint().setColor(backgroundColorValue);
  shapeDrawable.getPaint().setShadowLayer(cornerRadiusValue / 3, DX, DY, shadowColorValue);

  view.setLayerType(LAYER_TYPE_SOFTWARE, shapeDrawable.getPaint());

  shapeDrawable.setShape(new RoundRectShape(outerRadius, null, null));

  LayerDrawable drawable = new LayerDrawable(new Drawable[]{shapeDrawable});
  drawable.setLayerInset(0, 0, 0, 0, elevationValue * 2);

  return drawable;

}
 
Example 13
Source File: FlatButton.java    From FamilyChat with Apache License 2.0 4 votes vote down vote up
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);
        }
    }
 
Example 14
Source File: FlatToggleButton.java    From FamilyChat with Apache License 2.0 4 votes vote down vote up
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);
    }
 
Example 15
Source File: FlatButton.java    From GreenDamFileExploere with Apache License 2.0 4 votes vote down vote up
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.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();
        }

        // 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));

        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();

        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);
        }
    }
 
Example 16
Source File: FlatToggleButton.java    From GreenDamFileExploere with Apache License 2.0 4 votes vote down vote up
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);
    }