首页 > 学院 > 开发设计 > 正文

自定义圆形进度条 自定义属性 单点触控

2019-11-09 15:41:46
字体:
来源:转载
供稿:网友
package com.example.administrator.myapplication;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF; import android.os.SystemClock;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.Toast;/** * 姓名:戚德水 //// 日期: 2017/2/8. */public class MyView2 extends View {    PRivate  Rect rect;    private float currentX=0;    private float currentY=0;    private  int wColor;    private  int nColor;    private Paint mPaint;    private android.graphics.Canvas canvas;    private RectF rectF;    private float max;    private float progress;    private Boolean aBoolean = true;    private Context context;    private float tag = 0;    private float ballx;    private float bally;    public MyView2(Context context) {        this(context,null);    }    public MyView2(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public MyView2(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        this.context = context;        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView2);        wColor = typedArray.getColor(R.styleable.MyView2_wbackgrount, Color.BLACK);        nColor = typedArray.getColor(R.styleable.MyView2_nbackgrount, Color.GREEN);        max = typedArray.getFloat(R.styleable.MyView2_max,100);        progress = typedArray.getFloat(R.styleable.MyView2_progerss,10);        typedArray.recycle();        mPaint = new Paint();        //抗锯齿        mPaint.setAntiAlias(true);        //空心 只显示最外层的线        mPaint.setStyle(Paint.Style.STROKE);        rect = new Rect();    }    @Override    protected void onDraw(final Canvas canvas) {        super.onDraw(canvas);        if (currentX == 0) {            currentX = getWidth() / 2;            currentY = getHeight() / 2;        }        //背景圆环        mPaint.setColor(wColor);        //画笔宽度        mPaint.setStrokeWidth(50);        canvas.drawCircle(currentX, currentY, 200, mPaint);        //进度条长度        mPaint.setStrokeWidth(30);        mPaint.setColor(nColor);        rectF = new RectF(currentX-200,currentY-200, currentX+200, currentY+200);        canvas.drawArc(rectF,-90,progress/max*360,false,mPaint);        //进度百分比        mPaint.setStrokeWidth(10);        mPaint.setTextSize(100);        int v = (int) (progress / max * 100);        String text = v + "%";        mPaint.getTextBounds(text,0,text.length(),rect);        canvas.drawText(text,currentX-rect.width()/2,currentY+rect.height()/2,mPaint);        float v1 = progress / max * 800;        ballx = currentX - 400 + v1;        bally = currentY - 300;        //总线        mPaint.setColor(wColor);        mPaint.setStrokeWidth(5);        canvas.drawLine(ballx,currentY-300,currentX+400,currentY-300,mPaint);        //进度线        mPaint.setColor(Color.GREEN);        mPaint.setStrokeWidth(5);        canvas.drawLine(currentX-400,currentY-300,currentX-400+ v1,currentY-300,mPaint);        //背景进度原点        mPaint.setColor(Color.GREEN);        mPaint.setStrokeWidth(20);        canvas.drawCircle(ballx, bally, 10, mPaint);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_UP:                tag = 0;                if (aBoolean){                    //给定最大值                    setMax(1098);                    new Thread(new Runnable() {                        @Override                        public void run() {                            while (tag!=max+1){                                SystemClock.sleep(10);                                setProgress(tag);                                //强制绘图 等于重新执行ondraw()方法                                postInvalidate();                                tag++;                            }                            aBoolean = true;                        }                    }).start();                    aBoolean = false;                }                break;            case MotionEvent.ACTION_DOWN:                Toast.makeText(context, ballx+"|||"+bally, Toast.LENGTH_SHORT).show();                break;            case MotionEvent.ACTION_MOVE:                setCurrentX((int) event.getX());                setCurrentY((int) event.getY());                //强制绘图 等于重新执行ondraw()方法                postInvalidate();                break;        }        return true;    }    public synchronized float getCurrentX() {        return currentX;    }    public synchronized void setCurrentX(int currentX) {        this.currentX = currentX;    }    public synchronized float getCurrentY(){        return currentY;    }    public synchronized void setCurrentY(int currentY) {        this.currentY = currentY;    }    //提供 ui使用方法    public synchronized float getProgress() {        return progress;    }    public synchronized void setProgress(float progress) {        this.progress = progress;    }    public synchronized float getMax() {        return max;    }    public synchronized void setMax(int max) {        this.max = max;    }}
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyView">        <attr name="textsize" format="float"/>        <attr name="textContext" format="string"/>    </declare-styleable>    <declare-styleable name="MyView2">        <attr name="wbackgrount" format="color"/>        <attr name="nbackgrount" format="color"/>        <attr name="max" format="float"/>        <attr name="progerss" format="float"/>    </declare-styleable></resources>
<com.example.administrator.myapplication.MyView2    android:id="@+id/myview2"    android:layout_width="500dp"    android:layout_height="500dp"    MyView2:wbackgrount="#ff0303"    MyView2:nbackgrount="#0318ff"    android:layout_centerVertical="true"    android:layout_centerHorizontal="true"    android:background="#401717"/>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表