In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.