Android中怎么通過自定義view實(shí)現(xiàn)進(jìn)度條加載效果,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、順城ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的順城網(wǎng)站制作公司
分析圖:
代碼如下:
package com.example.dotloadview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Handler; import android.util.AttributeSet; import android.view.View; /** * Created by zhouguizhi on 6/18/15. */ public class DotLoadProgressView extends View { private boolean isStart = false; private int dotIndex =0;//點(diǎn)下面的角標(biāo) private int dotNumber = 0; private Paint mPaint; private int paddingTop = 50; private int paddingBottom = 50; private int paddingLeft = 30; private int paddingRight = 30; private Handler mHandler = new Handler(); private long DELAY_TIME = 300; private Bitmap bitmap; private int itemDotSpace = 10;//二個(gè)點(diǎn)之間的距離 private Bitmap moveBitmap ; private Runnable mRunnable = new Runnable() { @Override public void run() { if (dotNumber != 0){ dotIndex = (dotIndex + 1) % dotNumber; } invalidate(); mHandler.postDelayed(mRunnable, DELAY_TIME); } }; public DotLoadProgressView(Context context, AttributeSet attrs) { super(context, attrs); moveBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.active_dot); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Paint.Style.FILL); mPaint.setDither(true); mPaint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for(int i=0;i<dotNumber;i++){ canvas.drawBitmap(bitmap,paddingLeft+i*itemDotSpace+bitmap.getWidth()*i,paddingTop,mPaint); } if(isStart){ canvas.drawBitmap(moveBitmap,paddingLeft+dotIndex*itemDotSpace+moveBitmap.getWidth()*dotIndex,paddingTop,mPaint); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int parentWidth = MeasureSpec.getSize(widthMeasureSpec); bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.inactive_dot); setMeasuredDimension(parentWidth,bitmap.getHeight()+paddingTop+paddingBottom); int widthWithoutPadding = parentWidth - paddingLeft - paddingRight; dotNumber = calculateDotsNumber(widthWithoutPadding); } private int calculateDotsNumber(int width) { int number = (width / (itemDotSpace + bitmap.getWidth())); return number; } public void startProgress() { isStart = true; dotIndex = -1; mHandler.removeCallbacks(mRunnable); mHandler.post(mRunnable); } public void stopProgress() { isStart = false; mHandler.removeCallbacks(mRunnable); invalidate(); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); startProgress(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); stopProgress(); } }
布局代碼:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.example.dotloadview.DotLoadProgressView android:id="@+id/progress1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
關(guān)于Android中怎么通過自定義view實(shí)現(xiàn)進(jìn)度條加載效果問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
分享名稱:Android中怎么通過自定義view實(shí)現(xiàn)進(jìn)度條加載效果
分享URL:http://www.rwnh.cn/article20/jeecjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、動(dòng)態(tài)網(wǎng)站、App開發(fā)、網(wǎng)站收錄、搜索引擎優(yōu)化、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)