我有两个贴纸,我想要两个贴纸来移动缩放旋转。当我添加ontouchlistener时,只有1个贴纸执行操作。
这是我设置监听器的代码
ImageView crown = (ImageView) root.findViewById(R.id.crown);
crown.setOnTouchListener(this);
ImageView crown1 = (ImageView) root.findViewById(R.id.crown1);
crown1.setOnTouchListener(this);
这是我布局的xml文件
<FrameLayout
android:id="@+id/editpic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
android:layout_marginTop="40dp"
android:background="@drawable/thumb" >
<ImageView
android:id="@+id/crown"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:src="@drawable/crown1" >
</ImageView>
<ImageView
android:id="@+id/crown1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:src="@drawable/crown2" >
</ImageView>
</FrameLayout>
让我知道是否有人有此解决方案。
更新
public boolean onTouch(View v, MotionEvent event) {
Log.e(TAG, "onTouch !!");
ImageView view = (ImageView) v;
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
lastEvent = null;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
lastEvent = new float[4];
lastEvent[0] = event.getX(0);
lastEvent[1] = event.getX(1);
lastEvent[2] = event.getY(0);
lastEvent[3] = event.getY(1);
d = rotation(event);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
lastEvent = null;
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 && event.getPointerCount() == 2) {
float newDist = spacing(event);
matrix.set(savedMatrix);
if (newDist > 10f) {
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
if (lastEvent != null) {
newRot = rotation(event);
float r = newRot - d;
matrix.postRotate(r, view.getMeasuredWidth() / 2,
view.getMeasuredHeight() / 2);
}
}
break;
}
view.setImageMatrix(matrix);
return true;
}
private float rotation(MotionEvent event) {
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
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);
}
答案 0 :(得分:1)
您必须在viewId
方法中为不同的onTouch()
条件添加条件。
试试这个
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.crown:
// put code for 1st crown touch
break;
case R.id.crown1:
// put code for 2nd crown touch
break;
default:
break;
}
return true;
}
答案 1 :(得分:0)
答案 2 :(得分:0)
如果你想在同一布局中移动,调整大小,缩放,旋转两个以上的视图,你可以选择android multi-touch controller,它简化了有点混乱和不一致的MotionEvent触摸点API。
控制器还支持双指缩放,包括跟踪屏幕坐标和对象坐标之间的转换。
最近更新了控制器以支持捏合旋转,允许您使用屏幕上的两个触摸点物理扭曲对象。
控制器可以很容易地使用单独对象的画布(例如一叠照片),每个对象可以使用单个触摸点单独拖动,也可以通过捏合操作进行缩放。