问题描述
我用下面的code检测单指触控,双指触控。在code检测到双手指触摸时(计数== 2
)。
我需要做单点触摸的一些动作也。如果我触摸屏幕用一个手指就不会去为别人的一部分。我在这code做错了?
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
INT行动= event.getAction()及MotionEvent.ACTION_MASK;
开关(动作){
案例MotionEvent.ACTION_POINTER_UP:{
诠释计数= event.getPointerCount();
Log.v(算>>中,数+);
如果(计数== 2){
//一些行动
} 其他 {
Log.v(计数不等于2,不是2);
}
打破;
}
}
返回true;
}
更新:
我用这个code的活动
里面。我需要做检测单点触摸,多点触摸。
在我的活动,我有两个图像,一个在左边,一个在右边。如果我点击了图像处理的一些已经完成。如果我用两个手指,我需要重新大小的图像,根据比例因子
。
这是我用code:
更新:
包com.pinch.detect;
进口java.io.BufferedInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.MalformedURLException;
进口的java.net.URL;
进口java.net.URLConnection中;
进口android.app.Activity;
进口android.content.Context;
进口android.content.ContextWrapper;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;
进口android.util.FloatMath;
进口android.util.Log;
进口android.view.Display;
进口android.view.MotionEvent;
进口android.view.ScaleGestureDetector;
进口android.view.View;
进口android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
进口android.view.View.OnClickListener;
进口android.view.View.OnTouchListener;
进口android.widget.ImageView;
进口android.widget.TextView;
进口android.widget.Toast;
公共类PinchDetectorActivity延伸活动{
TextView的textGestureAction;
ImageView的IMG1,IMG2;
静态位图BM,BM1;
布尔多点触控= FALSE;
上下文语境;
浮动比例因子= 0.0;
浮动lastscale;
INT scr_width,scr_height;
显示屏显示;
私人ScaleGestureDetector scaleGestureDetector;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包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);
显示器= getWindowManager()getDefaultDisplay()。
scr_width = display.getWidth();
scr_height = display.getHeight();
Log.v(宽度GT;>中,Integer.toString(scr_width));
Log.v(高度>>中,Integer.toString(scr_height));
BM = BitmapFactory.de codeResource(this.getResources()
R.drawable.fiction1);
img1.setImageBitmap(BM);
BM1 = BitmapFactory.de codeResource(this.getResources()
R.drawable.fiction2);
img2.setImageBitmap(BM1);
img1.setScaleType(ImageView.ScaleType.FIT_START);
img2.setScaleType(ImageView.ScaleType.FIT_END);
scaleGestureDetector =新ScaleGestureDetector(这一点,
新MySimpleOnScaleGestureListener());
img1.setOnTouchListener(新OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
// TODO自动生成方法存根
返回false;
}
});
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
scaleGestureDetector.onTouchEvent(事件);
INT指数= event.getActionIndex();
Log.v(指数值,指数+);
INT行动= event.getAction();
如果(索引== 1){
多点触控=真;
的System.out.println(mutli1);
}否则,如果(多点触控== FALSE){
的System.out.println(single1);
img1.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
Log.v(点击image1的,IMG1>>>中);
}
});
img2.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){// TODO自动生成方法
Log.v(点击图像2,IMG2>>>中);
}
});
}
否则,如果(行动== MotionEvent.ACTION_MOVE和放大器;&安培;多点触控){
的System.out.println(mutli2);
如果(比例因子大于1){
位图resizedbitmap = Bitmap.createScaledBitmap(BM,
scr_width / 2,scr_height,真正的);
img1.setImageBitmap(resizedbitmap);
位图resizedbitmap1 = Bitmap.createScaledBitmap(BM1,
scr_width / 2,scr_height,真正的);
img2.setImageBitmap(resizedbitmap1);
Log.v(宽度GT;>中,Integer.toString(scr_width));
Log.v(高度>>中,Integer.toString(scr_height));
}否则如果(比例因子< 1){
Log.v(宽度GT;>中,Integer.toString(scr_width));
Log.v(高度>>中,Integer.toString(scr_height));
如果(scr_width> = 640){
位图resizedbitmap = Bitmap.createScaledBitmap(BM,
scr_height + 90,scr_height,真正的);
img1.setImageBitmap(resizedbitmap);
位图resizedbitmap1 = Bitmap.createScaledBitmap(BM1,
scr_height + 90,scr_height,真正的);
img2.setImageBitmap(resizedbitmap1);
} 其他 {
位图resizedbitmap = Bitmap.createScaledBitmap(BM,
scr_height,scr_height + 30,真正的);
img1.setImageBitmap(resizedbitmap);
位图resizedbitmap1 = Bitmap.createScaledBitmap(BM1,
scr_height,scr_height + 30,真正的);
img2.setImageBitmap(resizedbitmap1);
}
}
}否则,如果(行动== MotionEvent.ACTION_MOVE和放大器;&安培;!多点触控)
{
的System.out.println(single2);
}
返回super.onTouchEvent(事件);
}
公共类MySimpleOnScaleGestureListener扩展
SimpleOnScaleGestureListener {
@覆盖
公共布尔onScale(ScaleGestureDetector检测器){
// TODO自动生成方法存根
比例因子= detector.getScaleFactor();
Log.v(比例因子>>>中,比例因子+);
返回true;
}
}
}
和我的main.xml是
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:ID =@ + ID / pinchlay>
< ImageView的
机器人:ID =@ + ID / img_left
机器人:layout_width =320dp
机器人:layout_height =FILL_PARENT
/>
< ImageView的
机器人:ID =@ + ID / img_right
机器人:layout_width =320dp
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentRight =真
/>
< / RelativeLayout的>
如果我理解正确的话,单点触摸不会引起ACTION_POINTER_UP,但ACTION_UP。这就是为什么它没有检测到单个手指。看到这个帖子,以供参考: http://stackoverflow.com/a/4269592/1084813
I used the following code to detect the single finger touch and double finger touch. The code detects the double finger touch (when count==2
).
I need to do some action on single touch also. If I touch the screen with one finger it doesn't go for the else part. What I have done wrong in this code?
@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;
}
Update:
I used this code inside an Activity
. I need do detect single touch, multi-touch.
In my activity, I have two images, one on the left and one on the right. If I click the image some process has to done. If I use two fingers, I need to re-size the images, according to the scaleFactor
.
This is the code that I've used:
Updated :
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;
}
}
}
and my main.xml is
<?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>
If I understand correctly, single touch will not cause ACTION_POINTER_UP, but ACTION_UP. That is why it doesn't detect the single finger. See this post for reference:http://stackoverflow.com/a/4269592/1084813
这篇关于如何检测单点触摸和多点触控的onTouchEvent()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!