Android自定義View實現(xiàn)可展開、會呼吸的按鈕
不專門練習(xí)的話,自定義View的知識又忘了許多。正好新項目里有這個需求,就再練習(xí)一下,代碼已上傳:地址

可以修改文本、文字大小、各種顏色:

1、按照國際慣例,就是新建attrs,寫各種需要的屬性,然后獲取,新建各種所需的Paint、Rect,重寫onMeasure計算寬高,重寫onDraw畫圖搞起。。
2、關(guān)于可展開效果,其實就是點擊發(fā)布時,啟動一個ValueAnimator,對一個圓角矩形的左邊距離不斷改變:
int mBackgroundRectFLeft;
RectF mBackgroundRectF = new RectF();
@Override
protected void onDraw(Canvas canvas) {
mBackgroundRectF.set(mBackgroundRectFLeft, 0, getWidth(), getHeight());
canvas.drawRoundRect(mBackgroundRectF, mOuterRadius, mOuterRadius, mmBackgroundRectPaint);//圓角背景矩形
}
private void openButton() {
ValueAnimator rectLeftAnim = ValueAnimator.ofInt(mBackgroundRectFLeft, mArcWidth / 2);
rectLeftAnim.setDuration(250);
ValueAnimator textAlphaAnim = ValueAnimator.ofInt(0, mItemTextAlpha);
textAlphaAnim.setDuration(120);
rectLeftAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mBackgroundRectFLeft = (int) animation.getAnimatedValue();
invalidate();
}
});
}
3、關(guān)于呼吸效果,就是一個對外圓圈半徑改變的ValueAnimator:
mBreatheRadius = getHeight() / 2 - mArcWidth / 4;
mBreatheAnim = ValueAnimator.ofFloat(mBreatheRadius, mBreatheRadius - mArcWidth / 2);
mBreatheAnim.setDuration(1000);
mBreatheAnim.setRepeatMode(ValueAnimator.REVERSE);
mBreatheAnim.setRepeatCount(Integer.MAX_VALUE);
mBreatheAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mBreatheRadius = (float) animation.getAnimatedValue();
invalidate();
}
});
mBreatheAnim.start();
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(mInnerCircleCenterX, mInnerCircleCenterY, mBreatheRadius, mBreathePaint);//呼吸圈
4、關(guān)于文字位置居中計算,以前我用一個Rect,用
paint.getTextBounds(text, 0, text.length(), mTextRect); int textWidth = mTextRect.width(); int textHeight = mTextRect.height();
這樣計算不準(zhǔn)確,可能是因為返回的寬高是int值,應(yīng)該用FontMetrics類來計算,大家可以搜一下:
float buttonTextWidth = mButtonTextPaint.measureText(mButtonStr, 0, mButtonStr.length()); Paint.FontMetrics publishFontMetrics = mButtonTextPaint.getFontMetrics(); canvas.drawText(mButtonStr, 0, mButtonStr.length(), getWidth() - mOuterRadius - mArcWidth / 2 - buttonTextWidth / 2, mOuterRadius + mArcWidth / 2 + -(publishFontMetrics.ascent + publishFontMetrics.descent) / 2, mButtonTextPaint);
5、再有就是OnTouchEvent的處理,因為這個控件不是一直都是展開狀態(tài),那么就要求控件在閉合的時候,要不影響該控件下層控件對點擊的處理。比如我這個ExpandableBreathngButton,下層是一個RecyclerView,并設(shè)置了OnItemClickListener,那我這個按鈕在閉合時,點擊按鈕左側(cè)但還是在這個View范圍內(nèi)的地方,如下圖紅框內(nèi)

這個范圍內(nèi)應(yīng)該不處理事件,return false
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) event.getX();
y = (int) event.getY();
if (!isOpen && x < getWidth() - 2 * mOuterRadius && y > 0 && y < getHeight()) {
//未展開狀態(tài)下,點擊發(fā)布圓左側(cè)的位置,不處理事件
return false;
}
break;
}
}
然后在up事件中計算點擊了發(fā)布按鈕還是展開的item,就是計算點擊的坐標(biāo)是在圓半徑內(nèi),還是在item矩形范圍內(nèi)。
最后源碼奉上: 詳細(xì)地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android?微信搶紅包工具AccessibilityService實現(xiàn)詳解
這篇文章主要為大家介紹了android?微信搶紅包工具AccessibilityService實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android實現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動態(tài)更新的方法
這篇文章主要介紹了Android實現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動態(tài)更新的方法,結(jié)合實例形式詳細(xì)分析了ListView異步加載數(shù)據(jù)的操作步驟與具體實現(xiàn)技巧,需要的朋友可以參考下2016-08-08
Android滑動到頂部和底部時出現(xiàn)的陰影如何去掉
本文給大家介紹android滑動到頂部和底部時出現(xiàn)的陰影去掉的解決方法,本文還涉及到listview各個屬性的用法介紹,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-10-10
Android自定義view實現(xiàn)標(biāo)簽欄功能(只支持固定兩個標(biāo)簽)
這篇文章主要介紹了Android自定義view實現(xiàn)標(biāo)簽欄(只支持固定兩個標(biāo)簽),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Android DrawerLayout帶有側(cè)滑功能的布局類(1)
這篇文章主要為大家詳細(xì)介紹了Android DrawerLayout帶有側(cè)滑功能的布局類,感興趣的小伙伴們可以參考一下2016-07-07
Android studio 使用Debugger問題(代碼中含有ndk)
這篇文章主要介紹了Android studio 使用Debugger問題(代碼中含有ndk),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
淺談Android app開發(fā)中Fragment的Transaction操作
這篇文章主要介紹了Android app開發(fā)中Fragment的Transaction操作,包括Transaction和Fragment的生命周期的聯(lián)系等內(nèi)容,需要的朋友可以參考下2016-02-02
android動態(tài)設(shè)置app當(dāng)前運行語言的方法
下面小編就為大家?guī)硪黄猘ndroid動態(tài)設(shè)置app當(dāng)前運行語言的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03

