这期内容当中小编将会给大家带来有关在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。