Android自定义控件实现圆形进度条

项目中常用到的圆形进度条有好多个,从网上搜到的自定义进度条多是封装的比较好的代码,但是不利于初学者,现在本博客就教给大家如何一步步实现自定义进度条的效果

相关视频链接:
http://edu.csdn.net/course/detail/3719/65396

先看效果如图…

代码实现过程–main布局
这个布局中就是一个简单的引用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="开始下载"
  android:onClick="start" />

 <com.example.pb.ProgressView
  android:id="@+id/circleView"
  android:layout_width="100dp"
  android:layout_height="100dp" />

</LinearLayout>

自定义ProgressView-默认是图中第一种效果

package com.example.pb;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

public class ProgressView extends View {
 int progress = 0;
 private String text="0%";
 private int max = 100;

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

 public ProgressView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public ProgressView(Context context) {
  super(context);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  // 对于画笔
  Paint paint = new Paint();
  // 设置抗锯齿
  paint.setAntiAlias(true);
  // 设置画笔颜色

  // 三种样式--Stroke 只要描边 Fill 填充 FILL_AND_STROKE和既有描边又有填充
  paint.setStyle(Style.STROKE);
  //设置描边宽度
  paint.setStrokeWidth(2);
  //定义外圈员的颜色
  paint.setColor(Color.RED);
  //绘制圆形进度条--获取当前控件多大,正好让进度条在这个控件区间内
  canvas.drawCircle(getMeasuredWidth()/2, getMeasuredWidth()/2, getMeasuredWidth()/2, paint);
  //重新设置描边宽度,这个宽度最好能完全盖过圆形
  paint.setStrokeWidth(3);

  //定义限制圆弧的矩形,当前这样定义正好让圆弧和圆重合
  RectF oval = new RectF(0, 0, getMeasuredWidth(), getMeasuredWidth());
  //设置进度条(圆弧的颜色)
  paint.setColor(Color.GREEN);
  //绘制,设置进度条的度数从0开始,结束值是个变量,可以自己自由设置,来设置进度
  //true和false 代表是否使用中心点,如果true,代表连接中心点,会出现扇形的效果
  canvas.drawArc(oval, 0, 360 * progress / max, false, paint);
  //文字的绘制
  paint.setTextSize(40);
  //设置文字宽度
  paint.setStrokeWidth(1.0f);
  //测量文字大小-提前准备个矩形
  Rect bounds = new Rect();
  //测量文字的宽和高,测量的值可以根据矩形获取
  paint.getTextBounds(text, 0, text.length(), bounds);
  paint.setColor(Color.BLACK);
  paint.setStyle(Style.FILL);
  //绘制文字,计算文字的宽高进行设置
  canvas.drawText(text, getMeasuredWidth()/2 - bounds.width() / 2,
    getMeasuredWidth()/2 + bounds.height() / 2, paint);

 }
 /**
  * 初始设置当前进度的最大值-默认100
  * @param max
  */
 public void setMax(int max) {
  this.max = max;
 }
 /**
  * 更新进度和文字
  * @param progress
  * @param text
  */
 public void setProgressAndText(int progress, String text) {
  this.progress = progress;
  this.text = text;
  //重新绘制
  postInvalidate();
 }

}

如果想要实现第二种效果

//设置填充模式
paint.setStyle(Style.FILL);
//绘制,设置进度条的度数从0开始,结束值是个变量,可以自己自由设置,来设置进度
  //true和false 代表是否使用中心点,如果true,代表连接中心点,会出现扇形的效果
  canvas.drawArc(oval, 0, 360 * progress / max, false, paint);

Activity中代码–模拟一下下载的过程,效果随便定义

package com.example.pb;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

 private ProgressView circleView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  circleView = (ProgressView) findViewById(R.id.circleView);

 }

 int progress = 0;

 public void start(View v) {
  // 1000公里
  circleView.setMax(100);
  progress=0;
  new Thread() {
   public void run() {
    while (true) {
     progress = progress + 1;
     String text = progress + "%";
     circleView.setProgressAndText(progress, text);
     try {
      sleep(30);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     if (progress == 100) {
      break;
     }
    }
   };
  }.start();
 }

}

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

(0)

相关推荐

  • Android实现带数字的圆形进度条(自定义进度条)

    开发 设计搞了一个带圆形进度的进度条,在GitHub上逛了一圈,发现没有,自己撸吧. 先看界面效果: 主要思路是写一个继承ProgressBar的自定义View,不废话,直接上代码: package com.fun.progressbarwithnumber; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.g

  • Android编程实现WebView添加进度条的方法

    本文实例讲述了Android编程实现WebView添加进度条的方法.分享给大家供大家参考,具体如下: 标准的XML界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

  • Android编程之ProgressBar圆形进度条颜色设置方法

    本文实例讲述了Android ProgressBar圆形进度条颜色设置方法.分享给大家供大家参考,具体如下: 你是不是还在为设置进度条的颜色而烦恼呢--别着急,且看如下如何解决. ProgressBar分圆形进度条和水平进度条 我这里就分享下如何设置圆形进度条的颜色吧,希望对大家会有帮助. 源码如下: 布局文件代码: <ProgressBar android:id="@+id/progressbar" android:layout_width="wrap_content

  • android自定义view制作圆形进度条效果

    还是我们自定View的那几个步骤: 1.自定义View的属性 2.在View的构造方法中获得我们自定义的属性 [ 3.重写onMesure ] 4.重写onDraw 1.自定义属性: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomTitleView"> <attr name="m

  • Android动态自定义圆形进度条

    效果图: A.绘制圆环,圆弧,文本 //1.画圆环 //原点坐标 float circleX = width / 2; float circleY = width / 2; //半径 float radius = width / 2 - roundWidth / 2; //设置画笔的属性 paint.setColor(roundColor); paint.setStrokeWidth(roundWidth); paint.setStyle(Paint.Style.STROKE); canvas.

  • Android编程基于自定义View实现绚丽的圆形进度条功能示例

    本文实例讲述了Android编程基于自定义View实现绚丽的圆形进度条功能.分享给大家供大家参考,具体如下: 本文包含两个组件,首先上效果图: 1.ProgressBarView1(支持拖动): 2.ProgressBarView2(不同进度值显示不同颜色,不支持拖拽):   代码不多,注释也比较详细,全部贴上了: (一)ProgressBarView1: /** * 自定义绚丽的ProgressBar. */ public class ProgressBarView1 extends View

  • Android实现环形进度条的实例

    Android实现环形进度条的效果图如下: 自定义控件:AttendanceProgressBar 代码如下: public class AttendanceProgressBar extends View { // 画圆环底部的画笔 private Paint mCirclePaint; // 画圆环的画笔 private Paint mRingPaint; // 画字体的画笔 private Paint mTextPaint; // 圆形颜色 private int mCircleColor

  • Android自定义Material进度条效果

    首先看下效果图 布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_par

  • Android带圆形数字进度的自定义进度条示例

    开发 设计搞了一个带圆形进度的进度条,在GitHub上逛了一圈,发现没有,自己撸吧. 先看界面效果: 主要思路是写一个继承ProgressBar的自定义View,不废话,直接上代码: package com.fun.progressbarwithnumber; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.g

  • Android编程实现类似于圆形ProgressBar的进度条效果

    本文实例讲述了Android编程实现类似于圆形ProgressBar的进度条效果.分享给大家供大家参考,具体如下: 我们要实现一个类似于小米分享中的圆形播放进度条,android自带的圆形ProgressBar是默认自动旋转的,所以无法实现,于是我们想到了使用自定义一个View,来实现这种效果. 首先来看看自己定义的View package cn.easymobi.application.bell.common; import android.content.Context; import an

  • Android自定义view实现进度条指示效果

    先看看效果图: 首先是布局文件 <FrameLayout android:layout_width="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_height="wrap_content"> <ProgressBar android:id="@+id/p

随机推荐