Skip to content

Instantly share code, notes, and snippets.

@daolq3012
Created November 9, 2016 01:47
Show Gist options
  • Save daolq3012/c3ab28f4e82309392b44dc3f99cd5c85 to your computer and use it in GitHub Desktop.
Save daolq3012/c3ab28f4e82309392b44dc3f99cd5c85 to your computer and use it in GitHub Desktop.
public class CornerButton extends TextViewCustomFont {
GradientDrawable mDrawable;
private int mBgColor = Color.WHITE;
private int mBgColorSelected = Color.WHITE;
private int mBgColorDisabled = Color.LTGRAY;
private boolean mSelected = false;
private int mCornerRadius = 0;
private int mStrokeWidth = 0;
private int mStrokeColor = Color.TRANSPARENT;
private Drawable mBackgroundDrawable;
public CornerButton(Context context) {
super(context);
init(null);
}
public CornerButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CornerButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
mDrawable = new GradientDrawable();
// fetch attributes
TypedArray a = getContext().getTheme()
.obtainStyledAttributes(attrs, R.styleable.CornerButton, 0, 0);
try {
mBgColor = a.getColor(R.styleable.CornerButton_bgColor, mBgColor);
mBgColorSelected = BitmapUtils.addColor(mBgColor, 0.85f);
mBgColorDisabled = BitmapUtils.addAlpha(mBgColor, 0.65f);
mBgColorSelected =
a.getColor(R.styleable.CornerButton_bgColorSelected, mBgColorSelected);
mBgColorDisabled =
a.getColor(R.styleable.CornerButton_bgColorDisabled, mBgColorDisabled);
mCornerRadius = a.getDimensionPixelSize(R.styleable.CornerButton_corner, mCornerRadius);
mSelected = a.getBoolean(R.styleable.CornerButton_selected, mSelected);
mStrokeWidth = a.getDimensionPixelSize(R.styleable.CornerButton_cornerStrokeWidth,
mStrokeWidth);
mStrokeColor = a.getColor(R.styleable.CornerButton_cornerStrokeColor, mStrokeColor);
mBackgroundDrawable = a.getDrawable(R.styleable.CornerButton_bgDrawable);
} finally {
a.recycle();
}
setClickable(true);
refresh();
}
public void setBackgroundColor(int color) {
setBackgroundColor(color, color);
}
public void setBackgroundColor(int color, int colorSelected) {
this.mBgColor = color;
this.mBgColorSelected = colorSelected;
refresh();
}
public void setCorner(int corner) {
this.mCornerRadius = corner;
refresh();
}
@Override
public void setSelected(boolean selected) {
super.setSelected(selected);
this.mSelected = selected;
refresh();
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
refresh();
}
private void refresh() {
if (!isEnabled()) {
mDrawable.setColor(mBgColorDisabled);
setBackground(mDrawable);
} else if (mBackgroundDrawable != null) {
setBackground(mBackgroundDrawable);
} else {
mDrawable.setColor(mSelected ? mBgColorSelected : mBgColor);
mDrawable.setCornerRadius(mCornerRadius);
mDrawable.setStroke(mStrokeWidth, mStrokeColor);
mDrawable.setShape(GradientDrawable.RECTANGLE);
setBackground(mDrawable);
}
invalidate();
}
@Override
public void setPressed(boolean pressed) {
super.setPressed(pressed);
this.mSelected = pressed;
refresh();
}
}
@daolq3012
Copy link
Author

<!-- Corner button -->
    <declare-styleable name="CornerButton">
        <attr format="color" name="bgColor"/>
        <attr format="color" name="bgColorSelected"/>
        <attr format="color" name="bgColorDisabled"/>
        <attr format="dimension" name="corner"/>
        <attr format="boolean" name="selected"/>
        <attr format="dimension" name="cornerStrokeWidth"/>
        <attr format="color" name="cornerStrokeColor"/>
        <attr format="integer" name="bgDrawable"/>
    </declare-styleable>

@daolq3012
Copy link
Author

public class StringUtils {

    private StringUtils() {
        // No-op
    }

    public static boolean isBlank(String str) {
        return str == null || TextUtils.isEmpty(str.trim()) || str.equalsIgnoreCase("null");
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static String getString(Context context, @StringRes int stringId) {
        return context.getResources().getString(stringId);
    }

    public static String trim(String input) {
        if (TextUtils.isEmpty(input)) {
            return input;
        }
        String output = input.trim();
        while (output.startsWith("\u3000")) {
            output = output.substring(1, output.length());
        }

        while (output.endsWith("\u3000")) {
            output = output.substring(0, output.length() - 1);
        }

        return output;
    }
}

@daolq3012
Copy link
Author

/**
     * Transform color to darker or lighter
     *
     * @param color input color
     * @param factor < 1: darker, > 1: lighter
     */
    public static int addColor(int color, float factor) {
        int a = Color.alpha(color);
        int r = Math.round(Color.red(color) * factor);
        int g = Math.round(Color.green(color) * factor);
        int b = Math.round(Color.blue(color) * factor);
        return Color.argb(a, Math.min(r, 255), Math.min(g, 255), Math.min(b, 255));
    }

    /**
     * Transform color to darker or lighter (alpha)
     *
     * @param color input color
     * @param factor < 1: opacity, > 1: transparent
     */
    public static int addAlpha(int color, float factor) {
        int a = Math.round(Color.alpha(color) * factor);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        return Color.argb(Math.min(a, 255), r, g, b);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment