概述
在平时的开发过程中,我们经常会遇到需要使用圆角按钮的情况,其中通常还包含很多其他的小功能,比如里面添加img、设置不同的圆角大小等
对于这样的场景,也可以直接创建多个形状并定义多个xml文件。但如果使用很频繁的话,直接定制一个会很方便。
甚至在某些情况下,常规图形无法用形状来定义,比如需要使用贝塞尔曲线等。
如果全局需要这样风格的View,那么自定义一个View就非常有必要了。
本文主要是demo记录。如有必要,读者可以从中学习。
演示
主要功能:
www.sychzs.cn
/** * 作者:徐佳佳 * 描述: * 1.Drawable只有在设置textString时才会生效(居中效果一起测) */ 公共类 RoundRadiusButton 扩展视图 { //数据 私有 int 宽度 = 0; 私有 int 高度 = 0; 私有 int roundRadius = 16; 私有 int bgColor = Color.LTGRAY; 私人布尔 isTouching = false; //图片和文字 私有Drawable leftDrawable = null;私有 int 可绘制宽度 = 20; 私有 int 可绘制高度 = 20; 私有 int leftDrawablePaddingRight = 0; 私有字符串文本字符串; 私有 int 文本大小 = 30; 私有 int textColor = www.sychzs.cn; //绘制时 油漆油漆; 路径路径; 矩形F 矩形F; 矩形矩形; 公共 RoundRadiusButton(上下文上下文,int 宽度,int 高度){ 超级(上下文); this.width = 宽度; this.height = 高度; this.setLayoutParams(new ViewGroup.LayoutParams(宽度,高度)); this.setClickable(true); } 公共 RoundRadiusButton(上下文上下文,属性集属性){ 超级(上下文,属性); getDataFromAttrs(上下文,attrs); this.setClickable(true); } 公共 RoundRadiusButton(上下文上下文,AttributeSet attrs,int defStyleAttr){ 超级(上下文,属性,defStyleAttr); getDataFromAttrs(上下文,attrs); this.setClickable(true); } 私有无效 getDataFromAttrs(上下文上下文,AttributeSet attrs){ 如果(属性==空){ 返回; }TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.RoundRadiusButton); roundRadius = ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_roundRadius,16); bgColor = ta.getColor(R.styleable.RoundRadiusButton_bgColor,Color.LTGRAY); leftDrawable = ta.getDrawable(R.styleable.RoundRadiusButton_leftDrawable); drawableWidth = ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_drawableWidth,0); drawableHeight = ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_drawableHeight,0); 左可绘制填充右= ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_leftDrawablePaddingRight,0); textString = ta.getString(R.styleable.RoundRadiusButton_textString); textSize = ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_textSize,0); textColor = ta.getColor(R.styleable.RoundRadiusButton_textColor,www.sychzs.cn); ta.recycle(); } 公共无效setRoundRadius(int roundRadius){ this.roundRadius = roundRadius;无效(); } 公共无效setBgColor(int bgColor){ this.bgColor = bgColor; 无效(); } 公共无效setLeftDrawable(Drawable leftDrawable,int drawableWidth,int drawableHeight,int paddingRight){ this.leftDrawable = leftDrawable; this.drawableWidth =drawableWidth; this.drawableHeight =drawableHeight; this.leftDrawablePaddingRight = paddingRight; 无效(); } 公共无效setTextString(字符串textString){ this.textString = textString; 无效(); } 公共无效setTextColor(int textColor){ this.textColor = 文本颜色; 无效(); } 公共无效setTextSize(int textSize){ this.textSize = textSize; 无效(); } @Override public boolean onTouchEvent(MotionEvent event) { 如果(isClickable()){ 开关 (event.getAction()) { 案例MotionEvent.ACTION_DOWN: 正在触摸=真; 无效(); 休息; 案例MotionEvent.ACTION_UP: 正在触摸=假; 无效(); 休息; } }返回 super.onTouchEvent(事件); } @Override protected void onDraw(Canvas canvas) { super.onDraw(画布); if (宽度 == 0 || 高度 == 0) { 宽度 = getWidth(); 高度 = getHeight(); } 如果(油漆==空){ 油漆=新油漆(); } 如果(路径==空){ 路径=新路径(); } if (rectF == null) { rectF = 新的 RectF(); } 如果(矩形==空){ 矩形=新矩形(); } 油漆.setColor(bgColor); Paint.setAntiAlias(true);//抗锯齿 Paint.setStrokeWidth(0);//设置线条宽度为0,防止绘制圆弧时部分圆弧与边界相切 Paint.setStyle(Paint.Style.FILL_AND_STROKE); 路径.setFillType(路径.FillType.WINDING); //左上角圆角 路径.moveTo(0,roundRadius); rectF.set(0,2 * roundRadius,2 * roundRadius); 路径.addArc(rectF,180,90); //顶部 path.lineTo(宽度-roundRadius,0); //右上角圆角 rectF.set(宽度 - roundRadius * 2,宽度,roundRadius * 2); 路径.addArc(rectF,-90,90); //正确的 path.lineTo(宽度,高度 - roundRadius); //右下圆角 rectF.set(宽度 - roundRadius * 2,高度 - roundRadius * 2,height);路径.addArc(rectF,90); //以下 path.lineTo(roundRadius,高度); //左下圆角 rectF.set(0,90,90); //左边 path.lineTo(0,roundRadius); 路径.close(); canvas.drawPath(路径,绘画); 如果(正在触摸){ Paint.setColor(getContext().getResources().getColor(www.sychzs.cn_tran_30)); canvas.drawPath(路径,绘画); } //填充背景中间的空白区域 路径.moveTo(0,roundRadius); path.lineTo(宽度-roundRadius,0); path.lineTo(宽度,高度 - roundRadius); path.lineTo(roundRadius,高度); 路径.close(); canvas.drawPath(路径,绘画); } //Text和drawable一起计算位置 if (!TextUtils.isEmpty(textString)) { 油漆.setStrokeWidth(1.5f); 油漆.setColor(文本颜色); Paint.setTextSize(textSize); rect.setEmpty(); Paint.getTextBounds(textString,textString.length(),rect); 浮动左位图 = 0; 浮动顶部位图 = 0; if (leftDrawable != null) { if (leftDrawable != null) { leftBitmap = (1.0f * 宽度 - drawableWidth - rect.width() - leftDrawablePaddingRight) / 2;topBitmap = (1.0f * 高度 - 可绘制高度) / 2; leftDrawable.setBounds((int) leftBitmap,(int) topBitmap,(int) (leftBitmap + drawableWidth),(int) (topBitmap + drawableHeight)); leftDrawable.draw(画布); } } 浮动文本X = 0; 浮动文本Y = 1.0f*高度/2+paint.getTextSize()/2-paint.getFontMetrics().descent/2; if (leftBitmap == 0 && topBitmap == 0) { textX = 宽度 / 2 - rect.width() / 2; } 别的 { textX = leftBitmap + drawableWidth + leftDrawablePaddingRight; } canvas.drawText(textString,textX,textY,paint); } } }
www.sychzs.cn
公共类 MainActivity 扩展 AppCompatActivity { 私有 LinearLayout llContainer; @覆盖 protected void onCreate(Bundle savingInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 初始化视图(); } 私有无效 initView() { llContainer = findViewById(R.id.ll_container);RoundRadiusButton roundRadiusButton = new RoundRadiusButton(this,500,200); roundRadiusButton.setBgColor(Color.LTGRAY); roundRadiusButton.setRoundRadius(40); //文本 roundRadiusButton.setTextString("testtesttest"); roundRadiusButton.setTextColor(Color.WHITE); roundRadiusButton.setTextSize(40); //可绘制的 roundRadiusButton.setLeftDrawable(getResources().getDrawable(R.mipmap.ic_launcher),60,80); roundRadiusButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"testest",Toast.LENGTH_LONG).show(); } }); roundRadiusButton.setClickable(false); llContainer.addView(roundRadiusButton); } }
activity_main.xml
<线性布局 xmlns:android =“http://www.sychzs.cn/apk/res/android” xmlns:app="http://www.sychzs.cn/apk/res-auto" xmlns:tools="http://www.sychzs.cn/tools" android:id="@+id/ll_container" 安卓:layout_width =“match_parent”安卓:layout_height =“match_parent” 安卓:背景=“#868684” 机器人:重力=“中心” android:方向=“垂直” 工具:context =“.MainActivity” >
attrs.xml
colors.xml
#30000000
总结
以上是小编介绍的android自定义圆角按钮效果的示例代码。希望对您有所帮助。如果您有任何疑问,请给我留言,编辑会及时回复您。非常感谢您对我们网站的支持!
如果您认为本文对您有帮助,欢迎转载,请注明出处,谢谢!
以上是来客网为您收集的android自定义圆角按钮效果(自定义视图demo)示例代码全部内容。希望本文能够帮助您解决android自定义圆角按钮效果的示例代码(自定义视图demo)。查看Demo)遇到程序开发问题。
如果您觉得来客网内容不错,欢迎您向您的程序员朋友推荐来客网。