這篇文章給大家介紹怎么在Android中通過(guò)自定義控件實(shí)現(xiàn)一個(gè)折線圖,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、靈壽網(wǎng)絡(luò)推廣、小程序開(kāi)發(fā)、靈壽網(wǎng)絡(luò)營(yíng)銷、靈壽企業(yè)策劃、靈壽品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供靈壽建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。
首先是控件繪圖區(qū)域的劃分,控件左邊取一小部分(控件總寬度的八分之一)繪制表頭,右邊剩余的部分繪制表格
確定表格的行列數(shù),首先繪制一個(gè)三行八列的網(wǎng)格,設(shè)置好行列的坐標(biāo)后開(kāi)始繪制
/*繪制三條橫線*/ for(int i=0;i<3;i++){ canvas.drawLine(textWide, mLineYs[i], totalWidth, mLineYs[i], mPaintLine); } /*繪制八條豎線*/ for(int i=0;i<8;i++){ canvas.drawLine(mLineXs[i], 0, mLineXs[i], totalHeight, mPaintLine); }
網(wǎng)格繪制完成后,開(kāi)始繪制折線圖
根據(jù)輸入的節(jié)點(diǎn)數(shù)據(jù),分別繪制兩條折線
通過(guò)canvas的drawLine方法依次連接兩點(diǎn)即可
在每個(gè)數(shù)據(jù)節(jié)點(diǎn)處繪制一個(gè)小圓,突出顯示
/*繪制第一條折線的路徑*/ for (int i = 0; i < mPerformance_1.length - 1; i++) { /*折線圖的折線的畫(huà)筆設(shè)置粗一點(diǎn)*/ mPaintLine.setStrokeWidth(5); /*計(jì)算當(dāng)前節(jié)點(diǎn)的坐標(biāo)值*/ float prePointX =mLineXs[i]; float prePointY =mLineYs[2] - (mLineYs[2] - mLineYs[mPerformance_1[i].type]) * animCurrentValue; /*計(jì)算下一個(gè)節(jié)點(diǎn)的坐標(biāo)值*/ float nextPointX=mLineXs[i + 1]; float nextPointY=mLineYs[2] - (mLineYs[2] - mLineYs[mPerformance_1[i + 1].type]) * animCurrentValue; /*連接當(dāng)前坐標(biāo)和下一個(gè)坐標(biāo),繪制線段*/ canvas.drawLine(prePointX, prePointY, nextPointX, nextPointY, mPaintLine1); /*當(dāng)前節(jié)點(diǎn)坐標(biāo)處繪制小圓*/ canvas.drawCircle(prePointX, prePointY, mSmallDotRadius, mPointPaint); }
兩條折線重合的地方,需要特殊考慮,比如希望兩條折線重合的地方折線變?yōu)榘咨?/p>
設(shè)置下兩條折線的畫(huà)筆即可
mPaintLine2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN)); mPaintLine1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
測(cè)試代碼及效果;
final Random random=new Random(); final LineChartView myView=(LineChartView)findViewById(R.id.custom_view); final LineChartView.Performance[] performances1=new LineChartView.Performance[8]; final LineChartView.Performance[] performances2=new LineChartView.Performance[8]; myView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ for(int i=0;i完整代碼如下:
public class LineChartView extends View { private Context context; /*動(dòng)畫(huà)插值器*/ DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator(); /*動(dòng)畫(huà)刷新的次數(shù)*/ private int mDuration = 10; /*當(dāng)前動(dòng)畫(huà)進(jìn)度值*/ private int mCurrentTime = 0; private Performance[] mPerformance_1, mPerformance_2; /*兩條折線的顏色*/ private int mLineColor1, mLineColor2; /*繪制表頭文字畫(huà)筆*/ private Paint mPaintText = new Paint(); /*繪制表格的畫(huà)筆*/ private Paint mPaintLine = new Paint(); /*第一條折線的畫(huà)筆*/ private Paint mPaintLine1 =new Paint(); /*第二條折線的畫(huà)筆*/ private Paint mPaintLine2 =new Paint(); /*坐標(biāo)點(diǎn)的小圓點(diǎn)畫(huà)筆*/ private Paint mPointPaint = new Paint(); private float mSmallDotRadius = 4; private TypedValue typedValue; private int mPaintClolor; /*Handler刷新界面產(chǎn)生動(dòng)畫(huà)效果*/ private Handler mHandler = new Handler(); private Runnable mAnimation = new Runnable() { @Override public void run() { if (mCurrentTime < mDuration) { mCurrentTime++; LineChartView.this.invalidate(); } } }; public LineChartView(Context context) { super(context); this.context=context; init(); } public LineChartView(Context context, AttributeSet attrs) { super(context, attrs); this.context=context; init(); } public LineChartView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context=context; init(); } public enum Performance { WIN(0), DRAW(1), LOSE(2); public int type; Performance(int type) { this.type = type; } } public void setPerformances(Performance[] performance1, Performance[] performance2) { if (performance1 == null) { performance1 = new Performance[0]; } if (performance2 == null) { performance2 = new Performance[0]; } mPerformance_1 = Arrays.copyOf(performance1, performance1.length > 8 ? 8 : performance1.length); mPerformance_2 = Arrays.copyOf(performance2, performance2.length > 8 ? 8 : performance2.length); if (isShown()) { mCurrentTime = 0; this.invalidate(); } } /** * 設(shè)置折線1的顏色 * * @param mLineColor1 */ public void setLineColor1(int mLineColor1) { this.mLineColor1 = mLineColor1; } /** * 設(shè)置折線2的顏色 * * @param mLineColor2 */ public void setLineColor2(int mLineColor2) { this.mLineColor2 = mLineColor2; } private void init() { mLineColor1=Color.BLUE; mLineColor2 = Color.GREEN; typedValue=new TypedValue(); context.getTheme().resolveAttribute(R.attr.title_bar,typedValue,true); mPaintClolor =getResources().getColor(typedValue.resourceId); final LineChartView.Performance[] performances1=new LineChartView.Performance[8]; final LineChartView.Performance[] performances2=new LineChartView.Performance[8]; final Random random=new Random(); for(int i=0;i關(guān)于怎么在Android中通過(guò)自定義控件實(shí)現(xiàn)一個(gè)折線圖就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁(yè)題目:怎么在Android中通過(guò)自定義控件實(shí)現(xiàn)一個(gè)折線圖
分享路徑:http://weahome.cn/article/pdheec.html