码迷,mamicode.com
首页 > 其他好文 > 详细

自定义view——环形进度条,带progress值

时间:2015-01-09 16:50:16      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

第一步:新建文件Circle.java
package com.lancy.demo.democircle.widget;
 
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.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
 
import com.lancy.demo.democircle.R;
 
public class Circle extends View {
    private Paint mPaint;
 
    private float strokeWidth = 10;
    private float progress = 0;
    private int defaultColor;
    private int activeColor;
 
    public Circle(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.circle);
 
        strokeWidth = a.getDimension(R.styleable.circle_strokeWidth, strokeWidth);
        progress = a.getFraction(R.styleable.circle_progress, 360, 100, 10);
        defaultColor = a.getColor(R.styleable.circle_defaultColor, Color.GRAY);
        activeColor = a.getColor(R.styleable.circle_activeColor, Color.BLUE);
       
        a.recycle();
 
    }
 
    public Circle(Context context) {
 
        this(context, null);
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int minimumWidth = getSuggestedMinimumWidth();
        final int minimumHeight = getSuggestedMinimumHeight();
        int viewWidth = resolveMeasured(widthMeasureSpec, minimumWidth);
        int viewHeight = resolveMeasured(heightMeasureSpec, minimumHeight);
        setMeasuredDimension(viewWidth, viewHeight);
 
    }
 
    private int resolveMeasured(int measureSpec, int desired) {
        int result = 0;
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (MeasureSpec.getMode(measureSpec)) {
        case MeasureSpec.UNSPECIFIED:
            result = desired;
            break;
        case MeasureSpec.AT_MOST:
            result = Math.max(specSize, desired);
            break;
        case MeasureSpec.EXACTLY:
        default:
            result = specSize;
        }
        return result;
    }
 
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        float h = width > height ? height : width;
 
        Paint p = new Paint();
        p.setColor(defaultColor);
        p.setStyle(Paint.Style.STROKE);
        p.setAntiAlias(true);
        p.setStrokeWidth(strokeWidth);
        RectF oval1 = new RectF(strokeWidth / 2, strokeWidth / 2, h-strokeWidth / 2, h-strokeWidth / 2);
        canvas.drawArc(oval1, 0, 360, false, p);
        p.setColor(activeColor);
        canvas.drawArc(oval1, 270, progress, false, p);//小弧形
 
    }
 
    public void setProgress(float progress) {
        this.progress = progress / 100 * 360;
        invalidate();
 
    }
 
    public float getProgress() {
        return progress * 100 / 360;
    }
 
}
 
第二步  新建attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="circle">
         <attr name="strokeWidth" format="dimension"></attr>
         <attr name="progress" format="fraction"></attr>
         <attr name="defaultColor" format="color"></attr>
         <attr name="activeColor" format="color"></attr>
         
         
     </declare-styleable>
</resources>
 
第三步  建布局文件 circle.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
xmlns:circle="http://schemas.android.com/apk/res/com.lancy.demo.democircle
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="50dp" 
android:gravity="center" 
android:orientation="vertical" > 

<com.lancy.demo.democircle.widget.Circle 
android:id="@+id/circle" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
circle:defaultColor="#95a1aa" 
circle:activeColor="#1290dd" 
circle:strokeWidth="10dp" 
circle:progress="25%" 
/> 

<!-- --> 

</LinearLayout> 
 
第三步 建MainActivity.java
 package com.lancy.demo.democircle;
 
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
 
import com.lancy.demo.democircle.widget.Circle;
 
public class MainActivity extends Activity {
    Circle circle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.circle);
        circle = (Circle) findViewById(R.id.circle);
       
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(new TimeReceiver(), filter);
    }
   
   
    class TimeReceiver extends BroadcastReceiver{
 
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("TimeReceiver----------------", intent.getAction());
            circle.setProgress(circle.getProgress()-1);
           
        }
       
    }
 

自定义view——环形进度条,带progress值

标签:

原文地址:http://www.cnblogs.com/yuanyuan-5683/p/4213556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!