新聞中心
本篇內(nèi)容主要講解“Android中如何通過自定義view實(shí)現(xiàn)動態(tài)柱狀圖”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android中如何通過自定義view實(shí)現(xiàn)動態(tài)柱狀圖”吧!
創(chuàng)新互聯(lián),為您提供成都網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、網(wǎng)站營銷推廣、網(wǎng)站開發(fā)設(shè)計,對服務(wù)咖啡廳設(shè)計等多個行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報價服務(wù),我們深知市場的競爭激烈,認(rèn)真對待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!
自定義view
public class Histogram extends View { int MAX = 100;//矩形顯示的最大值 int corner = 0; //矩形的角度。 設(shè)置為0 則沒有角度。 double data = 0.0;//顯示的數(shù) double tempData = 0; //初始數(shù)據(jù) int textPadding = 50; //字體與矩形圖的距離 Paint mPaint; int mColor; Context mContext; //構(gòu)造函數(shù) 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(); } //畫筆方法 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());// 設(shè)置個新的長方形 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; } //防止數(shù)值很大的的時候,動畫時間過長 int step = (int) (data / 100 + 1.0); if (tempData < data - step) { tempData = tempData + step; } else { tempData = data; } //畫圓角矩形 String S = tempData + ""; //如果數(shù)字后面需要加% 則在""中添加% //設(shè)置顯示的字體 Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),"digital-7.ttf"); mPaint.setTypeface(typeface); // //一個字和兩,三個字的字號相同 if (S.length() < 4) { mPaint.setTextSize(getWidth()/2 ); } else { mPaint.setTextSize(50); //可以通過getWidth()/2 改變字體大小 也可以通過設(shè)置數(shù)字來改變自己想要的字體大小 當(dāng)超出矩形圖寬度時不能顯示全部 } // float textH = mPaint.ascent() + mPaint.descent(); float MaxH = getHeight() - textH - 2 * DensityUtils.pxTodip(mContext, textPadding); // //圓角矩形的實(shí)際高度 float realH = (float) (MaxH / MAX * tempData); RectF oval3 = new RectF(0, getHeight() - realH, getWidth(), getHeight());// 設(shè)置個新的長方形 canvas.drawRoundRect(oval3, DensityUtils.pxTodip(mContext, corner), DensityUtils.pxTodip(mContext, corner), mPaint); //寫數(shù)字 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; } }
布局
MainActivity調(diào)用initAllViews()方法
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.2, 100); column_three.setData(40, 100); column_one.mPaint.setColor(getResources().getColor(R.color.colorAccent)); //改變柱狀圖的顏色 }
到此,相信大家對“Android中如何通過自定義view實(shí)現(xiàn)動態(tài)柱狀圖”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)站題目:Android中如何通過自定義view實(shí)現(xiàn)動態(tài)柱狀圖
新聞來源:http://www.dlmjj.cn/article/ihipjh.html