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>