Android 实现自定义圆形进度条的实例代码

 更新时间:2016年11月26日 08:58:44   投稿:lqh  
进度条在Android中教程使用到,本文章向大家介绍一下Android自定义圆形进度条实现代码,需要的朋友可以参考一下。

Android 自定义圆形进度条

今天无意中发现一个圆形进度,想想自己实现一个,如下图:

基本思路是这样的:

1.首先绘制一个实心圆

2.绘制一个白色实心的正方形,遮住实心圆

3.在圆的中心动态绘制当前进度的百分比字符

4.绘制一个与之前实心圆相同颜色的空心圆

5.逐渐改变当前的百分比

6.根据百分比,逐渐改变正方形的大小,逐渐减小正方形的底部y轴的坐标,不断重绘,直到达到100%

首先看看自定义的属性

在values目录下新建attrs.xml内容如下:

定义绘制圆形的背景色,和绘制圆形的半径大小

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

  <attr name="circlecolor" format="color"></attr>
  <attr name="half" format="dimension"></attr>

  <declare-styleable name="myCircleImage">
    <attr name="circlecolor"></attr>
    <attr name="half"></attr>
  </declare-styleable>

</resources>

自定义视图

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class CirclePro extends View {

 private Paint paint;
 private int circleBack;//圆的背景色
 private int mschedual = 0;//用于控制动态变化
 float circleHalf; //圆的半径
 String percent = "";//绘制百分比的字符串

 @SuppressLint("Recycle")
 public CirclePro(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 paint = new Paint();
 TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.myCircleImage, defStyleAttr,0);
 @SuppressWarnings("unused")
 int leng = array.length();
 //获取自定义的属性,这里注意是R.styleable.myCircleImage_circlecolor而不是R.attr.circlecolor
 circleBack = array.getColor(R.styleable.myCircleImage_circlecolor,Color.GREEN);
 circleHalf = array.getDimension(R.styleable.myCircleImage_half,200.f);
 System.out.println(circleBack);

 }

 /**
 * 这个构造参数,当在布局文件中引用该view的时候,必须重写该构造函数
 * @param context
 * @param attrs
 */
 public CirclePro(Context context, AttributeSet attrs) {
 this(context, attrs, 0);//调用自己的构造函数

 }

 /**
 * 根据文本的
 * @param text
 * @param textSize
 * @return
 */
 public float getTextWidth(String text,float textSize) {

 TextPaint textPaint = new TextPaint();
 textPaint.setTextSize(textSize);
 return textPaint.measureText(text);
 }

 @Override
 protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
 super.onDraw(canvas);

 float height = getHeight();
 float width = getWidth();
// float circleHalf = (float) (width*0.7/2);

 paint.setColor(circleBack);
 paint.setAntiAlias(true);
 paint.setStyle(Paint.Style.FILL);
 canvas.drawCircle(width/2,height/2,circleHalf, paint);//画实心圆

 if (mschedual <= 100) {//,如果当前进度小于100,画实心矩形
  paint.setColor(Color.WHITE);
  canvas.drawRect(width/2-circleHalf,height/2-circleHalf,width/2+circleHalf,height/2+circleHalf - mschedual*circleHalf/50, paint);
 }

 //画当前进度的字符串
 paint.setColor(Color.BLACK);
 paint.setTextSize(30.f);
 percent = mschedual+" %";
 canvas.drawText(percent, width/2-getTextWidth(percent,30)/2,height/2+paint.getTextSize()*3/8, paint);//字体的高度=paint.getTextSize()*3/4

 //画空心圆
 paint.setColor(circleBack);
 paint.setStyle(Paint.Style.STROKE);
 canvas.drawCircle(width/2,height/2,circleHalf, paint);

 if (mschedual < 100) {//更改当前进度值,并重绘
  mschedual++;
  invalidate();
 }
 }
}

在activity_main.xml中,需要用到自定义的属性,首先添加命名空间:

xmlns:liu=”http://schemas.android.com/apk/res/com.example.androidcirclepro”

其中liu是自定义的一个前缀,随意命名的,com.example.androidcirclepro是我们的应用的包名

activity_main.xmln内容如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:liu="http://schemas.android.com/apk/res/com.example.androidcirclepro"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity" >

  <com.example.androidcirclepro.CirclePro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    liu:half="90dp"
    liu:circlecolor="#fff0f0"
    />

</RelativeLayout>

至此一个自定义的圆形进度条就完成了,是不是很简单。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Android 为ListView添加分段标头的方法

    Android 为ListView添加分段标头的方法

    下面小编就为大家带来一篇Android 为ListView添加分段标头的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Android框架RePlugin使用详解

    Android框架RePlugin使用详解

    这篇文章给大家分享了Android 插件化框架 RePlugin使用心得,对此有兴趣的朋友参考学习下。
    2018-07-07
  • Android开发之BottomSheetDialog组件的使用

    Android开发之BottomSheetDialog组件的使用

    BottomSheetDialog是底部操作控件,可在屏幕底部创建一个支持滑动关闭视图。本文将通过示例详细讲解它的使用,感兴趣的小伙伴可以了解一下
    2023-01-01
  • Android用ActionBar高仿微信主界面的实例代码

    Android用ActionBar高仿微信主界面的实例代码

    这篇文章主要介绍了Android用ActionBar高仿微信主界面的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Android图片缓存之Bitmap详解(一)

    Android图片缓存之Bitmap详解(一)

    这篇文章主要为大家详细介绍了Android图片缓存之Bitmap,点学习一下Bitmap、BitmapFactory这两个类,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android View与Compose互相调用实例探究

    Android View与Compose互相调用实例探究

    这篇文章主要介绍了Android View与Compose互相调用,Compose 具有超强的兼容性,兼容现有的所有代码,Compose 能够与现有 View 体系并存,可实现渐进式替换
    2023-01-01
  • 详解Flutter中key的正确使用方式

    详解Flutter中key的正确使用方式

    这篇文章主要为大家介绍了详解Flutter中key的正确使用方式,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android Retrofit使用详细教程

    Android Retrofit使用详细教程

    Retrofit是Android用来接口请求的网络框架,内部是基于OkHttp实现的,retrofit负责接口请求的封装,retrofit可以直接将接口数据解析为Bean类、List集合等,直接简化了中间繁琐的数据解析过程,这篇文章主要介绍了Android Retrofit使用详情,需要的朋友可以参考下
    2024-03-03
  • Android开发实现从相册中选择照片功能详解

    Android开发实现从相册中选择照片功能详解

    这篇文章主要介绍了Android开发实现从相册中选择照片功能,涉及Android权限控制、事件绑定、文件路径与获取等相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • Android基于Http协议实现文件上传功能的方法

    Android基于Http协议实现文件上传功能的方法

    这篇文章主要介绍了Android基于Http协议实现文件上传功能的方法,结合实例形式分析了Android的HTTP协议原理与文件上传功能实现技巧,需要的朋友可以参考下
    2016-07-07

最新评论