自定义View_绘制圆_进行拖动(不出屏幕)放大缩小
2017-04-16 21:13
399 查看
package com.example.administrator.week_2_demo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by Administrator on 2017/4/16. */ public class MyView extends View { private float vx=100; private float vy=100; private float vr=50; private float distance; private int width; private int height; private final float increment = 10; private final float min = 50; public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); } 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(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); canvas.drawCircle(vx, vy, vr, paint); width = getWidth(); height = getHeight(); } @Override public boolean onTouchEvent(MotionEvent event) { //手指的数量 int count = event.getPointerCount(); float x = event.getX(); float y = event.getY(); int action = event.getAction(); if(action==MotionEvent.ACTION_POINTER_DOWN){ distance = getDistance(event); } if(count==1) { if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_MOVE) { if (x > width - vr) { vx = width - vr; } else if (x < vr) { vx = vr; } else { vx = x; } if (y > height - vr) { vy = height - vr; } else if (y < vr) { vy = vr; } else { vy = y; } postInvalidate(); } } else if(count==2&&action == MotionEvent.ACTION_MOVE){ float maxR = 0; if(vx<vy){ maxR = vx; }else{ maxR = vy; } //判断手指方向 if(distance>getDistance(event)){ vr -= increment; if(vr<min){ vr = min; } }else if(distance<getDistance(event)){ vr += increment; if(vr>maxR){ vr = maxR; }else if(vr>width/2){ vr = 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); } }
相关文章推荐
- Android 自定义View可拖动移动位置及边缘拉伸放大缩小
- Android 自定义View可拖动移动位置及边缘拉伸放大缩小 - Livingstone
- Android 自定义View可拖动移动位置及边缘拉伸放大缩小
- 自定义ImageView 实现双击放大缩小还原,无极缩小和旋转及拖动(多机型测试很稳定)
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- 实现自定义view(1):可在全屏幕自由拖动的view
- 自定义View 之利用ViewPager 实现画廊效果(滑动放大缩小)
- 自定义View利用手势检测实现图片放大缩小
- Android 自定义View 之利用ViewPager 实现画廊效果(滑动放大缩小)
- 自定义ImageView实现图片手势滑动、放大缩小效果
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控缩放
- 实现自定义view(1):可在全屏幕自由拖动的view
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- Androin学习笔记五十四: android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- 属性动画图片从上移动到屏幕中间,放大图片的二倍再缩小到原来,自定义圆实现倒计时,解析数据显示,点击条目实现js交互
- 自定义imageVIew实现圆角和圆形图,片圆形图片缩小放大正常,圆角图片放大缩小有点问题
- 自定义view触摸放大缩小
- 百度地图API制作自定义的放大缩小控件ZoomControlView