自定义控件之放大缩小和移动.进度

来源:互联网 发布:数据字典的作用是什么 编辑:程序博客网 时间:2024/05/24 04:30
public class FirstProgressView extends View {    private int width;    private int height;    private int progress;    private int maxProgress = 100;    private Paint mPaintBackGround;    private Paint mPaintCurrent;    private Paint mPaintText;    //圆的初始位置    Context context;    public void setWidth(int width) {        this.width = width;    }    public void setHeight(int height) {        this.height = height;    }    public int getProgress() {        return progress;    }    public void setProgress(int progress) {        this.progress = progress;        invalidate();    }    public int getMaxProgress() {        return maxProgress;    }    public void setMaxProgress(int maxProgress) {        this.maxProgress = maxProgress;    }    public FirstProgressView(Context context) {        super(context);    }    public FirstProgressView(Context context, AttributeSet attrs) {        super(context, attrs);        mPaintBackGround = new Paint();//新定义一个画笔,用来画背景        mPaintBackGround.setColor(Color.GRAY);//设置画笔颜色        mPaintBackGround.setAntiAlias(true);//设置为true,代表抗锯齿        mPaintCurrent = new Paint();//用于画进度图        mPaintCurrent.setColor(Color.GREEN);        mPaintCurrent.setAntiAlias(true);        mPaintText = new Paint();//用来画文本的画笔        mPaintText.setColor(Color.BLACK);        mPaintText.setAntiAlias(true);        mPaintText.setTextAlign(Paint.Align.CENTER);//设置文本排布方式:正中央        mPaintText.setTextSize(50);//设置文本大小,这里为50xp    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //左起:圆心x坐标,圆心y坐标,半径,Paint对象(画笔)        canvas.drawCircle(width / 2, height / 2, 200, mPaintBackGround);        canvas.drawCircle(width / 2, height / 2, progress * 200f / maxProgress, mPaintCurrent);        //左起:文本内容,起始位置x坐标,起始位置y坐标,画笔        canvas.drawText(progress * 100f / maxProgress + "%", width / 2, height / 2, mPaintText);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //View布局宽        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);        //View布局高        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);        //设定本View的大小的方法        setMeasuredDimension(width, height);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                ScaleAnimation scaleAnimation2 = new ScaleAnimation(0.7f, 1.0f, 0.7f, 1.0f, ScaleAnimation.RELATIVE_TO_PARENT, 0.5f, ScaleAnimation.RELATIVE_TO_PARENT, 0.5f);                scaleAnimation2.setDuration(5000);// 设置持续时间                startAnimation(scaleAnimation2);                break;            case MotionEvent.ACTION_MOVE:            case MotionEvent.ACTION_UP:                // 获取当前触摸点的x,y坐标                width = (int) event.getX();                height = (int) event.getY();                break;        }//重新绘制圆 ,控制小球不会被移出屏幕        invalidate();        // 自己处理触摸事件        return true;    }
原创粉丝点击