首页 web前端 H5教程 Android自定义环形LoadingView效果

Android自定义环形LoadingView效果

Jul 03, 2018 am 09:36 AM
android

这篇文章主要为大家详细介绍了Android自定义环形LoadingView效果的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近项目有要用到环形的进度条,Github上有一个类似的DashedCircularProgress控件,但是他画的进度是通过设置画笔的虚线效果来实现间隔的:progressPaint.setPathEffect(new DashPathEffect(new float[]{dashWith, dashSpace}, dashSpace));如果内层还有一层圆环,在动态设置时,内层和外层有细微的偏差.于是我在原有基础上改了一个,实现了我要的效果(设置进度时可以选择加动画或者不加动画):

控件实现:

这个控件继承RelativeLayout,在onDraw时做了两件事:

1、先画出底部的黑色环形;
2、按照当时的进度值画出对应比例的外层绿色环形.

对外提供一个接口,回调当前进度值:

1

2

3

public interface OnValueChangeListener {

  void onValueChange(float value);

}

登录后复制

核心绘制类:

InternalCirclePainterImp2,绘制内层的黑色的环形:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

/**

 * @author Chuck

 */

public class InternalCirclePainterImp2 implements InternalCirclePainter {

 

  private RectF internalCircle;//画出圆弧时,圆弧的外切矩形

  private Paint internalCirclePaint;

  private int color;

  private float startAngle = 270f;

  int arcQuantity=100;//等分(圆弧加间隔),比如arcQuantity=100时,表示将有100个圆弧,和100个空白间隔

  float ratio=0.5f;//每段圆弧与圆弧加间隔之和的比例,ratio=0.5表示每个圆弧与相邻的间隔弧度比是1:1

  private int width;

  private int height;

  private int internalStrokeWidth = 48;//圆环宽度

 

  public InternalCirclePainterImp2(int color, int progressStrokeWidth, int arcQuantity,float ratio) {

    this.color = color;

    this.internalStrokeWidth = progressStrokeWidth;

    this.arcQuantity = arcQuantity;

    if(ratio>0&&ratio<1){

      this.ratio = ratio;

    }

 

    init();

  }

 

  private void init() {

    initExternalCirclePainter();

  }

 

  private void initExternalCirclePainter() {

    internalCirclePaint = new Paint();

    internalCirclePaint.setAntiAlias(true);

    internalCirclePaint.setStrokeWidth(internalStrokeWidth);

    internalCirclePaint.setColor(color);

    internalCirclePaint.setStyle(Paint.Style.STROKE);

 

  }

 

  //圆弧外切矩形

  private void initExternalCircle() {

    internalCircle = new RectF();

    float padding = internalStrokeWidth * 0.5f;

    internalCircle.set(padding, padding , width - padding, height - padding);

    initExternalCirclePainter();

  }

 

 

  @Override

  public void draw(Canvas canvas) {

 

    float eachAngle=360f/arcQuantity;

 

    float eachArcAngle=eachAngle*ratio;

 

    for(int i=0;i<arcQuantity*2;i++){

      if(i%2==0){//遇到偶数就画圆弧,基数则跳过

        canvas.drawArc(internalCircle, startAngle+eachAngle*i/2, eachArcAngle, false, internalCirclePaint);

      }

      else{

        continue;

      }

    }

 

  }

 

  public void setColor(int color) {

    this.color = color;

    internalCirclePaint.setColor(color);

  }

 

  @Override

  public int getColor() {

    return color;

  }

 

  @Override

  public void onSizeChanged(int height, int width) {

    this.width = width;

    this.height = height;

    initExternalCircle();

  }

}

登录后复制

ProgressPainterImp2,绘制内层的黑色的环形:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

/**

 * @author Chuck

 */

public class ProgressPainterImp2 implements ProgressPainter {

 

  private RectF progressCircle;

  private Paint progressPaint;

  private int color = Color.RED;

  private float startAngle = 270f;

  private int internalStrokeWidth = 48;

  private float min;

  private float max;

  private int width;

  private int height;

 

  private int currentPecent;//当前的百分比

 

  int arcQuantity=100;//等分(圆弧加间隔),比如arcQuantity=100时,表示将有100个圆弧,和100个空白间隔

  float ratio=0.5f;//每段圆弧与圆弧加间隔之和的比例,ratio=0.5表示每个圆弧与相邻的间隔弧度比是1:1

 

  public ProgressPainterImp2(int color, float min, float max, int progressStrokeWidth, int arcQuantity,float ratio) {

    this.color = color;

    this.min = min;

    this.max = max;

    this.internalStrokeWidth = progressStrokeWidth;

    this.arcQuantity = arcQuantity;

    this.ratio = ratio;

    init();

    Log.e("ProgressPainterImp","构造函数执行");

  }

 

  private void init() {

    initInternalCirclePainter();

 

  }

 

  private void initInternalCirclePainter() {

    progressPaint = new Paint();

    progressPaint.setAntiAlias(true);

    progressPaint.setStrokeWidth(internalStrokeWidth);

    progressPaint.setColor(color);

    progressPaint.setStyle(Paint.Style.STROKE);

 

  }

 

  //初始化外切的那个矩形

  private void initInternalCircle() {

    progressCircle = new RectF();

    float padding = internalStrokeWidth * 0.5f;

    progressCircle.set(padding, padding , width - padding, height - padding);

 

    initInternalCirclePainter();

  }

 

  @Override

  public void draw(Canvas canvas) {

 

    float eachAngle=360f/arcQuantity;

 

    float eachArcAngle=eachAngle*ratio;

 

    int quantity=2*arcQuantity*currentPecent/100;

    for(int i=0;i<quantity;i++){

      if(i%2==0){//遇到偶数就画圆弧,基数则跳过

        canvas.drawArc(progressCircle, startAngle+eachAngle*i/2, eachArcAngle, false, progressPaint);

      }

      else{

        continue;

      }

    }

  }

 

  public float getMin() {

    return min;

  }

 

  public void setMin(float min) {

    this.min = min;

  }

 

  public float getMax() {

    return max;

  }

 

  public void setMax(float max) {

    this.max = max;

  }

 

  public void setValue(float value) {

    this.currentPecent = (int) (( 100f * value) / max);

  }

 

  @Override

  public void onSizeChanged(int height, int width) {

    Log.e("ProgressPainterImp","onSizeChanged执行");

 

    this.width = width;

    this.height = height;

    initInternalCircle();

  }

 

  public int getColor() {

    return color;

  }

 

  public void setColor(int color) {

    this.color = color;

    progressPaint.setColor(color);

  }

}

登录后复制

可以自定义的属性:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<declare-styleable name="CircularLoadingView">

  <attr name="base_color" format="color" /> <!--内层圆环的颜色-->

  <attr name="progress_color" format="color" /><!--进度圆环的颜色-->

  <attr name="max" format="float" /><!--最小值-->

  <attr name="min" format="float" /><!--最大值-->

  <attr name="duration" format="integer" /><!--动画时长-->

  <attr name="progress_stroke_width" format="integer" /><!--圆环宽度-->

 

  <!--等分(圆弧加间隔),比如arcQuantity=100时,表示将有100个圆弧,和100个空白间隔-->

  <attr name="argQuantity" format="integer" />

   

  <!--每段圆弧与圆弧加间隔之和的比例,ratio=0.5表示每个圆弧与相邻的间隔弧度比是1:1-->

  <attr name="ratio" format="float" />

</declare-styleable>

登录后复制

调用:

main_activity.xml:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:paddingBottom="@dimen/activity_vertical_margin"

  android:paddingLeft="@dimen/activity_horizontal_margin"

  android:paddingRight="@dimen/activity_horizontal_margin"

  android:paddingTop="@dimen/activity_vertical_margin"

  xmlns:custom="http://schemas.android.com/apk/res-auto"

  android:background="#ffffff"

  >

 

  <!--自定义控件,继承RelativeLayout-->

  <qdong.com.mylibrary.CircularLoadingView

    android:id="@+id/simple"

    custom:base_color="@color/pager_bg"

    custom:min="0"

    custom:max="100"

    custom:argQuantity="100"

    custom:ratio="0.6"

    custom:progress_color="@android:color/holo_green_light"

    custom:progress_icon="@mipmap/ic_launcher"

    custom:duration="1000"

    custom:progress_stroke_width="28"

    android:layout_centerInParent="true"

    android:layout_width="200dp"

    android:layout_height="200dp">

 

    <RelativeLayout

      android:layout_centerInParent="true"

      android:layout_width="match_parent"

      android:layout_height="match_parent">

       

 

        <TextView

          android:layout_centerInParent="true"

 

          android:textSize="20sp"

          android:layout_centerHorizontal="true"

          android:id="@+id/number"

          android:text="0"

          android:gravity="center"

          android:textColor="@color/pager_bg"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content" />

 

    </RelativeLayout>

 

  </qdong.com.mylibrary.CircularLoadingView>

 

  <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Set_Value"

    android:id="@+id/button"

    android:layout_alignParentBottom="true"

    android:layout_alignParentStart="true"/>

 

  <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Animation"

    android:id="@+id/button3"

    android:layout_alignTop="@+id/button"

    android:layout_alignParentEnd="true"/>

</RelativeLayout>

登录后复制

MainActivity:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View view) {

    try {

      mDashedCircularProgress.setValue(66);//没有动画的,直接设置

    } catch (Exception e) {

      e.printStackTrace();

    }

  }

});

findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View view) {

    try {

      mDashedCircularProgress.setValue(0);//无动画,归零

      mDashedCircularProgress.setValueWithAnimation(100,2000);//带动画

    } catch (Exception e) {

      e.printStackTrace();

    }

  }

});

登录后复制

Github地址:https://github.com/506954774/AndroidCircularLoadingView

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

jQuery实现的AJAX简单弹出层效果

jQuery的ajax下载blob文件的方法

以上是Android自定义环形LoadingView效果的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1669
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
新报告对传闻中的三星 Galaxy S25、Galaxy S25 Plus 和 Galaxy S25 Ultra 相机升级进行了严厉的评估 新报告对传闻中的三星 Galaxy S25、Galaxy S25 Plus 和 Galaxy S25 Ultra 相机升级进行了严厉的评估 Sep 12, 2024 pm 12:23 PM

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

三星 Galaxy S25 Ultra 泄露了第一张渲染图,传闻中的设计变化被曝光 三星 Galaxy S25 Ultra 泄露了第一张渲染图,传闻中的设计变化被曝光 Sep 11, 2024 am 06:37 AM

OnLeaks 现在与 Android Headlines 合作,首次展示了 Galaxy S25 Ultra,几天前,他试图从他的 X(以前的 Twitter)粉丝那里筹集到 4,000 美元以上的资金,但失败了。对于上下文,嵌入在 h 下面的渲染图像

IFA 2024 | TCL 的 NXTPAPER 14 在性能上无法与 Galaxy Tab S10 Ultra 相媲美,但在尺寸上几乎可以与之媲美 IFA 2024 | TCL 的 NXTPAPER 14 在性能上无法与 Galaxy Tab S10 Ultra 相媲美,但在尺寸上几乎可以与之媲美 Sep 07, 2024 am 06:35 AM

除了发布两款新智能手机外,TCL 还发布了一款名为 NXTPAPER 14 的新 Android 平板电脑,其大屏幕尺寸是其卖点之一。 NXTPAPER 14 采用 TCL 标志性品牌哑光液晶面板 3.0 版本

Vivo Y300 Pro 在 7.69 毫米纤薄机身中配备 6,500 mAh 电池 Vivo Y300 Pro 在 7.69 毫米纤薄机身中配备 6,500 mAh 电池 Sep 07, 2024 am 06:39 AM

Vivo Y300 Pro刚刚全面亮相,它是最薄的中端Android手机之一,配备大电池。准确来说,这款智能手机的厚度仅为 7.69 毫米,但配备了 6,500 mAh 的电池。这与最近推出的容量相同

三星 Galaxy S24 FE 预计将以低于预期的价格推出,有四种颜色和两种内存选项 三星 Galaxy S24 FE 预计将以低于预期的价格推出,有四种颜色和两种内存选项 Sep 12, 2024 pm 09:21 PM

三星尚未就何时更新其 Fan Edition (FE) 智能手机系列提供任何提示。目前来看,Galaxy S23 FE 仍然是该公司的最新版本,于 2023 年 10 月年初推出。

新报告对传闻中的三星 Galaxy S25、Galaxy S25 Plus 和 Galaxy S25 Ultra 相机升级进行了严厉的评估 新报告对传闻中的三星 Galaxy S25、Galaxy S25 Plus 和 Galaxy S25 Ultra 相机升级进行了严厉的评估 Sep 12, 2024 pm 12:22 PM

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

小米红米 Note 14 Pro Plus 上市,成为首款配备 Light Hunter 800 摄像头的高通 Snapdragon 7s Gen 3 智能手机 小米红米 Note 14 Pro Plus 上市,成为首款配备 Light Hunter 800 摄像头的高通 Snapdragon 7s Gen 3 智能手机 Sep 27, 2024 am 06:23 AM

Redmi Note 14 Pro Plus 现已正式成为去年 Redmi Note 13 Pro Plus 的直接后继产品(亚马逊售价 375 美元)。正如预期的那样,Redmi Note 14 Pro Plus与Redmi Note 14和Redmi Note 14 Pro一起成为Redmi Note 14系列的主角。李

摩托罗拉 Razr 50s 在早期泄露中显示自己可能是新的预算可折叠手机 摩托罗拉 Razr 50s 在早期泄露中显示自己可能是新的预算可折叠手机 Sep 07, 2024 am 09:35 AM

摩托罗拉今年发布了无数设备,尽管其中只有两款是可折叠的。就上下文而言,虽然世界上大多数地区都收到了 Razr 50 和 Razr 50 Ultra,但摩托罗拉在北美提供了 Razr 2024 和 Razr 2

See all articles