怎么在android中通過自定義view實現(xiàn)一個數(shù)字進(jìn)度條?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、成都做網(wǎng)站、廣南網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、廣南網(wǎng)絡(luò)營銷、廣南企業(yè)策劃、廣南品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供廣南建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:aaarwkj.com
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
package com.tuya; import android.animation.ValueAnimator; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; public class DownLoadProgressView extends View { private Paint paint;//繪制進(jìn)度條畫筆 private Paint textPaint;//繪制文字畫筆 private Paint dottePaint;//繪制灰色線畫筆 private int width; private int height; private int padding =5; private int value = 0; public DownLoadProgressView(Context context) { this(context,null); } public DownLoadProgressView(Context context, AttributeSet attrs) { this(context, attrs,0); } public DownLoadProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaint(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; } /** * 初始化畫筆 */ private void initPaint() { paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(2); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLUE); textPaint = new Paint(); textPaint.setAntiAlias(true); textPaint.setStrokeWidth(3); textPaint.setStyle(Paint.Style.FILL); textPaint.setColor(Color.BLUE); textPaint.setTextSize(12); dottePaint = new Paint(); dottePaint.setAntiAlias(true); dottePaint.setStrokeWidth(2); dottePaint.setStyle(Paint.Style.FILL); dottePaint.setColor(Color.parseColor("#e5e5e5")); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); String str = value+"%"; float strWidth = textPaint.measureText(value+"%")+padding;//繪制文字的寬度 +padding是為了防止在進(jìn)度條加載完畢的時候文字繪制出現(xiàn)被切掉情況 Rect rect = new Rect(); textPaint.getTextBounds(str,0,str.length(),rect); canvas.drawLine(0,height/2,value*((width-strWidth)/100),height/2,paint);//繪制進(jìn)度 canvas.drawText(value+"%",value*((width-strWidth)/100)+padding,(height-rect.height())/2+2*padding,textPaint);//繪制進(jìn)度文字 這個高度+2*padding是因為drawText是根據(jù)基線計算的,要準(zhǔn)確的話要去求基線 canvas.drawLine(value*((width-strWidth)/100)+strWidth+padding,height/2,width,height/2,dottePaint);//繪制灰色進(jìn)度表示剩余多少 postDelayed(new Runnable() { @Override public void run() { if(value<100){ value++; postInvalidate(); } } },100); } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#7EC0EE"> <com.tuya.DownLoadProgressView android:id="@+id/dpv" android:layout_width="fill_parent" android:layout_height="30dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="60dp" ></com.tuya.DownLoadProgressView> </RelativeLayout>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
當(dāng)前名稱:怎么在android中通過自定義view實現(xiàn)一個數(shù)字進(jìn)度條
文章鏈接:http://aaarwkj.com/article10/ihhpgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、微信公眾號、域名注冊、搜索引擎優(yōu)化、商城網(wǎng)站、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)