您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關在Android開發中通過自定義View實現一個圓形進度條,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
首先來看看自己定義的View
package cn.easymobi.application.bell.common; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; public class ProgressView extends View{ private float fArcNum; private float fMax; private float density; public float getDensity() { return density; } public void setDensity(float density) { this.density = density; } public ProgressView(Context context) { super(context); } public ProgressView(Context context,AttributeSet attrs) { super(context,attrs); } protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Paint paint=new Paint(); if(fArcNum>0) { paint.setColor(Color.GRAY); paint.setFlags(Paint.ANTI_ALIAS_FLAG); canvas.drawCircle(40*density/2, 40*density/2, 40*density/2, paint); } paint.setColor(Color.YELLOW); paint.setFlags(Paint.ANTI_ALIAS_FLAG); //paint.setStrokeWidth(2); RectF rect=new RectF(0, 0, 40*density, 40*density); canvas.drawArc(rect, -90, fArcNum,true, paint); paint.setColor(Color.BLACK); paint.setFlags(Paint.ANTI_ALIAS_FLAG); canvas.drawCircle(40*density/2, 40*density/2, 40*density/2-5, paint); } public void setProgress(float num) { fArcNum = (num/fMax) * 360; } public float getfArcNum() { return fArcNum; } public void setfArcNum(float fArcNum) { this.fArcNum = fArcNum; } public float getfMax() { return fMax; } public void setfMax(float fMax) { this.fMax = fMax; } }
我們通過重寫View的onDraw方法,根據fArcNum好fMax來判斷當前播放到的位置,然后不停的刷新改View就實現了這個效果。至于畫弧,是采用了drawArc方法,然后通過在其內部畫圓遮蓋多余部分實現。
下面是MediaPlayer與該View的同步處理,核心代碼如下
// ******************************************************************* // Func: playAudio // // by: Sun // 2011.9.1 // ******************************************************************* public void playAudio(final String path, final ProgressBar pb) { Thread thread = new Thread(new Runnable() { public void run() { try { if (mpMediaPlayer != null) { mpMediaPlayer.stop(); mpMediaPlayer.release(); mpMediaPlayer = null; } mpMediaPlayer = new MediaPlayer(); mpMediaPlayer.setDataSource(path); mpMediaPlayer.prepare(); mpMediaPlayer.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer mp) { pb.setVisibility(ProgressBar.GONE); frontPv.setfMax(mpMediaPlayer.getDuration()); frontPv.setProgress(0); mpMediaPlayer.start(); refrash = new Thread(new Runnable() { public void run() { try { while (frontPv.getfArcNum() <= 360 && mpMediaPlayer.isPlaying()) { if (bIsOver) break; frontPv.setProgress(mpMediaPlayer.getCurrentPosition()); Thread.sleep(1000); mHandle.sendEmptyMessage(MSG_REFRESH_UI); } mHandle.sendEmptyMessage(MSG_PLAY_OVER); } catch (Exception e) { e.printStackTrace(); } } }); refrash.start(); } }); } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); }
其中frontPv是我們自己定義的view,最后發送handler是調用invalidate方法刷新該 view,mpMediaPlayers是我們定義的MediaPlayer對象。我們通過在進程中每隔一秒更新frontPv當中的fArcNum并且 刷新實現轉動的動畫效果。
上述就是小編為大家分享的在Android開發中通過自定義View實現一個圓形進度條了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。