工作中经常会用到圆角的按钮,另外点击还需要有按下的效果。机智的你是否已经厌烦了有时甚至为了改变颜色而不停的写drawable文件。于是我想写一个自定义的View实现这个效果以后项目中也能直接用上。废话不多说直接上源码
public class RoundClickButton extends View {
private static final String TAG = RoundClickButton.class.getSimpleName();
private static final int DEFAULT_TEXT_PADDING = 40;
private Paint paint;
//平常时的颜色
private int normalColor;
//点击后的颜色
private int pressedColor;
//圆角的半径
private float roundRadius;
//按钮显示的内容
private String buttonText;
//设置字体大小
private int buttonTextSize;
private float buttonWidth;
private float buttonHeight;
private float buttonPadding;
public RoundClickButton(Context context) {
super(context);
}
public RoundClickButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundClickButton);
normalColor = array.getColor(R.styleable.RoundClickButton_buttonNormalColor, Color.WHITE);
pressedColor = array.getColor(R.styleable.RoundClickButton_buttonPressedColor, Color.RED);
roundRadius = array.getDimension(R.styleable.RoundClickButton_buttonRoundRadius, 5.0f);
buttonText = array.getString(R.styleable.RoundClickButton_buttonText);
buttonWidth = array.getDimension(R.styleable.RoundClickButton_buttonWidth, 0);
buttonHeight = array.getDimension(R.styleable.RoundClickButton_buttonHeight, 0);
buttonTextSize = array.getInt(R.styleable.RoundClickButton_buttonTextSize, 16);
buttonPadding = array.getDimension(R.styleable.RoundClickButton_buttonPadding, 5);
paint = new Paint();
paint.setColor(normalColor);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(2);
paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, buttonTextSize, context.getResources().getDisplayMetrics()));
textView = new TextView(context);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, buttonTextSize);
buttonWidth = textView.getPaint().measureText(buttonText) + DEFAULT_TEXT_PADDING * 2;
Log.e(TAG, "w = " + buttonWidth); }
TextView textView;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Paint.FontMetrics fm = paint.getFontMetrics();
buttonHeight = (float) (Math.abs(Math.ceil(fm.descent + fm.top)) + buttonPadding * 2);
buttonHeight = buttonHeight > DEFAULT_TEXT_PADDING ? buttonHeight : DEFAULT_TEXT_PADDING;
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY)
buttonWidth = MeasureSpec.getSize(widthMeasureSpec);
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY)
buttonHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension((int) buttonWidth, (int) buttonHeight); }
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rectF = new RectF(0, 0, buttonWidth, buttonHeight);
canvas.drawRoundRect(rectF, roundRadius, roundRadius, paint);
Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
int baseLine = (int) ((rectF.bottom + rectF.top - fontMetricsInt.bottom - fontMetricsInt.top) / 2); paint.setTextAlign(Paint.Align.CENTER);
paint.setColor(Color.WHITE);
canvas.drawText(buttonText, rectF.centerX(), baseLine, paint); }
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
paint.setColor(pressedColor);
postInvalidate();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
paint.setColor(normalColor);
postInvalidate();
break;
}
return super.onTouchEvent(event);
}
}