我使用以下代码检测单指触摸和双指触摸。代码检测到双指触摸(count==2
时)。
我也需要在单点触控上做一些动作。如果我用一根手指触摸屏幕,它就不会用于其他部分。我在这段代码中做错了什么?
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_POINTER_UP: {
int count = event.getPointerCount();
Log.v("count >>", count + "");
if (count == 2) {
// some action
} else {
Log.v("count not equal to 2", "not 2");
}
break;
}
}
return true;
}
更新:
我在Activity
中使用了此代码。我需要检测单点触控,多点触控。
在我的活动中,我有两张图片,一张在左边,另一张在右边。如果我点击图像,一些过程必须要做。如果我使用两根手指,我需要根据scale-factor
重新调整图像大小。
这是我用过的代码:
已更新:
package com.pinch.detect;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class PinchDetectorActivity extends Activity {
TextView textGestureAction;
ImageView img1, img2;
static Bitmap bm, bm1;
boolean multiTouch = false;
Context context;
float scaleFactor = 0.0f;
float lastscale;
int scr_width, scr_height;
Display display;
private ScaleGestureDetector scaleGestureDetector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textGestureAction = (TextView) findViewById(R.id.GestureAction);
img1 = (ImageView) findViewById(R.id.img_left);
img2 = (ImageView) findViewById(R.id.img_right);
display = getWindowManager().getDefaultDisplay();
scr_width = display.getWidth();
scr_height = display.getHeight();
Log.v("width >>", Integer.toString(scr_width));
Log.v("height >>", Integer.toString(scr_height));
bm = BitmapFactory.decodeResource(this.getResources(),
R.drawable.fiction1);
img1.setImageBitmap(bm);
bm1 = BitmapFactory.decodeResource(this.getResources(),
R.drawable.fiction2);
img2.setImageBitmap(bm1);
img1.setScaleType(ImageView.ScaleType.FIT_START);
img2.setScaleType(ImageView.ScaleType.FIT_END);
scaleGestureDetector = new ScaleGestureDetector(this,
new MySimpleOnScaleGestureListener());
img1.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);
int index = event.getActionIndex();
Log.v("Index value ",index+"");
int action = event.getAction();
if (index == 1) {
multiTouch = true;
System.out.println("mutli1");
} else if (multiTouch == false) {
System.out.println("single1");
img1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("clicked image1","img1>>>");
}
});
img2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { // TODO Auto-generated method
Log.v("clicked image2","img2>>>");
}
});
}
else if (action == MotionEvent.ACTION_MOVE && multiTouch) {
System.out.println("mutli2");
if (scaleFactor > 1) {
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm,
scr_width / 2, scr_height, true);
img1.setImageBitmap(resizedbitmap);
Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1,
scr_width / 2, scr_height, true);
img2.setImageBitmap(resizedbitmap1);
Log.v("width >>", Integer.toString(scr_width));
Log.v("height >>", Integer.toString(scr_height));
} else if(scaleFactor<1){
Log.v("width >>", Integer.toString(scr_width));
Log.v("height >>", Integer.toString(scr_height));
if (scr_width >= 640) {
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm,
scr_height + 90, scr_height, true);
img1.setImageBitmap(resizedbitmap);
Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1,
scr_height + 90, scr_height, true);
img2.setImageBitmap(resizedbitmap1);
} else {
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm,
scr_height, scr_height + 30, true);
img1.setImageBitmap(resizedbitmap);
Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1,
scr_height, scr_height + 30, true);
img2.setImageBitmap(resizedbitmap1);
}
}
} else if (action == MotionEvent.ACTION_MOVE && !multiTouch)
{
System.out.println("single2");
}
return super.onTouchEvent(event);
}
public class MySimpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
scaleFactor = detector.getScaleFactor();
Log.v("scaleFactor >>>", scaleFactor + "");
return true;
}
}
}
我的main.xml是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/pinchlay" >
<ImageView
android:id="@+id/img_left"
android:layout_width="320dp"
android:layout_height="fill_parent"
/>
<ImageView
android:id="@+id/img_right"
android:layout_width="320dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
答案 0 :(得分:16)
如果我理解正确,单击不会导致ACTION_POINTER_UP,而是ACTION_UP。这就是它没有检测到单指的原因。看这篇文章以供参考: https://stackoverflow.com/a/4269592/1084813
答案 1 :(得分:5)
正如之前的海报建议的那样,单个触摸指针有单独的事件,而不是动作。 motionEvents序列如下:
答案 2 :(得分:2)
以下是检测点并以不同颜色绘制每个点的代码get competition example here
public class custom_view extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public int cu = 0;
final int MAX_NUMBER_OF_POINT = 10;
float[] x = new float[MAX_NUMBER_OF_POINT];
float[] y = new float[MAX_NUMBER_OF_POINT];
boolean[] touching = new boolean[MAX_NUMBER_OF_POINT];
public custom_view(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public custom_view(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public custom_view(Context context) {
super(context);
init();
}
void init() {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(40);
}
@Override
protected void onDraw(Canvas canvas) {
for (int i = 0; i < MAX_NUMBER_OF_POINT; i++) {
if (touching[i]) {
switch (i) {
case 1:
paint.setColor(Color.BLUE);
break;
case 2:
paint.setColor(Color.RED);
break;
case 3:
paint.setColor(Color.CYAN);
break;
case 4:
paint.setColor(Color.GREEN);
break;
case 5:
paint.setColor(Color.YELLOW);
break;
case 6:
paint.setColor(Color.MAGENTA);
break;
case 7:
paint.setColor(Color.DKGRAY);
break;
case 8:
paint.setColor(Color.LTGRAY);
break;
case 9:
paint.setColor(Color.GREEN);
break;
case 10:
paint.setColor(Color.BLACK);
break;
}
canvas.drawCircle(x[i], y[i], 70f, paint);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = (event.getAction() & MotionEvent.ACTION_MASK);
int pointCount = event.getPointerCount();
for (int i = 0; i < pointCount; i++) {
int id = event.getPointerId(i);
if (id < MAX_NUMBER_OF_POINT) {
x[id] = (int) event.getX(i);
y[id] = (int) event.getY(i);
if ((action == MotionEvent.ACTION_DOWN)
|| (action == MotionEvent.ACTION_POINTER_DOWN)
|| (action == MotionEvent.ACTION_MOVE)) {
touching[id] = true;
} else {
touching[id] = false;
}
}
}
invalidate();
return true;
}
}
答案 3 :(得分:1)
您是否尝试使用OnDoubleTapListener?
找到这个tutorial也很有帮助,展示了如何检测单击和双击。
答案 4 :(得分:1)
单次触摸我无法重现count == 2。单手触摸对我来说是1。 尝试在真实设备上调试您的代码并查看您实际拥有的内容。
将event.getPointerId(i)与event.getPointerCount()一起使用,看看这些实际触摸点是什么。
答案 5 :(得分:1)
您检查了ACTION_DOWN
,但同时您还需要检查ACTION_POINTER_DOWN
,这更清楚了。试试吧
@Override
public boolean onTouchEvent(MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);
int action = event.getAction();
if( (action == MotionEvent.ACTION_DOWN) && (action != MotionEvent.ACTION_POINTER_DOWN))
// Single touch
else if ( action == MotionEvent.ACTION_POINTER_DOWN)
//multi touch
return true;
}
被修改
boolean multiTouch=false;
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int index= event.getActionIndex();
int action = event.getAction();
if( index ==1)
{
multiTouch=true;
System.out.println("mutli");
}
else if (multiTouch==false)
System.out.println("single");
//else if (action== MotionEvent.ACTION_POINTER_1_UP)
// multiTouch=false;
else if (action== MotionEvent.ACTION_MOVE && multiTouch)
System.out.println("mutli");
else if (action== MotionEvent.ACTION_MOVE && !multiTouch)
System.out.println("single");
return super.onTouchEvent(event);
}
编辑:
package com.pinch.detect;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class PinchDetectorActivity extends Activity {
TextView textGestureAction;
ImageView img1, img2;
static Bitmap bm, bm1;
boolean multiTouch = false;
Context context;
float scaleFactor = 0.0f;
float lastscale;
int scr_width, scr_height;
Display display;
private ScaleGestureDetector scaleGestureDetector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textGestureAction = (TextView) findViewById(R.id.GestureAction);
img1 = (ImageView) findViewById(R.id.img_left);
img2 = (ImageView) findViewById(R.id.img_right);
display = getWindowManager().getDefaultDisplay();
scr_width = display.getWidth();
scr_height = display.getHeight();
Log.v("width >>", Integer.toString(scr_width));
Log.v("height >>", Integer.toString(scr_height));
bm = BitmapFactory.decodeResource(this.getResources(),
R.drawable.fiction1);
img1.setImageBitmap(bm);
bm1 = BitmapFactory.decodeResource(this.getResources(),
R.drawable.fiction2);
img2.setImageBitmap(bm1);
img1.setScaleType(ImageView.ScaleType.FIT_START);
img2.setScaleType(ImageView.ScaleType.FIT_END);
scaleGestureDetector = new ScaleGestureDetector(this,
new MySimpleOnScaleGestureListener());
img1.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
system.out.println("Image 1 touched");
return false;
}
});
img2.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
system.out.println("Image 2 touched");
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);
int index = event.getActionIndex();
Log.v("Index value ",index+"");
int action = event.getAction();
if (index == 1) {
multiTouch = true;
System.out.println("mutli1");
} else if (multiTouch == false) {
System.out.println("single1");
img1.onTouch(view, event);
}
else if (action == MotionEvent.ACTION_MOVE && multiTouch) {
System.out.println("mutli2");
img2.onTouch(view, event);
if (scaleFactor > 1) {
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm,
scr_width / 2, scr_height, true);
img1.setImageBitmap(resizedbitmap);
Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1,
scr_width / 2, scr_height, true);
img2.setImageBitmap(resizedbitmap1);
Log.v("width >>", Integer.toString(scr_width));
Log.v("height >>", Integer.toString(scr_height));
} else if(scaleFactor<1){
Log.v("width >>", Integer.toString(scr_width));
Log.v("height >>", Integer.toString(scr_height));
if (scr_width >= 640) {
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm,
scr_height + 90, scr_height, true);
img1.setImageBitmap(resizedbitmap);
Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1,
scr_height + 90, scr_height, true);
img2.setImageBitmap(resizedbitmap1);
} else {
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm,
scr_height, scr_height + 30, true);
img1.setImageBitmap(resizedbitmap);
Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1,
scr_height, scr_height + 30, true);
img2.setImageBitmap(resizedbitmap1);
}
}
} else if (action == MotionEvent.ACTION_MOVE && !multiTouch)
{
System.out.println("single2");
}
return super.onTouchEvent(event);
}
public class MySimpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
scaleFactor = detector.getScaleFactor();
Log.v("scaleFactor >>>", scaleFactor + "");
return true;
}
}
}
最新:
if (index == 1) {
multiTouch = true;
img2.onTouch(view, event);
System.out.println("mutli1");
} else if (multiTouch == false) {
System.out.println("single1");
img1.onTouch(view, event);
}
else if (action == MotionEvent.ACTION_MOVE && multiTouch) {
System.out.println("mutli2");
答案 6 :(得分:1)
您必须使用MultiTouch的PointerID用户。见 Complete Example
private View.OnTouchListener OnTouchListener = new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
int pointerIndex = ((event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT);
int action = event.getAction() & MotionEvent.ACTION_MASK;
int pointerId = event.getPointerId(pointerIndex);
Log.i("", "Pointer ID = " + pointerId);
switch (action) {
case MotionEvent.ACTION_POINTER_UP: {
break;
}
}
return true;
}
};