Android自定义View实现仿驾考宝典显示分数效果(收藏)Android

小编最近发现,一些炫酷的view效果,通过需要自定义view和属性动画结合在一起,才能更容易的实现。

实现的效果图如下:

所用的知识有:

(1)自定义View中的path,主要用来绘制指示块。

(2)属性动画-ValueAnimator,并设置属性动画的监听器。

(3)根据属性动画是否结束的标志,决定是否绘制分数对应的描述文本内容。

实现步骤:

继承自View,在构造函数中获取自定义属性和初始化操作(初始化画笔)

privatevoidobtainAttrs(Contextcontext,AttributeSetattrs){TypedArraytypedArray=context.obtainStyledAttributes(attrs,R.styleable.ScoreView);lineLength=typedArray.getDimension(R.styleable.ScoreView_lineLength,dp2Px(10));lineColor=typedArray.getColor(R.styleable.ScoreView_lineColor,Color.WHITE);typedArray.recycle();}privatevoidinit(){arrowPaint=createPaint(Color.WHITE,0,Paint.Style.FILL,0);arcPaint=createPaint(lineColor,dp2Px(1),Paint.Style.STROKE,0);bgPaint=createPaint(lineColor,dp2Px(1),Paint.Style.FILL,0);reachProgressPaint=createPaint(Color.WHITE,dp2Px(1),Paint.Style.FILL,0);arcReachPaint=createPaint(Color.WHITE,dp2Px(1),Paint.Style.STROKE,0);scoreTextPaint=createPaint(Color.WHITE,0,Paint.Style.STROKE,dp2Px(26));descTextPaint=createPaint(Color.WHITE,0,Paint.Style.STROKE,dp2Px(16));}其中初始化画笔抽取到一个函数中:

privatePaintcreatePaint(intcolor,intstrokeWidth,Paint.Stylestyle,floattextSize){Paintpaint=newPaint(Paint.ANTI_ALIAS_FLAG);paint.setColor(color);paint.setStrokeWidth(strokeWidth);paint.setStyle(style);paint.setStrokeJoin(Paint.Join.ROUND);paint.setStrokeCap(Paint.Cap.ROUND);paint.setTextSize(textSize);returnpaint;}覆盖onSizeChanged(),得到控件的宽高,并决定要绘制区域的大小(控件默认设置了内边距)

@OverrideprotectedvoidonSizeChanged(intw,inth,intoldw,intoldh){super.onSizeChanged(w,h,oldw,oldh);viewWidth=w;viewHeight=h;halfView=Math.min(viewWidth,viewHeight)/2;//宽度或高度中最小值的一半,即决定圆心的位置。radius=(Math.min(viewWidth,viewHeight)-2*DEFAULT_PADDING)/2;//绘制园的半径是宽高除去默认内边距}核心绘制代码,覆盖onDraw()方法,根据动画是否结束的标志,决定是否绘制分数对应的文本。

@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);drawArcBackground(canvas);drawArcProgress(canvas);drawScoreText(canvas);if(isAnimEnd){drawDescText(canvas);}}(1)绘制圆弧背景和灰色刻度背景。

privatevoiddrawArcBackground(Canvascanvas){canvas.save();canvas.translate(halfView,halfView);//绘制圆弧RectFrectF=newRectF(dp2Px(5)-radius,dp2Px(5)-radius,radius-dp2Px(5),radius-dp2Px(5));canvas.drawArc(rectF,120,300,false,arcPaint);//绘制刻度线canvas.rotate(30);for(inti=0;i<100;i++){canvas.drawLine(0,radius-dp2Px(15),0,radius-dp2Px(15)-lineLength,bgPaint);canvas.rotate(degree);}canvas.restore();}(2)绘制刻度,根据ValueAnimator进行动画的当前值curValue,来动态改变绘制指示块和已达进度圆弧,从而实现从0开始移动到设定值的动画效果。

privatevoiddrawArcProgress(Canvascanvas){canvas.save();canvas.translate(halfView,halfView);//绘制圆弧RectFrectF=newRectF(dp2Px(5)-radius,dp2Px(5)-radius,radius-dp2Px(5),radius-dp2Px(5));canvas.drawArc(rectF,120,curValue*degree,false,arcReachPaint);//绘制指示方块,方块是从0开始移动某一个位置的canvas.rotate(30+degree*curValue);Pathpath=newPath();path.moveTo(dp2Px(5),radius);path.lineTo(dp2Px(5),radius-dp2Px(10));path.lineTo(0,radius-dp2Px(15));path.lineTo(-dp2Px(5),radius-dp2Px(10));path.lineTo(-dp2Px(5),radius);path.close();canvas.drawPath(path,arrowPaint);//绘制已经达到的刻度canvas.restore();canvas.save();canvas.translate(halfView,halfView);canvas.rotate(30);for(inti=0;i

privatevoiddrawScoreText(Canvascanvas){canvas.save();canvas.translate(halfView,halfView);StringscoreText=curValue+"分";floattextLength=scoreTextPaint.measureText(scoreText);canvas.drawText(scoreText,-textLength/2,0,scoreTextPaint);canvas.restore();}(4)动画结束时,绘制最终分数对应的提示信息,该信息只有在动画结束后,才会显示出来。

privatevoiddrawDescText(Canvascanvas){canvas.save();canvas.translate(halfView,halfView);Stringdesc="";if(curValue>=90){desc="车神";}else{desc="马路杀手";}floatdescLength=descTextPaint.measureText(desc);canvas.drawText(desc,-descLength/2,dp2Px(30),descTextPaint);canvas.restore();isAnimEnd=false;//isAnimEnd置为false,防止再次点击start时,就显示动画结束时才能显示的内容}(5)提供对外设置最大值的接口,决定最后的分数。

/***对外提供的接口,用于设置进度的最大值**@paramvalue*/publicvoidsetMaxValue(intvalue){if(valueAnimator==null){valueAnimator=ValueAnimator.ofInt(0,value);}valueAnimator.setInterpolator(newLinearInterpolator());valueAnimator.setDuration(30*value);valueAnimator.addUpdateListener(newValueAnimator.AnimatorUpdateListener(){@OverridepublicvoidonAnimationUpdate(ValueAnimatoranimation){curValue=(int)animation.getAnimatedValue();Log.i("debug","curValue="+curValue);invalidate();}});valueAnimator.addListener(newAnimatorListenerAdapter(){@OverridepublicvoidonAnimationEnd(Animatoranimation){super.onAnimationEnd(animation);isAnimEnd=true;//标记动画结束Log.i("debug","onAnimationEnd");Log.i("debug","onAnimationEndcurValue="+curValue);invalidate();}});valueAnimator.start();}

THE END
1.驾考宝典下载2024官方最新电脑版手机版维语版驾考计算器今天已有1152位用户获取了驾考预算 所在城市北京 热门城市: 全国 直辖市: *是否已报考驾校未报考已报考 姓名 手机 驾考圈 交通标志 软件下载 驾考宝典企业版 智慧驾校 买新车 买车网平行之家 二手车 其他系统/版本下载 Windows 客户端 Windows XP以上操作系统 https://www.jiakaobaodian.com/download/
2.驾校一点通2018科目一模拟考试驾考宝典科目四模拟考试2018驾驶证考试网提供最新版科目一模拟考试2018、科目四模拟考试2018、驾照考试科目一、2018科目一考试题库、c1科目一模拟考试、机动车驾驶人科目一考试题库等在线模拟考试供大家练习,保证体验秒杀驾校一点通2018科目一模拟考试、驾考宝典科目四模拟考试2018、驾考宝典2018科目http://www.jszks.cn/
3.驾考宝典智慧驾校版历史版本下载 APK1.1.8 驾考宝典智慧驾校版 2024-07-08 203MB APK1.1.7 驾考宝典智慧驾校版 2024-06-25 202MB APK1.1.6 驾考宝典智慧驾校版 2024-06-14 202MB APK1.1.5 驾考宝典智慧驾校版 2024-06-11 202MB APK1.1.4 驾考宝典智慧驾校版 2024-05-27 201MB 热https://m.liqucn.com/os/android/rj/9769961060632-history.wml
4.驾考宝典2014电脑版.doc驾考宝典2014电脑版.doc 资源ID:12245119资源大小:258.52KB全文页数:81页 资源格式:DOC下载积分:20积分 扫码快捷下载 会员登录下载 微信登录下载 三方登录下载: 点击刷新 手机扫码下载 请使用微信 或支付宝 扫码支付 ? 扫码支付后即可登录、下载文档,同时代表您同意《人人文库网用户协议》https://www.renrendoc.com/d-12245119.html
5.驾考宝典科目一全题1764道(文字题).pdf答案:√ 驾考宝典科目一全题1764道 (文字题)--第10页 驾考宝典科目一全题1764道 (文字题)--第11页 196、 197、59、 机动车在道路上变更车道需要注意什么? 198、A、尽快加速进入左侧车道 199、B、不能影响其他车辆正常行驶 200、C、进入左侧车道时适当减速 201、D、开启转向灯迅速向左转向 202、答案:B https://m.book118.com/html/2023/0602/8124024063005074.shtm
6.驾考宝典主讲的颜值女讲师张懂车帝提供驾考宝典主讲的颜值女讲师张的详细内容,懂车帝是一个汽车资讯平台,懂车更懂你。我们提供最新汽车报价,汽车图片,汽车价格大全,行情、评测、导购等内容,看车选车买车就上懂车帝。https://www.dongchedi.com/tag/pgc/14625757
7.您好,请问在吗。我在驾考宝典上看到的免费领取的茅台酒,我不小心不用理会 http://www.110.com/ask/question-14707233.html
8.iOS开发类似驾考宝典题目切换控件实现切换题目ui当我们题目切换控件时候,很多时候我们会看下驾考宝典的效果 具体如何实现该效果呢,手势控制及切换view 下面给出具体代码如下 1、内容控件UIView。JJDQContentView.h #import "BaseView.h" @interface JJDQContentView : BaseView @property (nonatomic, strong) NSString *title; https://blog.csdn.net/gloryFlow/article/details/131605277
9.豆丁书房VIP解锁版下载豆丁书房VIP解锁版v4.6.2驾考宝典vip解锁版2023 热门推荐 中文在线っと好きだっ最新版 30.52M / v6.0.9 绿巨人无限看丝瓜ios破解版 43.65M / v3.6.4 成品网站W灬源码1377免费版 19.61M / v4.36.5 草莓香蕉榴莲丝瓜绿巨人大全 42.14M / V1.4.9 草莓樱桃丝瓜绿秋葵榴莲泡芙 34.29M / v4.9.8.9 丝瓜榴莲幸福宝绿巨人小蝌蚪app 32.8https://www.bt201.com/app/2267.html