Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize dynamic histogram through self-defined view in Android

Shulou Source: shulou.com Published: 2022-05-31 22:03:04 10月01日 Update

This article mainly explains "how to realize dynamic bar chart through custom view in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve dynamic bar chart through custom view in Android"!

Custom view

Public class Histogram extends View {int MAX = 100ram / the maximum value shown by the rectangle int corner = 0 role / angle of the rectangle. If set to 0, there is no angle. The number of double data = 0. 0 super / the number displayed double tempData = 0; the initial data int textPadding = 50; / / the distance between the font and the rectangle Paint mPaint; int mColor; Context mContext; / / constructor public Histogram (Context context) {super (context); mContext = context;} public Histogram (Context context, @ Nullable AttributeSet attrs) {super (context, attrs); mContext = context; initPaint () } public Histogram (Context context, @ Nullable AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); mContext = context; initPaint ();} / / Brush method private void initPaint () {mPaint = new Paint (); mPaint.setAntiAlias (true); mColor = mContext.getResources (). GetColor (R.color.gary); mPaint.setColor (mColor);} @ Override public void draw (Canvas canvas) {super.draw (canvas) If (data = = 0.0) {mPaint.setTextSize (getWidth () / 2); RectF oval3 = new RectF (0, getHeight ()-DensityUtils.pxTodip (mContext, 20), getWidth (), getHeight ()); / / set a new rectangular 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;} / / prevent long animation time int step = (int) (data / 100 + 1.0); if (tempData < data-step) {tempData = tempData + step } else {tempData = data;} / / draw rounded rectangle String S = tempData + ""; / / if you need to add% after the number, add% / / set the displayed font Typeface typeface = Typeface.createFromAsset (getContext (). GetAssets (), "digital-7.ttf"); mPaint.setTypeface (typeface) / one word and two or three words have the same font size if (S.length () < 4) {mPaint.setTextSize (getWidth () / 2);} else {mPaint.setTextSize (50); / / you can change the font size through getWidth () / 2 or you can change the font size you want by setting a number. When it exceeds the width of the rectangle, it cannot display all} / / float textH = mPaint.ascent () + mPaint.descent (). Float MaxH = getHeight ()-textH-2 * DensityUtils.pxTodip (mContext, textPadding); / / the actual height of the rounded rectangle float realH = (float) (MaxH / MAX * tempData); RectF oval3 = new RectF (0, getHeight ()-realH, getWidth (), getHeight ()); / / set a new rectangle canvas.drawRoundRect (oval3, DensityUtils.pxTodip (mContext, corner), DensityUtils.pxTodip (mContext, corner), mPaint) / / write numbers canvas.drawText (S, getWidth () * 0.5f-mPaint.measureText (S) * 0.5f, getHeight ()-realH-2 * DensityUtils.pxTodip (mContext, textPadding), mPaint); if (tempData! = data) {postInvalidate ();}} public void setData (double data, int MAX) {this.data = data; this.MAX = MAX; postInvalidate ();} public int getmColor () {return mColor } public void setmColor (int mColor) {this.mColor = mColor;}}

Overall Arrangement

MainActivity calls the initAllViews () method

Private void initAllViews () {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.2100); column_three.setData (40,100); column_one.mPaint.setColor (getResources (). GetColor (R.color.colorAccent)) / / change the color of the bar chart} so far, I believe you have a deeper understanding of "how to achieve dynamic bar chart through custom view in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Rectangle column font dynamic number method content can pass fillet size actual angle rectangle learning practical large deeper maximum same three Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi vpn Shulou Tech Info Xiaomi OPPO Reno