自定义view学习系列之圆形进度条

</pre>工作后,人都变懒了。。。。<p></p><p><span style="font-size:18px">好久不更新博客了,最近工作中发现不记录一些常见问题或者是不做个学习记录的话,进步很小,于是今天又提起笔来写写博客吧。争取以后有机会就写写。</span></p><p><span style="font-size:18px">还是自定义view吧,之前公司有个需求是圆形评分,界面类似这样,把中间的想象成10分。这种界面很常见,很简单(当然要做的扩展性和适用性很好的话也不容易)。</span></p><p>                                                                                                                <img src="https://img-blog.csdn.net/20150925235432388?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="font-size:18px" /></p><p><span style="font-size:18px">这种界面不用多想了,就是画弧了,根据评分大小,计算她的比例然后画弧度就行了。</span></p><p><span style="font-size:18px">当然今天我们不做这个,我们做个更常见的百分比进度条。做到末尾的时候发现csdn上也有一篇博客跟这个类似,当然人家讲的的比我的好一些,但是原理是一样的。略有参考。</span></p><p><span style="font-size:18px"></span></p><p><span style="font-size:18px">自定义view的过程和自定义属性就不贴了。这个大家应该都知道,直接贴代码吧</span></p><pre name="code" class="java">package com.example.administrator.helloword;

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.util.Log;
import android.view.View;

import java.net.ConnectException;

/**
 * Created by lyd on 2015/9/5 0005.
 */
public class CustomCirlce extends View {

    private String mText = null;
    private float mPercent = 0;
    private int i = 0;

    private Paint mPercentPaint;//百分比
    private Paint mOutCircle;//外层大圆
    private Paint mInArc;//圆弧
    private Canvas mCanvas;

    public CustomCirlce(Context context) {
        this(context, null);
    }

    public CustomCirlce(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomCirlce(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false);
        //外层大圆
        mOutCircle = new Paint();
        mOutCircle = new Paint();
        mOutCircle.setColor(Color.parseColor("#E9E9E9"));
        mOutCircle.setStyle(Paint.Style.STROKE);
        mOutCircle.setAntiAlias(true);
        mOutCircle.setStrokeWidth(20.0f);

        //圆弧
        mInArc = new Paint();
        mInArc = new Paint();
        mInArc.setAntiAlias(true);
        mInArc.setColor(Color.parseColor("#6BCFA2"));
        mInArc.setStyle(Paint.Style.STROKE);
        mInArc.setStrokeWidth(20.0f);

        //百分比
        mPercentPaint = new Paint();
        mPercentPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPercentPaint.setAntiAlias(true);
        mPercentPaint.setColor(Color.BLUE);
        mPercentPaint.setStrokeWidth(1.0f);
        mPercentPaint.setTextSize(70);
        mPercentPaint.setTextAlign(Paint.Align.CENTER);//设置文本水平居中
        init(context, attrs);

    }


    private void init(Context context,AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CustomCirlce);
        int count = array.getIndexCount();
        for (int i = 0;i<count;i++)
        {
            int a  = array.getIndex(i);
            switch (a)
            {
                case  R.styleable.CustomCirlce_CirclePercent1:
                    mPercent =array.getInt(a,0);
                case R.styleable.CustomCirlce_CircleText1 :
                    mText = array.getString(a);
            }
        }

        array.recycle();
    }

    public void setCurrentText(int text) {
        this.mPercent = text;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        i++;
        Log.i("===","ondraw"+i);
        super.onDraw(canvas);
        drawCircle(canvas);
        drawArc(canvas);
        drawText(canvas);
    }

    private void drawCircle(Canvas canvas) {
        RectF rectF = new RectF(10,100,300,390);
        canvas.drawArc(rectF, 0, 360, false, mOutCircle);
    }

    private void drawArc(Canvas canvas) {

        RectF rectF = new RectF(10,100,300,390);
        canvas.drawArc(rectF, -90,(float)(mPercent*3.6f),false,mInArc);

    }

    private void drawText(Canvas canvas) {
        Log.i("===",mPercent+"");
        canvas.drawText(mPercent+"%",0,(mPercent+"%").length(),150,250,mPercentPaint);

    }

}

主mainActivity函数

public class MainActivity extends ActionBarActivity{
    private CustomCirlce customCirlce;
    private int mCurrent = 0;
    private Button mButton;
    private android.os.Handler mHandler = new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0:
                    mCurrent++;
                    if (mCurrent<=100){
                        customCirlce.setCurrentText(mCurrent);
                        sendEmptyMessageDelayed(0, 1000);
                    }
                    break;
            }

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        customCirlce = (CustomCirlce) findViewById(R.id.myprogress);
        Button button = (Button) findViewById(R.id.button_start);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHandler.sendEmptyMessageDelayed(0, 1000);

            }
        });

    }

}

布局文件就不写了。很简单。

不过这里我要说一个bug,一开始的时候我发现ondraw函数只执行一次,上网查了很多资料,有的说是因为安卓4.1之后默认开启了硬件加速,这样的话就算你调用invalidate函数,也不会再执行ondraw函数,解决办法是在mainfest文件中添加

android:hardwareAccelerated="false" 这条命令,但是我添加后没用。。。
后来参考了一下另一篇博客,发现原来是自定义的view引用错了,我是直接new的,而不是通过findViewById找到的,怪不得不能更新。以后要多多注意。
综上,上效果图吧:








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
总共分为三层:一层为圆形边线,一层为进度边线,一层用来显示标识进度节点。 public class CircleProgressBar extends View { private int maxProgress = 100; private int progress = 15; private int progressStrokeWidth = 2; private int marxArcStorkeWidth = 16; // 画圆所在的距形区域 RectF oval; Paint paint; public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub oval = new RectF(); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); width = (width > height) ? height : width; height = (width > height) ? height : width; paint.setAntiAlias(true); // 设置画笔为抗锯齿 paint.setColor(Color.WHITE); // 设置画笔颜色 canvas.drawColor(Color.TRANSPARENT); // 白色背景 paint.setStrokeWidth(progressStrokeWidth); // 线宽 paint.setStyle(Style.STROKE); oval.left = marxArcStorkeWidth / 2; // 左上角x oval.top = marxArcStorkeWidth / 2; // 左上角y oval.right = width - marxArcStorkeWidth / 2; // 左下角x oval.bottom = height - marxArcStorkeWidth / 2; // 右下角y canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景 paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); paint.setStrokeWidth(marxArcStorkeWidth); canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色 paint.setStrokeWidth(1); String text = progress + "%"; int textHeight = height / 4; paint.setTextSize(textHeight); int textWidth = (int) paint.measureText(text, 0, text.length()); paint.setStyle(Style.FILL); canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, paint); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } /** * 设置进度 * * @param progress * 进度百分比 * @param view * 标识进度的节点视图 */ public void setProgress(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.invalidate(); } /** * 非UI线程调用 */ public void setProgressNotInUiThread(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.postInvalidate(); } /** * 进度标注点的动画 * * @param fromDegrees * @param toDegrees * @return */ private Animation pointRotationAnima(float fromDegrees, float toDegrees) { int initDegress = 306;// 进度点起始位置(图片偏移约54度) RotateAnimation animation = new RotateAnimation(fromDegrees, initDegress + toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(1);// 设置动画执行时间 animation.setRepeatCount(1);// 设置重复执行次数 animation.setFillAfter(true);// 设置动画结束后是否停留在结束位置 return animation; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值