Android自定义View实现可展开、会呼吸的按钮

Android自定义View实现可展开、会呼吸的按钮,第1张

概述Android自定义View实现可展开、会呼吸的按钮 不专门练习的话,自定义View的知识又忘了许多..正好新项目里有这个需求,就再练习一下,代码已上传:地址 可以修改文本.文字大小.各种颜色: 1.按照国际惯例,就是新建attrs,写各种需要的属性,然后获取,新建各种所需的Paint.Rect,重写onMeasure计算宽高,重写onDraw画图搞起.. 2.关于可展开效果,其实就是点击发布时,启动一个ValueAnimator,对一个圆角矩形的左边距离不断改变: int mBackgroundRectFLeft; RectF mBackgrou

不专门练习的话,自定义view的知识又忘了许多。。正好新项目里有这个需求,就再练习一下,代码已上传:地址


可以修改文本、文字大小、各种颜色:


1、按照国际惯例,就是新建attrs,写各种需要的属性,然后获取,新建各种所需的Paint、Rect,重写onMeasure计算宽高,重写onDraw画图搞起。。

2、关于可展开效果,其实就是点击发布时,启动一个ValueAnimator,对一个圆角矩形的左边距离不断改变:

int mBackgroundRectFleft;RectF mBackgroundRectF = new RectF();@OverrIDeprotected voID onDraw(Canvas canvas) { mBackgroundRectF.set(mBackgroundRectFleft,getWIDth(),getHeight()); canvas.drawRoundRect(mBackgroundRectF,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、关于呼吸效果,就是一个对外圆圈半径改变的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();@OverrIDeprotected voID onDraw(Canvas canvas) { canvas.drawCircle(mInnerCircleCenterX,mInnerCircleCenterY,mBreatheRadius,mBreathePaint);//呼吸圈

4、关于文字位置居中计算,以前我用一个Rect,用

paint.getTextBounds(text,text.length(),mTextRect);int textWIDth = mTextRect.wIDth();int textHeight = mTextRect.height();

这样计算不准确,可能是因为返回的宽高是int值,应该用FontMetrics类来计算,大家可以搜一下:

float buttonTextWIDth = mbuttonTextPaint.measureText(mbuttonStr,mbuttonStr.length());Paint.FontMetrics publishFontMetrics = mbuttonTextPaint.getFontMetrics();canvas.drawText(mbuttonStr,mbuttonStr.length(),getWIDth() - mOuterRadius - marcWIDth / 2 - buttonTextWIDth / 2,mOuterRadius + marcWIDth / 2 + -(publishFontMetrics.ascent + publishFontMetrics.descent) / 2,mbuttonTextPaint);

5、再有就是OntouchEvent的处理,因为这个控件不是一直都是展开状态,那么就要求控件在闭合的时候,要不影响该控件下层控件对点击的处理。比如我这个ExpandableBreathngbutton,下层是一个RecyclerVIEw,并设置了OnItemClickListener,那我这个按钮在闭合时,点击按钮左侧但还是在这个VIEw范围内的地方,如下图红框内


这个范围内应该不处理事件,return false

@OverrIDepublic 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()) {    //未展开状态下,点击发布圆左侧的位置,不处理事件    return false;   }   break; }}

然后在up事件中计算点击了发布按钮还是展开的item,就是计算点击的坐标是在圆半径内,还是在item矩形范围内。

最后源码奉上: 详细地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

总结

以上是内存溢出为你收集整理的Android自定义View实现可展开、会呼吸的按钮全部内容,希望文章能够帮你解决Android自定义View实现可展开、会呼吸的按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1145780.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存