91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用Android模仿微信攝像圓環進度效果實例

發布時間:2020-09-02 09:02:14 來源:腳本之家 閱讀:168 作者:翻滾吧李博 欄目:移動開發

前言

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

微信效果:

利用Android模仿微信攝像圓環進度效果實例

源碼下載:

github代碼直通車

本地下載

自制效果:

利用Android模仿微信攝像圓環進度效果實例

實現過程:

1.自定義圓半徑和圓環顏色屬性:

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

2.設置3支畫筆,分別畫圓環,背景淺白色,中心白色圓。

 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() ,第一個參數表示圓弧外切矩形大小;第二、三個參數表示起始角度,當前角度,-90度為12點方向,0度為3點方向,這里用-90度作為起始;第四個參數表示是否與中心點填充為扇形,false表示只畫圓弧線;

利用Android模仿微信攝像圓環進度效果實例

畫圓弧drawArc()方法參數

 @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.計時器,每100毫秒更新一次進度,可設置拍攝總時間totalTime;時間轉化為進度范圍為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.按下開始拍攝,只要抬起就完成拍攝,進度恢復為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 :指定圓弧的外輪廓矩形區域。
  • startAngle: 圓弧起始角度,單位為度。
  • sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
  • useCenter: 如果為True時,在繪制圓弧時將圓心包括在內,通常用來繪制扇形。
  • 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;

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。  

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

宿州市| 若尔盖县| 会宁县| 庄浪县| 留坝县| 邛崃市| 雷波县| 郑州市| 洪湖市| 大庆市| 克什克腾旗| 富民县| 宣城市| 谢通门县| 进贤县| 汽车| 东台市| 南充市| 嘉峪关市| 德惠市| 舞钢市| 荔波县| 新竹县| 虎林市| 交口县| 杨浦区| 南城县| 商城县| 闽清县| 仙居县| 马鞍山市| 余姚市| 东乌| 沂南县| 吉林省| 西青区| 米易县| 金湖县| 盐亭县| 东港市| 四川省|