我想通过手指手势移动视图。我搜索了但添加了这段代码,但仍无效。
请帮我查一下问题所在。
public boolean onTouchEvent(MotionEvent event) {
curX = event.getX();
curY = event.getY();
if(event.getPointerCount() == 2) {
previousFingerDistance = currentFingerDistance;
currentFingerDistance = fingerDist(event);
if(previousFingerDistance == currentFingerDistance) {
prevX = curX;
prevY = curY;
invalidate();
}
}
return true;
}
答案 0 :(得分:2)
试试这段代码。我正在使用它在屏幕上移动ImageView,但我认为它适合其他类型的视图。
imView.setOnTouchListener(new OnTouchListener() {
private static final String TAG = "Touch";
// These matrices will be used to move and zoom image
Matrix savedMatrix = new Matrix();
PointF start = new PointF();
public PointF mid = new PointF();
// We can be in one of these 3 states
public static final int NONE = 0;
public static final int DRAG = 1;
public static final int ZOOM = 2;
public int mode = NONE;
float oldDist;
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
}
// Perform the transformation
Log.d("point",start.x +" "+start.y);
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
});
了解更多一般观点
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
float dx = motionEvent.getX();
float dy = motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE:
float x = motionEvent.getX();
fload y = motionEvent.getY();
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
float left = lp.leftMargin + (x - dx);
float top = lp.topMargin + (y - dy);
lp.leftMargin = (int)left;
lp.topMargin = (int)top;
view.setLayoutParams(lp);
break;
}
return true;
}
答案 1 :(得分:0)
尝试一下 假设您有一个使用draw()方法的视图
private var Cx = 0f ; private var Cy = 0f
override fun onDraw(canvas: Canvas?) {
canvas?.drawCircle(Cx,Cy,_radius.toFloat(), paint)
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
event?:return false
val action = event.actionMasked
return when(action){
MotionEvent.ACTION_MOVE -> {
Cx = event.x
Cy = event.y
invalidate()
true
}
MotionEvent.ACTION_DOWN -> {
true
}
MotionEvent.ACTION_UP -> {
true
}
else -> false
}
}