工作中經常會用到圓角的按鈕,另外點擊還需要有按下的效果。機智的你是否已經厭煩了有時甚至為了改變顏色而不停的寫drawable文件。于是我想寫一個自定義的View實現(xiàn)這個效果以后項目中也能直接用上。廢話不多說直接上源碼
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);
}
}