版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】如何在android中自定义圆角button效果
这篇文章将为大家详细讲解有关如何在android中自定义圆角button效果,文章内容质量较高,因此在下分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。源码RoundRadiusButton.java/**
*
author:
xujiajia
*
description:
*
1、drawable只有在设置textString的时候才会生效(居中效果两个一起测量)
*/
public
class
RoundRadiusButton
extends
View
{
//data
private
int
width
=
0;
private
int
height
=
0;
private
int
roundRadius
=
16;
private
int
bgColor
=
Color.LTGRAY;
private
boolean
isTouching
=
false;
//img
and
text
private
Drawable
leftDrawable
=
null;
private
int
drawableWidth
=
20;
private
int
drawableHeight
=
20;
private
int
leftDrawablePaddingRight
=
0;
private
String
textString;
private
int
textSize
=
30;
private
int
textColor
=
Color.BLACK;
//onDraw
Paint
paint;
Path
path;
RectF
rectF;
Rect
rect;
public
RoundRadiusButton(Context
context,
int
width,
int
height)
{
super(context);
this.width
=
width;
this.height
=
height;
this.setLayoutParams(new
ViewGroup.LayoutParams(width,
height));
this.setClickable(true);
}
public
RoundRadiusButton(Context
context,
AttributeSet
attrs)
{
super(context,
attrs);
getDataFromAttrs(context,
attrs);
this.setClickable(true);
}
public
RoundRadiusButton(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
getDataFromAttrs(context,
attrs);
this.setClickable(true);
}
private
void
getDataFromAttrs(Context
context,
AttributeSet
attrs)
{
if
(attrs
==
null)
{
return;
}
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);
leftDrawablePaddingRight
=
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,
Color.BLACK);
ta.recycle();
}
public
void
setRoundRadius(int
roundRadius)
{
this.roundRadius
=
roundRadius;
invalidate();
}
public
void
setBgColor(int
bgColor)
{
this.bgColor
=
bgColor;
invalidate();
}
public
void
setLeftDrawable(Drawable
leftDrawable,
int
drawableWidth,
int
drawableHeight,
int
paddingRight)
{
this.leftDrawable
=
leftDrawable;
this.drawableWidth
=
drawableWidth;
this.drawableHeight
=
drawableHeight;
this.leftDrawablePaddingRight
=
paddingRight;
invalidate();
}
public
void
setTextString(String
textString)
{
this.textString
=
textString;
invalidate();
}
public
void
setTextColor(int
textColor)
{
this.textColor
=
textColor;
invalidate();
}
public
void
setTextSize(int
textSize)
{
this.textSize
=
textSize;
invalidate();
}
@Override
public
boolean
onTouchEvent(MotionEvent
event)
{
if
(isClickable())
{
switch
(event.getAction())
{
case
MotionEvent.ACTION_DOWN:
isTouching
=
true;
invalidate();
break;
case
MotionEvent.ACTION_UP:
isTouching
=
false;
invalidate();
break;
}
}
return
super.onTouchEvent(event);
}
@Override
protected
void
onDraw(Canvas
canvas)
{
super.onDraw(canvas);
if
(width
==
0
||
height
==
0)
{
width
=
getWidth();
height
=
getHeight();
}
if
(paint
==
null)
{
paint
=
new
Paint();
}
if
(path
==
null)
{
path
=
new
Path();
}
if
(rectF
==
null)
{
rectF
=
new
RectF();
}
if
(rect
==
null)
{
rect
=
new
Rect();
}
paint.setColor(bgColor);
paint.setAntiAlias(true);//抗锯齿
paint.setStrokeWidth(0);//线的宽度设为0,避免画圆弧的时候部分圆弧与边界相切
paint.setStyle(Paint.Style.FILL_AND_STROKE);
path.setFillType(Path.FillType.WINDING);
//左上圆角
path.moveTo(0,
roundRadius);
rectF.set(0,
0,
2
*
roundRadius,
2
*
roundRadius);
path.addArc(rectF,
180,
90);
//上边
path.lineTo(width
-
roundRadius,
0);
//右上圆角
rectF.set(width
-
roundRadius
*
2,
0,
width,
roundRadius
*
2);
path.addArc(rectF,
-90,
90);
//右边
path.lineTo(width,
height
-
roundRadius);
//右下圆角
rectF.set(width
-
roundRadius
*
2,
height
-
roundRadius
*
2,
width,
height);
path.addArc(rectF,
0,
90);
//下边
path.lineTo(roundRadius,
height);
//左下圆角
rectF.set(0,
height
-
roundRadius
*
2,
2
*
roundRadius,
height);
path.addArc(rectF,
90,
90);
//左边
path.lineTo(0,
roundRadius);
path.close();
canvas.drawPath(path,
paint);
if
(isTouching)
{
paint.setColor(getContext().getResources().getColor(R.color.black_tran_30));
canvas.drawPath(path,
paint);
}
//填充背景中间空白的部分
path.moveTo(0,
roundRadius);
path.lineTo(width
-
roundRadius,
0);
path.lineTo(width,
height
-
roundRadius);
path.lineTo(roundRadius,
height);
path.close();
canvas.drawPath(path,
paint);
if
(isTouching)
{
paint.setColor(getContext().getResources().getColor(R.color.black_tran_30));
canvas.drawPath(path,
paint);
}
//text,
drawable两个一起计算位置
if
(!TextUtils.isEmpty(textString))
{
paint.setStrokeWidth(1.5f);
paint.setColor(textColor);
paint.setTextSize(textSize);
rect.setEmpty();
paint.getTextBounds(textString,
0,
textString.length(),
rect);
float
leftBitmap
=
0;
float
topBitmap
=
0;
if
(leftDrawable
!=
null)
{
if
(leftDrawable
!=
null)
{
leftBitmap
=
(1.0f
*
width
-
drawableWidth
-
rect.width()
-
leftDrawablePaddingRight)
/
2;
topBitmap
=
(1.0f
*
height
-
drawableHeight)
/
2;
leftDrawable.setBounds((int)
leftBitmap,
(int)
topBitmap,
(int)
(leftBitmap
+
drawableWidth),
(int)
(topBitmap
+
drawableHeight));
leftDrawable.draw(canvas);
}
}
float
textX
=
0;
float
textY
=
1.0f
*
height
/
2
+
paint.getTextSize()
/
2
-
paint.getFontMetrics().descent
/
2;
if
(leftBitmap
==
0
&&
topBitmap
==
0)
{
textX
=
width
/
2
-
rect.width()
/
2;
}
else
{
textX
=
leftBitmap
+
drawableWidth
+
leftDrawablePaddingRight;
}
canvas.drawText(textString,
textX,
textY,
paint);
}
}
}MainActivity.javapublic
class
MainActivity
extends
AppCompatActivity
{
private
LinearLayout
llContainer;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private
void
initView()
{
llContainer
=
findViewById(R.id.ll_container);
RoundRadiusButton
roundRadiusButton
=
new
RoundRadiusButton(this,
500,
200);
roundRadiusButton.setBgColor(Color.LTGRAY);
roundRadiusButton.setRoundRadius(40);
//text
roundRadiusButton.setTextString("testtesttest");
roundRadiusButton.setTextColor(Color.WHITE);
roundRadiusButton.setTextSize(40);
//drawable
roundRadiusButton.setLeftDrawable(getResources().getDrawable(R.mipmap.ic_launcher),
60,
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<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:id="@+id/ll_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#868684"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity"
>
<com.example.newbuttiontest.RoundRadiusButton
android:layout_width="300dp"
android:layout_height="200dp"
app:bgColor="#FFEB3B"
app:drawableHeight="18dp"
app:drawableWidth="18dp"
app:leftDrawable="@mipmap/ic_launcher"
app:leftDrawablePaddingRight="5dp"
app
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025车间师傅劳动合同范本
- 延吉协议书离婚
- 发票协议单位合同范本
- 转让协议书可以更换协议书
- 夫妻用车协议合同范本
- 2026届甘肃省兰州市城关区外国语学校物理九上期中联考试题含解析
- 旅行社委托协议书模板
- 二手房购房意向合同(标准版)
- 仪器定制协议书
- 独家协议合同范本房产
- 医院陪护服务投标方案(技术标 )
- 监控机房搬迁实施方案
- 华为公司股权分配政策
- 统计用产品分类目录
- 果蔬贮藏与加工 13第三章 果蔬乙烯代谢生理
- GB/T 7999-2007铝及铝合金光电直读发射光谱分析方法
- GB/T 3620.1-2016钛及钛合金牌号和化学成分
- GB/T 13173-2021表面活性剂洗涤剂试验方法
- 套筒冠义齿课件
- 《生物力学》配套教学课件
- DB41-T 2127-2021冬小麦夏玉米两熟制农田有机肥替减化肥技术规程-(高清现行)
评论
0/150
提交评论