自定义view圆放大缩小

/**
 * 自定义画圆的方法
 * Created by lenovo on 2017/4/7.
 */

public class MyView extends View {
    //画笔
    private Paint mpaint;
    //原始坐标
    private float mx;
    private float my;
    //半径
    private float redio=100;

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

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

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //实例化画笔
        mpaint=new Paint();
        //抗锯齿
        mpaint.setAntiAlias(true);
       //画笔颜色
        mpaint.setColor(Color.RED);
        //单点移动
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
            if (event.getPointerCount()>1)
                return  false;
            //当前坐标
                float x=   event.getX();
                float y=   event.getY();

                switch (event.getAction()){


                    //移动
                    case MotionEvent.ACTION_MOVE:
                        if(isOK(x,y,redio)){
                            mx=x;
                            my=y;

                            invalidate();
                        }

                        break;
                }

                return true;
            }
        });

    }

    /**
     * 计算了两点之间的距离
     * @param event
     * @return
     */
    private float getDisByXY(MotionEvent event){

        if(event.getPointerCount()>=2){
            //第一个点
            float x=event.getX();
            float y=event.getY();
            //第二个点
            float x2=event.getX(1);
            float y2=event.getY(1);

            return (float) Math.sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));

        }else{
            float x=event.getX();
            float y=event.getY();
            switch (event.getAction()){

                //移动
                case MotionEvent.ACTION_MOVE:
                    if(isOK(x,y,redio)){
                        mx=x;
                        my=y;

                        invalidate();
                    }

                    break;
            }
        }
        return  0;

    }

    private boolean isOK(float x,float y,float redio){
        return x>=redio&&x<=getWidth()-redio&&y>=redio&&y<=getHeight()-redio;
    }

    /**
     * 多点触控
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float dis=getDisByXY(event);
        if(dis!=0){
            if (isOK(mx,my,dis)){
                redio=dis;
                invalidate();
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mx=getWidth()/2;
        my=getHeight()/2;

    }
    //画圆
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    canvas.drawCircle(mx,my,redio,mpaint);

    }

}

你可能感兴趣的:(自定义view圆放大缩小)