Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418467
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-04-03 10:06:51

解决方案:自定义View
效果如下:

贴出代码:

注意:对于自定义View,必须要有两个参数的构造函数

MyProgressBar.java

  1. package cn.com.xiebiao.circleprogress;

  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.RectF;
  8. import android.util.AttributeSet;
  9. import android.util.Log;
  10. import android.view.View;

  11. import java.text.DecimalFormat;


  12. /**
  13.  * Created by vibexie on 4/2/15.
  14.  */
  15. public class MyProgressBar extends View {
  16.     /**
  17.      *内圈的颜色
  18.      */
  19.     private int innerColor;
  20.     /**
  21.      *外圈的颜色
  22.      */
  23.     private int outerColor;
  24.     /**
  25.      *内圆的半径
  26.      */
  27.     private int circleRadius;
  28.     /**
  29.      * 圆环的宽度
  30.      */
  31.     private int ringWidth;
  32.     /**
  33.      *速度
  34.      */
  35.     private int speed;
  36.     /**
  37.      *当前进度
  38.      */
  39.     private int currentProgress;
  40.     /*
  41.      *环的画笔
  42.      */
  43.     private Paint circlePaint;

  44.     /**
  45.      * 进度画笔
  46.      */
  47.     private Paint progressPaint;

  48.     /**
  49.      * 进度文本字体大小
  50.      */
  51.     private int progressTextSize;

  52.     public MyProgressBar(Context context) {
  53.         this(context, null);
  54.     }

  55.     public MyProgressBar(Context context, AttributeSet attrs) {
  56.         super(context, attrs);

  57.         /**
  58.          *获取自定义属性数组
  59.          */
  60.         TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyProgressBar);
  61.         int attrCount=typedArray.getIndexCount();

  62.         /**
  63.          *提取自定义属性的值
  64.          */
  65.         for(int i=0;i<attrCount;i++){
  66.             int attr=typedArray.getIndex(i);
  67.             switch (attr){
  68.                 case R.styleable.MyProgressBar_innerColor:
  69.                     //getColor的第二个参数是在第一个参数为空的时候设置的值
  70.                     innerColor=typedArray.getColor(R.styleable.MyProgressBar_innerColor,Color.GREEN);
  71.                     break;
  72.                 case R.styleable.MyProgressBar_outerColor:
  73.                     outerColor=typedArray.getColor(R.styleable.MyProgressBar_outerColor,Color.RED);
  74.                     break;
  75.                 case R.styleable.MyProgressBar_innerCircleRadius:
  76.                     circleRadius=typedArray.getDimensionPixelOffset(R.styleable.MyProgressBar_innerCircleRadius,100);
  77.                     break;
  78.                 case R.styleable.MyProgressBar_ringWidth:
  79.                     ringWidth=typedArray.getDimensionPixelOffset(R.styleable.MyProgressBar_ringWidth, 10);
  80.                     break;
  81.                 case R.styleable.MyProgressBar_progressTextSize:
  82.                     progressTextSize=typedArray.getDimensionPixelSize(R.styleable.MyProgressBar_progressTextSize,25);
  83.                     break;
  84.                 case R.styleable.MyProgressBar_speed:
  85.                     speed=typedArray.getInt(R.styleable.MyProgressBar_speed,20);
  86.                     break;
  87.             }
  88.         }
  89.         /**
  90.          * Recycle the TypedArray, to be re-used by a later caller. After calling
  91.          * this function you must not ever touch the typed array again.
  92.          */
  93.         typedArray.recycle();

  94.         /**
  95.          * new出画笔
  96.          */
  97.         circlePaint=new Paint();
  98.         progressPaint=new Paint();
  99.         /**
  100.          * 绘图线程
  101.          */

  102.         new Thread()
  103.         {
  104.             public void run()
  105.             {
  106.                 while (true)
  107.                 {
  108.                     currentProgress++;
  109.                     if (currentProgress == 360)
  110.                     {
  111.                         currentProgress = 0;
  112.                     }
  113.                     postInvalidate();
  114.                     try
  115.                     {
  116.                         Thread.sleep(speed);
  117.                     } catch (InterruptedException e)
  118.                     {
  119.                         e.printStackTrace();
  120.                     }
  121.                 }
  122.             };
  123.         }.start();
  124.     }


  125.     @Override
  126.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  127.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  128.     }

  129.     @Override
  130.     protected void onDraw(Canvas canvas) {
  131.         super.onDraw(canvas);
  132.         /**
  133.          * 获取圆心的x坐标
  134.          */
  135.         int x=getWidth()/2;
  136.         /**
  137.          * 获取圆心的Y坐标
  138.          */
  139.         int y=getHeight()/2;

  140.         /**
  141.          *设置环画笔的属性
  142.          */
  143.         circlePaint.setStyle(Paint.Style.STROKE);
  144.         circlePaint.setStrokeWidth(ringWidth);
  145.         circlePaint.setAntiAlias(true);

  146.         /**
  147.          * 设置进度画笔的属性
  148.          */
  149.         progressPaint.setStyle(Paint.Style.FILL);
  150.         progressPaint.setTextSize(progressTextSize);
  151.         progressPaint.setAntiAlias(true);
  152.         /**
  153.          * 进度文本的X坐标
  154.          */
  155.         int progressX=(int)(x-(progressTextSize*3/4));
  156.         int progressY=y+(progressTextSize/2);
  157.         /**
  158.          * 用于定义的圆弧的形状和大小的界限
  159.          */
  160.         RectF oval = new RectF(x-circleRadius, y-circleRadius, x+circleRadius, y+circleRadius);
  161.         /**
  162.          * 底层的圆圈完整,上层的圆圈跑
  163.          */
  164.         circlePaint.setColor(innerColor);
  165.         canvas.drawCircle(x, y, circleRadius, circlePaint);
  166.         circlePaint.setColor(outerColor);

  167.         DecimalFormat decimalFormat=new DecimalFormat("#.0");
  168.         canvas.drawText("" + decimalFormat.format(currentProgress/3.6), progressX, progressY, progressPaint);
  169.         canvas.drawArc(oval, -90, currentProgress, false, circlePaint);
  170.     }
  171. }

/value/attr.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <declare-styleable name="MyProgressBar">
  4.         <attr name="innerColor" format="color"/>
  5.         <attr name="outerColor" format="color"/>
  6.         <!--内圆的半径-->
  7.         <attr name="innerCircleRadius" format="dimension"/>
  8.         <!--圆环的宽度-->
  9.         <attr name="ringWidth" format="dimension"/>
  10.         <!--进度文本字体大小-->
  11.         <attr name="progressTextSize" format="dimension"/>
  12.         <attr name="speed" format="integer"/>
  13.     </declare-styleable>
  14. </resources>


MainActivity.java

  1. package cn.com.xiebiao.circleprogress;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuItem;


  6. public class MainActivity extends Activity {

  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.     }

  12. }

AndroidMainfest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="cn.com.xiebiao.circleprogress" >

  4.     <application
  5.         android:allowBackup="true"
  6.         android:icon="@mipmap/ic_launcher"
  7.         android:label="@string/app_name"
  8.         android:theme="@style/AppTheme" >
  9.         <activity
  10.             android:name="cn.com.xiebiao.circleprogress.MainActivity"
  11.             android:label="@string/app_name" >
  12.             <intent-filter>
  13.                 <action android:name="android.intent.action.MAIN" />

  14.                 <category android:name="android.intent.category.LAUNCHER" />
  15.             </intent-filter>
  16.         </activity>
  17.     </application>

  18. </manifest>

注意,在android Studio上开发安卓,对于自定义View的命名空间,只要
""即可。

activity_main.xml

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     xmlns:myProgressBar=""
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent">

  6.     <cn.com.xiebiao.circleprogress.MyProgressBar
  7.         android:layout_width="match_parent"
  8.         android:layout_height="match_parent"
  9.         myProgressBar:innerColor="#ff1f8a3c"
  10.         myProgressBar:outerColor="#ffa60a0e"
  11.         myProgressBar:ringWidth="30dp"
  12.         myProgressBar:innerCircleRadius="100dp"
  13.         myProgressBar:progressTextSize="50dp"
  14.         myProgressBar:speed="20"
  15.         />

  16. </RelativeLayout>




阅读(1436) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~