自定义设置圆角大小的圆角按钮
来源:互联网 发布:电子病历软件 免费 编辑:程序博客网 时间:2024/05/16 04:47
【由于本人是第一次写博客,所以就直接上代码,不罗嗦!!!】
1.效果图(单机时可设置动画)
2.属性
lq:roundSize="30" //设置圆角大小,默认是16lq:backgroud="@color/colorPrimary" //设置背景lq:text="圆角按钮2" //设置文本lq:textSize="18sp"//设置文本大小
lq:textColor="@color/colorPrimaryDark" //设置文本颜色
3.布局中的使用<com.beacon.constomdemo.view.RoundButton android:id="@+id/roundBtn" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" lq:backgroud="@color/colorAccent" lq:text="圆角按钮1" lq:textSize="18sp"/><com.beacon.constomdemo.view.RoundButton android:id="@+id/roundBtn2" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" lq:roundSize="30" lq:backgroud="@color/colorPrimary" lq:text="圆角按钮2" lq:textSize="18sp"/>
4.Activity中的使用
//圆角按钮1 默认单机效果透明动画RoundButton roundButton = (RoundButton) findViewById(R.id.roundBtn);roundButton.setOnRoundBtnClickListener(new RoundButton.OnRoundBtnClickListener() { @Override public void onClick(String content) { Toast.makeText(MainActivity.this,content,Toast.LENGTH_SHORT).show(); }});//圆角按钮2 单机效果摇头动画RoundButton roundButton2 = (RoundButton) findViewById(R.id.roundBtn2);roundButton2.setOnRoundBtnClickListener(new RoundButton.OnRoundBtnClickListener() { @Override public void onClick(String content) { Toast.makeText(MainActivity.this,content,Toast.LENGTH_SHORT).show(); }},RoundButton.SWING_ANIMATION);
5.源码分享
1)java代码
/** * Created by 刘强lq * <p> * 圆角按钮 */public class RoundButton extends View { //设置摇头动画 public static final int SWING_ANIMATION = 1; //设置透明动画 public static final int ALPHA_ANIMATION = 2; //画笔 private Paint mPaint; //文本 private String mText; //文本颜色 private int mTextColor = Color.WHITE; //文本大小 private int mTextSize; //控件的宽度 private int mWidth; //控件的高度 private int mHeight; //背景 private int mBackgroud = Color.BLACK; //圆角的角度 private float mRoundSize = 16; //矩形 private Rect mTextbounds; private int mAnomationMode = ALPHA_ANIMATION; //监听事件的接口 private OnRoundBtnClickListener mOnRoundBtnClickListener; public void setOnRoundBtnClickListener(OnRoundBtnClickListener onRoundBtnClickListener) { mOnRoundBtnClickListener = onRoundBtnClickListener; } public void setOnRoundBtnClickListener(OnRoundBtnClickListener onRoundBtnClickListener, int anomationMode) { mOnRoundBtnClickListener = onRoundBtnClickListener; this.mAnomationMode = anomationMode; } public RoundButton(Context context) { this(context, null); } public RoundButton(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public RoundButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundButton, defStyleAttr, 0); int indexCount = typedArray.getIndexCount(); for (int i = 0; i < indexCount; i++) { int index = typedArray.getIndex(i); switch (index) { case R.styleable.RoundButton_text: mText = typedArray.getString(index); break; case R.styleable.RoundButton_textSize: mTextSize = typedArray.getDimensionPixelSize(index, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); break; case R.styleable.RoundButton_textColor: mTextColor = typedArray.getColor(index, Color.WHITE); break; case R.styleable.RoundButton_backgroud: mBackgroud = typedArray.getColor(index, Color.BLACK); break; case R.styleable.RoundButton_roundSize: mRoundSize = typedArray.getFloat(index, 16); break; default: break; } } typedArray.recycle(); init(); this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnRoundBtnClickListener != null) { mOnRoundBtnClickListener.onClick(mText); if (mAnomationMode == ALPHA_ANIMATION) { onClickAnimation(); } else { onClickAnimation2(); } } } }); } /** * 初始化 */ private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextSize(mTextSize); mPaint.setStyle(Paint.Style.FILL); //定义Rect做文本的宽高的测量 mTextbounds = new Rect(); mPaint.getTextBounds(mText, 0, mText.length(), mTextbounds); } /** * 测量 * * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); if (widthMode == MeasureSpec.EXACTLY) { //(精确值)控件为匹配父亲 mWidth = widthSize; } else { //包裹内容或无限大的时候 mWidth = mTextbounds.width() + getPaddingRight() + getPaddingLeft() + mTextbounds.width() / mText.length(); if (widthMode == MeasureSpec.AT_MOST) { //无限大的时候 mWidth = Math.min(mWidth, widthSize); } } int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (heightMode == MeasureSpec.EXACTLY) { mHeight = heightSize; } else { mHeight = mTextbounds.height() * 2 + getPaddingBottom() + getPaddingTop(); if (heightMode == MeasureSpec.AT_MOST) { mHeight = Math.min(mHeight, heightSize); } } setMeasuredDimension(mWidth, mHeight); } /** * 绘制 * * @param canvas */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //绘制边框 mPaint.setColor(mBackgroud); RectF ractF = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()); canvas.drawRoundRect(ractF, mRoundSize, mRoundSize, mPaint); //绘制文本 mPaint.setColor(mTextColor); //计算绘制文本的位置 int startX = getMeasuredWidth() / 2 - mTextbounds.width() / 2; int startY = getMeasuredHeight() / 2 + mTextbounds.height() / 2 - mTextbounds.bottom; canvas.drawText(mText, startX, startY, mPaint); } /** * 定义监听时间的接口 */ public interface OnRoundBtnClickListener { void onClick(String content); } /** * 设置默认点击的动画 */ private void onClickAnimation() { AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(660); this.startAnimation(alphaAnimation); } /** * 设置点机的动画2 */ private void onClickAnimation2() { SwingAnimation swingAnimation = new SwingAnimation(); swingAnimation.setDuration(660); this.startAnimation(swingAnimation); } /** * 摇头动画 */ class SwingAnimation extends Animation { @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { t.getMatrix().setTranslate((float) (Math.sin(interpolatedTime * 20) * 50), 0); super.applyTransformation(interpolatedTime, t); } } /** * 设置点击动画 * * @param anomationMode */ public void setAnomationMode(int anomationMode) { mAnomationMode = anomationMode; } public String getText() { return mText; } public void setText(String text) { mText = text; postInvalidate(); } public int getTextColor() { return mTextColor; } public void setTextColor(int textColor) { mTextColor = textColor; postInvalidate(); } public int getTextSize() { return mTextSize; } public void setTextSize(int textSize) { mTextSize = textSize; postInvalidate(); } public int getBtnWidth() { return mWidth; } public void setBtnWidth(int width) { mWidth = width; postInvalidate(); } public int getBtnHeight() { return mHeight; } public void setBtnHeight(int height) { mHeight = height; postInvalidate(); } public int getBackgroud() { return mBackgroud; } public void setBackgroud(int backgroud) { mBackgroud = backgroud; postInvalidate(); } public float getRoundSize() { return mRoundSize; } public void setRoundSize(float roundSize) { mRoundSize = roundSize; postInvalidate(); }}2)属性
<!--圆角按钮--><declare-styleable name="RoundButton"> <attr name="textSize" format="dimension"/> <attr name="textColor" format="color"/> <attr name="text" format="string"/> <attr name="backgroud" format="color"/> <attr name="roundSize" format="float"/></declare-styleable>
阅读全文
1 0
- 自定义设置圆角大小的圆角按钮
- 自定义圆角按钮
- 设置按钮的大小 ,按钮变小手
- 设置圆角按钮的方法
- 设置按钮圆角的两种方
- Button按钮圆角的设置:
- Android 自定义圆角按钮
- android自定义圆角按钮
- android圆角按钮自定义
- 自定义View改变圆的颜色设置开始、重置按钮
- storyboard设置圆角按钮
- 自定义组件大小的设置
- 设置自定义View的大小
- 安卓 实现圆角按钮的的自定义和点击圆角按钮按钮实现背景变化
- iOS中创建自定义的圆角按钮
- Android自定义Iphone风格的圆角边框开关按钮
- Android自定义Iphone风格的圆角边框开关按钮
- Android 自定义带点击效果的圆角按钮
- FastDFS分布式文件系统
- uoj34多项式乘法
- 99%的人都理解错了HTTP中GET与POST的区别
- mysql 主从复制原理及实战操作
- gamemaker学习笔记:按钮检测
- 自定义设置圆角大小的圆角按钮
- Linux 下快速配置网卡操作
- PHP函数spl_autoload_register()用法和__autoload()介绍
- FastDFS安装、配置、部署(一)
- Java实现HTML代码生成PDF文档
- Linux 系统启动过程
- Moving to “Micro-service” and “Serverless”
- bzoj 1610: [Usaco2008 Feb]Line连线游戏
- 知道这20个正则表达式,能让你少写1,000行代码 沐子哈 发表于10小时前 转 知道这20个正则表达式,能让你少写1,000行代码