您好,登錄后才能下訂單哦!
Android中怎么通過自定義view實現進度條加載效果,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
分析圖:
代碼如下:
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;//點下面的角標 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;//二個點之間的距離 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>
關于Android中怎么通過自定義view實現進度條加載效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。