自定义图可以放大和缩小

1.自定义圆圈让其可以单指点击进行拖拽,并且不能拖出边框

2.双指点击进行放大和缩小,不能拖出边框

代码如下

(1).定义的View类


public class MyView extends View {
    private float aFloat;
    private float startX;
    private float startY;
    private int color;
    private int width;
    private int height;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        aFloat = typedArray.getFloat(R.styleable.MyView_BandRadius, 10);
        startX = typedArray.getFloat(R.styleable.MyView_BandStartX, 10);
        startY = typedArray.getFloat(R.styleable.MyView_BandStartY, 10);
        color = typedArray.getColor(R.styleable.MyView_BandColor, Color.RED);


    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();

        //设置画笔
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);

        canvas.drawCircle(startX, startY, aFloat, paint);

 }
  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getWidth();
        height=getHeight();

    }
    //设置触摸事件

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int pointer = event.getPointerCount();

        float x = event.getX();
        float y = event.getY();

        int k = event.getAction();

        if(k == MotionEvent.ACTION_POINTER_DOWN){
            distance = getDistance(event);
        }

        if(pointer==1){
         if(k == MotionEvent.ACTION_UP||k == MotionEvent.ACTION_MOVE){
             //移动后x的值如果大于屏幕的宽度减去半径就让开始的x值等于屏幕宽减去半径
                if(x>width-aFloat){
                    startX = width - aFloat;
                    //小于半径就让其等于半径,不能将其缩放到没有
                }else if(x<aFloat){
                      startX = aFloat;
                }else{
                    //如果没有移动就设置为原来的大小
                    startX = x;
                }
                    //同理x                if(y>height-aFloat){
                    startY = height - aFloat;
                }else if(y<aFloat){
                    startY = aFloat;
                }else{
                    startY= y;
                }
             //重新绘制
                postInvalidate();
            }

        }
        //多点触控时
        else if(pointer==2&&k == MotionEvent.ACTION_MOVE){

            float maxR = 0;
            //如果x的起点小于y的起点就等于x的起点,谁小就赋值谁
            if(startX<startY){
                maxR = startX;
            }else{
                maxR = startY;
            }

            //判断手指方向
            if(distance>getDistance(event)){
                //缩小就按照每次缩小10进行
                aFloat -= increment;
                if(aFloat<min){
                    //最小缩放为10
                    aFloat = min;
                }
            }else if(distance<getDistance(event)){
                //放大按照+10
                aFloat += increment;

                if(aFloat>maxR){
                    aFloat = maxR;

                }else if(aFloat>width/2){ //最大也要小于等于屏幕的一半
                    aFloat = width/2;
                }
            }
            //移动之后重新判断手指的距离
            distance = getDistance(event);
            postInvalidate();
        }
        return true;
    }

    private float getDistance(MotionEvent event){
          //判断手指滑动的方向
        float xOne = event.getX(0);
        float yOne = event.getY(0);
        float xTwo = event.getX(1);
        float yTwo = event.getY(1);

        return (xOne - xTwo)*(xOne - xTwo)+(yOne - yTwo)*(yOne - yTwo);
    }

}


 

(2).自定义的属性xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">

        <attr name="BandColor"  format="reference|color"/>
        <attr name="BandStartX" format="float"/>
        <attr name="BandStartY" format="float"/>
        <attr name="BandRadius" format="float"/>

     </declare-styleable>
    
</resources>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值