




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】如何在android中自定義圓角button效果
這篇文章將為大家詳細(xì)講解有關(guān)如何在android中自定義圓角button效果,文章內(nèi)容質(zhì)量較高,因此在下分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。源碼RoundRadiusButton.java/**
*
author:
xujiajia
*
description:
*
1、drawable只有在設(shè)置textString的時(shí)候才會(huì)生效(居中效果兩個(gè)一起測(cè)量)
*/
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);//線(xiàn)的寬度設(shè)為0,避免畫(huà)圓弧的時(shí)候部分圓弧與邊界相切
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兩個(gè)一起計(jì)算位置
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 單位整修合同范本
- 2025年天津從業(yè)資格證貨運(yùn)題庫(kù)答案大全
- 關(guān)于消防器材購(gòu)買(mǎi)合同范本
- 企業(yè)聯(lián)營(yíng)合作合同范本
- 醫(yī)美手術(shù)合同范本
- 單位公車(chē)出租合同范本
- 加高工程合同范本
- 農(nóng)戶(hù)合同范本
- 劇組服裝采購(gòu)合同范本
- 共享單車(chē)租金合同范本
- 《建筑設(shè)備安裝與識(shí)圖》混合式教學(xué)課程規(guī)范(課程標(biāo)準(zhǔn))
- 2024年云南省第二強(qiáng)制隔離戒毒所醫(yī)療衛(wèi)生公務(wù)員招錄1人《行政職業(yè)能力測(cè)驗(yàn)》模擬試卷(答案詳解版)
- 《體育開(kāi)學(xué)第一課:體育常規(guī)教育》課件
- 上海市高新技術(shù)成果轉(zhuǎn)化項(xiàng)目認(rèn)定申請(qǐng)書(shū)
- 休閑體育小鎮(zhèn)規(guī)劃方案
- 海南紅色拓展培訓(xùn)方案
- 鎂合金汽車(chē)輪轂的研究與開(kāi)發(fā)
- SHAFER氣液聯(lián)動(dòng)執(zhí)行機(jī)構(gòu)培訓(xùn)
- 小學(xué)生守則、日常行為規(guī)范教育實(shí)施方案
- 湖南省六年級(jí)上冊(cè)數(shù)學(xué)期末試卷(含答案)
- 部編版小學(xué)六年級(jí)道德與法治下冊(cè)課堂達(dá)標(biāo)檢測(cè)試卷全冊(cè)含答案
評(píng)論
0/150
提交評(píng)論