您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android中自定義View實(shí)現(xiàn)可展開的按鈕,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
1、按照國(guó)際慣例,就是新建attrs,寫各種需要的屬性,然后獲取,新建各種所需的Paint、Rect,重寫onMeasure計(jì)算寬高,重寫onDraw畫圖搞起。。
2、關(guān)于可展開效果,其實(shí)就是點(diǎn)擊發(fā)布時(shí),啟動(dòng)一個(gè)ValueAnimator,對(duì)一個(gè)圓角矩形的左邊距離不斷改變:
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)于呼吸效果,就是一個(gè)對(duì)外圓圈半徑改變的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)于文字位置居中計(jì)算,以前我用一個(gè)Rect,用
paint.getTextBounds(text, 0, text.length(), mTextRect); int textWidth = mTextRect.width(); int textHeight = mTextRect.height();
這樣計(jì)算不準(zhǔn)確,可能是因?yàn)榉祷氐膶捀呤莍nt值,應(yīng)該用FontMetrics類來計(jì)算,大家可以搜一下:
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的處理,因?yàn)檫@個(gè)控件不是一直都是展開狀態(tài),那么就要求控件在閉合的時(shí)候,要不影響該控件下層控件對(duì)點(diǎn)擊的處理。比如我這個(gè)ExpandableBreathngButton,下層是一個(gè)RecyclerView,并設(shè)置了OnItemClickListener,那我這個(gè)按鈕在閉合時(shí),點(diǎn)擊按鈕左側(cè)但還是在這個(gè)View范圍內(nèi)的地方,如下圖紅框內(nèi)
這個(gè)范圍內(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)下,點(diǎn)擊發(fā)布圓左側(cè)的位置,不處理事件 return false; } break; } }
關(guān)于怎么在Android中自定義View實(shí)現(xiàn)可展開的按鈕就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。