Android中如何通过自定义view实现动态柱状图

Android中如何通过自定义view实现动态柱状图

本篇内容主要讲解“Android中如何通过自定义view实现动态柱状图”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android中如何通过自定义view实现动态柱状图”吧!

自定义view

Android中如何通过自定义view实现动态柱状图

publicclassHistogramextendsView{intMAX=100;//矩形显示的最大值intcorner=0;//矩形的角度。设置为0则没有角度。doubledata=0.0;//显示的数doubletempData=0;//初始数据inttextPadding=50;//字体与矩形图的距离PaintmPaint;intmColor;ContextmContext;//构造函数publicHistogram(Contextcontext){super(context);mContext=context;}publicHistogram(Contextcontext,@NullableAttributeSetattrs){super(context,attrs);mContext=context;initPaint();}publicHistogram(Contextcontext,@NullableAttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);mContext=context;initPaint();}//画笔方法privatevoidinitPaint(){mPaint=newPaint();mPaint.setAntiAlias(true);mColor=mContext.getResources().getColor(R.color.gary);mPaint.setColor(mColor);}@Overridepublicvoiddraw(Canvascanvas){super.draw(canvas);if(data==0.0){mPaint.setTextSize(getWidth()/2);RectFoval3=newRectF(0,getHeight()-DensityUtils.pxTodip(mContext,20),getWidth(),getHeight());//设置个新的长方形canvas.drawRoundRect(oval3,DensityUtils.pxTodip(mContext,corner),DensityUtils.pxTodip(mContext,corner),mPaint);canvas.drawText("0",getWidth()*0.5f-mPaint.measureText("0")*0.5f,getHeight()-DensityUtils.pxTodip(mContext,20)-2*DensityUtils.pxTodip(mContext,textPadding),mPaint);return;}//防止数值很大的的时候,动画时间过长intstep=(int)(data/100+1.0);if(tempData<data-step){tempData=tempData+step;}else{tempData=data;}//画圆角矩形StringS=tempData+"";//如果数字后面需要加%则在""中添加%//设置显示的字体Typefacetypeface=Typeface.createFromAsset(getContext().getAssets(),"digital-7.ttf");mPaint.setTypeface(typeface);////一个字和两,三个字的字号相同if(S.length()<4){mPaint.setTextSize(getWidth()/2);}else{mPaint.setTextSize(50);//可以通过getWidth()/2改变字体大小也可以通过设置数字来改变自己想要的字体大小当超出矩形图宽度时不能显示全部}//floattextH=mPaint.ascent()+mPaint.descent();floatMaxH=getHeight()-textH-2*DensityUtils.pxTodip(mContext,textPadding);////圆角矩形的实际高度floatrealH=(float)(MaxH/MAX*tempData);RectFoval3=newRectF(0,getHeight()-realH,getWidth(),getHeight());//设置个新的长方形canvas.drawRoundRect(oval3,DensityUtils.pxTodip(mContext,corner),DensityUtils.pxTodip(mContext,corner),mPaint);//写数字canvas.drawText(S,getWidth()*0.5f-mPaint.measureText(S)*0.5f,getHeight()-realH-2*DensityUtils.pxTodip(mContext,textPadding),mPaint);if(tempData!=data){postInvalidate();}}publicvoidsetData(doubledata,intMAX){this.data=data;this.MAX=MAX;postInvalidate();}publicintgetmColor(){returnmColor;}publicvoidsetmColor(intmColor){this.mColor=mColor;}}

布局

<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="0.2"/><com.mieasy.myhistogramview.Histogramandroid:id="@+id/column_one"android:layout_width="0dp"android:layout_height="300dp"android:layout_weight="0.8"/><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2.4"/><com.mieasy.myhistogramview.Histogramandroid:id="@+id/column_two"android:layout_width="0dp"android:layout_height="300dp"android:layout_weight="1"/><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2.4"/><com.mieasy.myhistogramview.Histogramandroid:id="@+id/column_three"android:layout_width="0dp"android:layout_height="300dp"android:layout_weight="1"/><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="0.2"/></LinearLayout>

MainActivity调用initAllViews()方法

privatevoidinitAllViews(){column_one=(Histogram)findViewById(R.id.column_one);column_two=(Histogram)findViewById(R.id.column_two);column_three=(Histogram)findViewById(R.id.column_three);column_one.setData(20.22,100);column_two.setData(30.2,100);column_three.setData(40,100);column_one.mPaint.setColor(getResources().getColor(R.color.colorAccent));//改变柱状图的颜色}

到此,相信大家对“Android中如何通过自定义view实现动态柱状图”有了更深的了解,不妨来实际操作一番吧!这里是恰卡编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

发布于 2022-04-15 22:29:01
收藏
分享
海报
0 条评论
26
上一篇:Android中如何读取资源文件 下一篇:Android中如何利用LitePal操作数据库
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码