利用Android模仿微信攝像圓環(huán)進(jìn)度效果實(shí)例

前言

創(chuàng)新互聯(lián)專注于紅安網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供紅安營銷型網(wǎng)站建設(shè),紅安網(wǎng)站制作、紅安網(wǎng)頁設(shè)計(jì)、紅安網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造紅安網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供紅安網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

大家在平時(shí)的生活上遇到新奇的事情,都要立即打開微信視頻錄下來發(fā)給朋友看看。這個(gè)錄制進(jìn)度條看起來還不錯(cuò)哦,就仿著寫了一個(gè),不是樣式完全的高仿,是功能的仿制。下面話不多說了,來一起看看詳細(xì)的介紹吧。

微信效果:

利用Android模仿微信攝像圓環(huán)進(jìn)度效果實(shí)例

源碼下載:

github代碼直通車

本地下載

自制效果:

利用Android模仿微信攝像圓環(huán)進(jìn)度效果實(shí)例

實(shí)現(xiàn)過程:

1.自定義圓半徑和圓環(huán)顏色屬性:

 <declare-styleable name="CiclePercentView">
 <attr name="radius" format="integer"/>
 <attr name="ring_color" format="color"/>
 </declare-styleable>

2.設(shè)置3支畫筆,分別畫圓環(huán),背景淺白色,中心白色圓。

 private void init() {
 paint = new Paint();
 paint.setColor(ringColor);
 paint.setStyle(Paint.Style.STROKE);
 paint.setAntiAlias(true);
 paint.setStrokeWidth(14);

 bgPaint = new Paint();
 bgPaint.setAntiAlias(true);
 bgPaint.setColor(getResources().getColor(R.color.halfwhite));

 centerPaint = new Paint();
 centerPaint.setAntiAlias(true);
 centerPaint.setColor(Color.WHITE);

 //起始角度
 startAngle = -90;
 }

3.依次畫背景圓,中心圓,圓弧。canvas.drawArc() ,第一個(gè)參數(shù)表示圓弧外切矩形大小;第二、三個(gè)參數(shù)表示起始角度,當(dāng)前角度,-90度為12點(diǎn)方向,0度為3點(diǎn)方向,這里用-90度作為起始;第四個(gè)參數(shù)表示是否與中心點(diǎn)填充為扇形,false表示只畫圓弧線;

利用Android模仿微信攝像圓環(huán)進(jìn)度效果實(shí)例

畫圓弧drawArc()方法參數(shù)

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);

 //畫圓弧
 RectF rectf = new RectF(6,6,dp2px(radius-2),dp2px(radius-2));
 canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius)/2,bgPaint);
 canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius/3)/2,centerPaint);
 canvas.drawArc(rectf,startAngle,curAngle,false,paint);
 }

4.計(jì)時(shí)器,每100毫秒更新一次進(jìn)度,可設(shè)置拍攝總時(shí)間totalTime;時(shí)間轉(zhuǎn)化為進(jìn)度范圍為0-100;

 public void countDown(final int totalTime){
 countDownTimer = new CountDownTimer(totalTime, (long)(totalTime/100f)) {
  @Override
  public void onTick(long millisUntilFinished) {
  curPercentate = (int) ((totalTime-millisUntilFinished)/(float)totalTime*100);
  percentToAngle(curPercentate);
  }

  @Override
  public void onFinish() {
  curPercentate = 0;
  percentToAngle(curPercentate);
  }
 }.start();
 }

5.按下開始拍攝,只要抬起就完成拍攝,進(jìn)度恢復(fù)為0。

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 switch (event.getAction()){
  case MotionEvent.ACTION_DOWN:
  countDown(countdownTime);
  break;
  case MotionEvent.ACTION_UP:
  countDownTimer.cancel();
  curPercentate = 0;
  percentToAngle(curPercentate);
  break;
 }
 return true;
 }

CiclePercentView類完整代碼:

public class CiclePercentView extends View{
 private Paint paint;
 private int curAngle;
 private int curPercentate;
 private Paint bgPaint,centerPaint;
 private int radius;
 private int ringColor;
 private int startAngle;
 private int countdownTime;
 private CountDownTimer countDownTimer;

 public CiclePercentView(Context context) {
 super(context);
 init();
 }

 public CiclePercentView(Context context, @Nullable AttributeSet attrs) {
 super(context, attrs);

 TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CiclePercentView);
 radius = array.getInt(R.styleable.CiclePercentView_radius,85);
 ringColor = array.getColor(R.styleable.CiclePercentView_ring_color,Color.GREEN);
 array.recycle();

 init();
 }

 private void init() {
 paint = new Paint();
 paint.setColor(ringColor);
 paint.setStyle(Paint.Style.STROKE);
 paint.setAntiAlias(true);
 paint.setStrokeWidth(14);

 bgPaint = new Paint();
 bgPaint.setAntiAlias(true);
 bgPaint.setColor(getResources().getColor(R.color.halfwhite));

 centerPaint = new Paint();
 centerPaint.setAntiAlias(true);
 centerPaint.setColor(Color.WHITE);

 //起始角度
 startAngle = -90;
 }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);

 //畫圓弧
 RectF rectf = new RectF(6,6,dp2px(radius-2),dp2px(radius-2));
 canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius)/2,bgPaint);
 canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius/3)/2,centerPaint);
 canvas.drawArc(rectf,startAngle,curAngle,false,paint);
 }

 private void percentToAngle(int percentage){
 curAngle = (int) (percentage/100f*360);
 invalidate();
 }

 public void setCountdownTime(int countdownTime){
 this.countdownTime = countdownTime;
 }

 public void countDown(final int totalTime){
 countDownTimer = new CountDownTimer(totalTime, (long)(totalTime/100f)) {
  @Override
  public void onTick(long millisUntilFinished) {
  curPercentate = (int) ((totalTime-millisUntilFinished)/(float)totalTime*100);
  percentToAngle(curPercentate);
  }

  @Override
  public void onFinish() {
  curPercentate = 0;
  percentToAngle(curPercentate);
  }
 }.start();
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 switch (event.getAction()){
  case MotionEvent.ACTION_DOWN:
  countDown(countdownTime);
  break;
  case MotionEvent.ACTION_UP:
  countDownTimer.cancel();
  curPercentate = 0;
  percentToAngle(curPercentate);
  break;
 }
 return true;
 }

 private int dp2px(int dp){
 return (int) (getContext().getResources().getDisplayMetrics().density*dp + 0.5);
 }
}

附:Android Canvas drawArc方法介紹

public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

  • oval :指定圓弧的外輪廓矩形區(qū)域。
  • startAngle: 圓弧起始角度,單位為度。
  • sweepAngle: 圓弧掃過的角度,順時(shí)針方向,單位為度。
  • useCenter: 如果為True時(shí),在繪制圓弧時(shí)將圓心包括在內(nèi),通常用來繪制扇形。
  • paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。

下面演示drawArc的四種不同用法,
1. 填充圓弧但不含圓心:

mPaints[0] = new Paint();
mPaints[0].setAntiAlias(true);
mPaints[0].setStyle(Paint.Style.FILL);
mPaints[0].setColor(0x88FF0000);
mUseCenters[0] = false;

2. 填充圓弧帶圓心(扇形)

mPaints[1] = new Paint(mPaints[0]);
mPaints[1].setColor(0x8800FF00);
mUseCenters[1] = true;

3. 只繪圓周,不含圓心

mPaints[2] = new Paint(mPaints[0]);
mPaints[2].setStyle(Paint.Style.STROKE);
mPaints[2].setStrokeWidth(4);
mPaints[2].setColor(0x880000FF);
mUseCenters[2] = false;

4. 只繪圓周,帶圓心(扇形)

mPaints[3] = new Paint(mPaints[2]);
mPaints[3].setColor(0x88888888);
mUseCenters[3] = true;

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。  

新聞標(biāo)題:利用Android模仿微信攝像圓環(huán)進(jìn)度效果實(shí)例
網(wǎng)頁網(wǎng)址:http://muchs.cn/article48/ipgiep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)微信小程序、網(wǎng)站設(shè)計(jì)、ChatGPT小程序開發(fā)、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)