Android仿小米安全中心检测进度条效果

模仿小米安全中心检测效果

废话少说,咱们先上效果图:

github地址: https://github.com/niniloveyou/GradeProgressView

这个效果的使用场景并不多,主要是各种检测的时候,比如垃圾清理,手机安全检测, 当然如果你不嫌弃这个效果丑, 也可以用作进度条。哈哈。

下面说点干货分析下这个效果怎么实现:

拿到这个效果首先想想主要有哪些技术难点:

1.进度条

2.中间的指针怎么弄

1.进度条

有人说进度条还不容易吗? 就这样写:

mPaint.setPathEffect(new DashPathEffect(new float[]{dashWith, dashSpace}, 。。。));

canvas.drawArc(mRectF, 135, 270, false, mPaint);
mPaint.setColor(Color.WHITE);
canvas.drawArc(mRectF, 135, degree, false, mPaint);

设置个PathEffect
然后画个圆弧,给画笔设置颜色然后根据进度,算出角度, 然后再画出一个圆弧,覆盖第一个圆弧的部分不就行了。废话这么多。
不过我想说的too young too simple. 当时我也是这样想的,于是就实现吧! 做好了先画个50% (也就是第二个圆弧覆盖第一个圆弧的一半)试试,不错啊perfect看来是这样的, 再来个30%试试尼玛不对啊, 怎么小格子没有重合,有点错位啊。MDZZ

后来想了一个简单点的办法,不覆盖,画两个圆弧, 但是这两个圆弧是对接起来的。 比如第一个圆弧,画一半,第二个画一半。

//draw background arc
canvas.drawArc(mRectF, 135 + degree, 270 - degree, false, mPaint);

//draw progress arc
canvas.drawArc(mRectF, 135, degree, false, mProgressPaint);

2.中间的指针怎么弄

先画出指针的路径

mPointerPath = new Path();
mPointerPath.moveTo(centerX + pointRadius, centerY - 7);
mPointerPath.lineTo(centerX + pointRadius, centerY + 7);
mPointerPath.lineTo(mRectF.right - pointGap - lineWidth / 2,centerY);
mPointerPath.lineTo(centerX + pointRadius, centerY - 7);
mPointerPath.close();

在中心draw一个小圆
然后draw指针,这样当画布旋转时指针自然也就旋转了,不懂得要去看看canvas.save(), canvas.restore()的作用

 //draw pointer
canvas.drawCircle(centerX, centerY, pointRadius,mInnerCirclePaint);
canvas.save();
canvas.rotate(135 + degree, centerX, centerY);
canvas.drawPath(mPointerPath, mPointerPaint);
canvas.restore();

下面上完整代码:

package deadline.grade;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.IntRange;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

/**
 * @author deadline
 * @time 2016/9/24
 */
public class GradeProgressView extends View {

 private static final String TAG = GradeProgressView.class.getSimpleName();

 private static final int DEFAULT_PROGRESS_COLOR = Color.WHITE;
 private static final int DEFAULT_BACKGROUND_COLOR = 0x5AFFFFFF;

 private int mBackgroundColor = DEFAULT_BACKGROUND_COLOR;
 private int mProgressColor = DEFAULT_PROGRESS_COLOR;

 //进度条的每格线宽,间距,长度
 private int dashWith = 4;
 private int dashSpace = 6;
 private int lineWidth = 60;

 //最外圈线的宽度和与进度条之间的间距
 private int outLineWidth = 5;
 private int gapWidth = 25;

 //指针的线宽度, 半径, 以及指针与进度条的间距
 private int pointLineWidth = 10;
 private int pointRadius = 25;
 private int pointGap = 20;

 private int mProgress = 0;

 //外线
 private RectF mOuterRectF;
 private Paint mOuterPaint;

 //进度条
 private RectF mRectF;
 private Paint mPaint;
 private Paint mProgressPaint;

 //指针
 private Paint mInnerCirclePaint;
 private Paint mPointerPaint;
 private Path mPointerPath;

 private float centerX;
 private float centerY;

 private ValueAnimator animator;

 private OnProgressChangeListener mListener;

 public GradeProgressView(Context context) {
 this(context, null);
 }

 public GradeProgressView(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }

 public GradeProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 setup();
 }

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 public GradeProgressView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
 super(context, attrs, defStyleAttr, defStyleRes);
 }

 private void setup() {

 mRectF = new RectF();
 mOuterRectF = new RectF();

 mOuterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mOuterPaint.setStrokeWidth(outLineWidth);
 mOuterPaint.setColor(mBackgroundColor);
 mOuterPaint.setStyle(Paint.Style.STROKE);

 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mPaint.setStrokeWidth(lineWidth);
 mPaint.setColor(mBackgroundColor);
 mPaint.setStyle(Paint.Style.STROKE);
 mPaint.setPathEffect(new DashPathEffect(new float[]{dashWith, dashSpace}, dashSpace));

 mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mProgressPaint.setStrokeWidth(lineWidth);
 mProgressPaint.setColor(mProgressColor);
 mProgressPaint.setStyle(Paint.Style.STROKE);
 mProgressPaint.setPathEffect(new DashPathEffect(new float[]{dashWith, dashSpace}, dashSpace));

 mPointerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mPointerPaint.setStrokeWidth(pointLineWidth / 2);
 mPointerPaint.setColor(mProgressColor);
 mPointerPaint.setStyle(Paint.Style.FILL_AND_STROKE);
 mPointerPaint.setStrokeCap(Paint.Cap.ROUND);
 mPointerPaint.setShadowLayer(4, 3, 0, 0x20000000);

 mInnerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mInnerCirclePaint.setStrokeWidth(pointLineWidth);
 mInnerCirclePaint.setColor(mProgressColor);
 mInnerCirclePaint.setStyle(Paint.Style.STROKE);
 mInnerCirclePaint.setShadowLayer(4, 3, 0, 0x20000000);
 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
 super.onSizeChanged(w, h, oldw, oldh);

 int value = outLineWidth / 2;
 mOuterRectF.set(value, value, w - value, h - value);

 int gap = lineWidth / 2 + outLineWidth + gapWidth;

 mRectF.set(mOuterRectF.left + gap,
  mOuterRectF.top + gap,
  mOuterRectF.right - gap,
  mOuterRectF.bottom - gap);

 centerX = mRectF.centerX();
 centerY = mRectF.centerY();

 mPointerPath = new Path();
 mPointerPath.moveTo(centerX + pointRadius, centerY - 7);
 mPointerPath.lineTo(centerX + pointRadius, centerY + 7);
 mPointerPath.lineTo(mRectF.right - pointGap - lineWidth / 2, centerY);
 mPointerPath.lineTo(centerX + pointRadius, centerY - 7);
 mPointerPath.close();
 }

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

 float degree = 2.7f * mProgress;

 //draw out arc
 canvas.drawArc(mOuterRectF, 135, 270, false, mOuterPaint);

 //draw background arc
 canvas.drawArc(mRectF, 135 + degree, 270 - degree, false, mPaint);

 //draw progress arc
 canvas.drawArc(mRectF, 135, degree, false, mProgressPaint);

 //draw pointer
 canvas.drawCircle(centerX, centerY, pointRadius, mInnerCirclePaint);

 canvas.save();
 canvas.rotate(135 + degree, centerX, centerY);
 canvas.drawPath(mPointerPath, mPointerPaint);
 canvas.restore();
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);

 int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
 int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
 setMeasuredDimension(Math.min(measureHeight, measureWidth), Math.min(measureHeight, measureWidth));
 }

 public void setOnProgressChangeListener(OnProgressChangeListener listener){
 this.mListener = listener;
 }

 public int getProgressColor() {
 return mProgressColor;
 }

 public void setProgressColor(int progressColor) {
 this.mProgressColor = progressColor;
 if(mProgressPaint != null){
  mProgressPaint.setColor(mProgressColor);
 }

 if(mPointerPaint != null){
  mPointerPaint.setColor(mProgressColor);
 }

 if(mInnerCirclePaint != null){
  mInnerCirclePaint.setColor(mProgressColor);
 }
 postInvalidate();
 }

 public int getBackgroundColor() {
 return mBackgroundColor;
 }

 public void setBackgroundColor(int backgroundColor) {

 this.mBackgroundColor = backgroundColor;
 if(mPaint != null){
  mPaint.setColor(mBackgroundColor);
 }

 if(mOuterPaint != null){
  mOuterPaint.setColor(mBackgroundColor);
 }
 postInvalidate();
 }

 public int getLineWidth() {
 return lineWidth;
 }

 public void setLineWidth(int lineWidth) {
 this.lineWidth = lineWidth;
 if(mPaint != null){
  mPaint.setStrokeWidth(lineWidth);
 }

 if(mProgressPaint != null){
  mProgressPaint.setStrokeWidth(lineWidth);
 }
 postInvalidate();
 }

 public int getOutLineWidth() {
 return outLineWidth;
 }

 public void setOutLineWidth(int outLineWidth) {
 this.outLineWidth = outLineWidth;
 if(mOuterPaint != null){
  mOuterPaint.setStrokeWidth(outLineWidth);
 }
 postInvalidate();
 }

 public int getGapWidth() {
 return gapWidth;
 }

 public void setGapWidth(int gapWidth) {
 this.gapWidth = gapWidth;
 }

 public int getProgress() {
 return mProgress;
 }

 public void setProgress(@IntRange(from = 0, to = 100) int progress) {
 if(progress > 100){
  progress = 100;
 }

 if(progress < 0){
  progress = 0;
 }
 this.mProgress = progress;
 if(mListener != null){
  mListener.onProgressChanged(GradeProgressView.this, mProgress);
 }
 postInvalidate();
 }

 public void setProgressWidthAnimation(@IntRange(from = 0, to = 100) int progress){

 if(progress > 100){
  progress = 100;
 }

 if(progress < 0){
  progress = 0;
 }

 if(animator != null && animator.isRunning()){
  animator.cancel();
  animator = null;
 }

 animator = ValueAnimator.ofInt(mProgress, progress);
 int duration = 10 * Math.abs(progress - mProgress);
 animator.setDuration(duration);

 animator.setInterpolator(new AccelerateDecelerateInterpolator());
 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator valueAnimator) {
  int value = (int)valueAnimator.getAnimatedValue();
  if(mProgress != value) {
   mProgress = value;
   if(mListener != null){
   mListener.onProgressChanged(GradeProgressView.this, mProgress);
   }
   postInvalidate();
  }
  }
 });
 animator.start();

 }

 @Override
 protected void onDetachedFromWindow() {
 super.onDetachedFromWindow();

 if(animator != null){
  animator.cancel();
  animator = null;
 }

 }

 public interface OnProgressChangeListener{

 void onProgressChanged(GradeProgressView gradeProgressView, int progress);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android仿水波纹流量球进度条控制器

    仿水波纹流球进度条控制器,Android实现高端大气的主流特效,供大家参考,具体内容如下 效果图: CircleView 这里主要是实现中心圆以及水波特效 package com.lgl.circleview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.gra

  • Android带进度的圆形进度条

    我们还是用一个小例子来看看自定义View和自定义属性的使用,带大家来自己定义一个带进度的圆形进度条,我们还是先看一下效果吧 从上面可以看出,我们可以自定义圆环的颜色,圆环进度的颜色,是否显示进度的百分比,进度百分比的颜色,以及进度是实心还是空心等等,这样子是不是很多元化很方便呢?接下来我们就来教大家怎么来定义 1.在values下面新建一个attrs.xml,现在里面定义我们的属性,不同的属性对应不同的format,接下来我贴上我在自定义这个进度条所用到的属性 <?xml version="

  • Android文件下载进度条的实现代码

    main.xml: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_paren

  • Android ProgressBar进度条使用详解

    ProgressBar进度条,分为旋转进度条和水平进度条,进度条的样式根据需要自定义,之前一直不明白进度条如何在实际项目中使用,网上演示进度条的案例大多都是通过Button点击增加.减少进度值,使用方法incrementProgressBy(int),最简单的做法是在xml布局文件中放置ProgressBar空间,然后再MainActivity中触发事件后执行incrementProgressBy(int),代码如下: <LinearLayout xmlns:android="http:/

  • android自定义进度条渐变色View的实例代码

    最近在公司,项目不是很忙了,偶尔看见一个兄台在CSDN求助,帮忙要一个自定义的渐变色进度条,我当时看了一下进度条,感觉挺漂亮的,就尝试的去自定义view实现了一个,废话不说,先上图吧! 这个自定义的view,完全脱离了android自带的ProgressView,并且没使用一张图片,这样就能更好的降低程序代码上的耦合性! 下面我贴出代码  ,大概讲解一下实现思路吧! 复制代码 代码如下: package com.spring.progressview; import android.conten

  • Android实现自定义圆形进度条

    今天无意中发现一个圆形进度,想想自己实现一个,如下图: 基本思路是这样的: 1.首先绘制一个实心圆 2.绘制一个白色实心的正方形,遮住实心圆 3.在圆的中心动态绘制当前进度的百分比字符 4.绘制一个与之前实心圆相同颜色的空心圆 5.逐渐改变当前的百分比 6.根据百分比,逐渐改变正方形的大小,逐渐减小正方形的底部y轴的坐标,不断重绘,直到达到100% 首先看看自定义的属性 在values目录下新建attrs.xml内容如下: 定义绘制圆形的背景色,和绘制圆形的半径大小 <?xml version=

  • 实例详解Android自定义ProgressDialog进度条对话框的实现

    Android SDK已经提供有进度条组件ProgressDialog组件,但用的时候我们会发现可能风格与我们应用的整体风格不太搭配,而且ProgressDialog的可定制行也不太强,这时就需要我们自定义实现一个ProgressDialog. 通过看源码我们发现,ProgressDialog继承自Alertdialog,有一个ProgressBar和两个TextView组成的,通过对ProgressDialog的源码进行改进就可以实现一个自定义的ProgressDialog. 1.效果: 首先

  • Android实现支持进度条显示的短信备份工具类

    使用内容提供者读取短信内容,写入XML文件,进度条ProgressDialog更新备份进度. 新知识点:子线程如何在在不使用Handler的情况下更新UI /** * 进行短信备份的工具类,支持进度条显示 * @author lian * */ public class SmsBackupUtils { private static class Data{ int progress; } /** * * @param context * 调用此工具类的Activity * @param pd *

  • Android中实现Webview顶部带进度条的方法

    写这篇文章,做份备忘,简单滴展示一个带进度条的Webview示例,进度条位于Webview上面. 示例图如下: 主Activity代码: 复制代码 代码如下: package com.droidyue.demo.webviewprogressbar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.vi

  • Android ProgressDialog进度条使用详解

    进度条以一种客观化的方式,让我们知道程序正在执行的情况,在程序需要时间执行任务的时候,提示进度条友好的告诉用户说,当前任务还没有完成,请稍稍等待,进度条经常使用在APP下载应用.更新应用.加载网络数据中,使用频繁,常见的进度条有ProgressDialog.ProgressBar,这里只要介绍ProgressDialog. ProgressDialog读取文件进度解析主要涉及到一个知识: Handler消息处理机制, 文件字节流的操作, ProgressDialog类的使用 1.Handler消

随机推荐