您好我想创建2个按钮,我想要多点触控?
我试图在互联网上做但没有例子。
所以如果你有一个可以分享或者你能给我意见吗? 我的代码是这个但不支持多点触控
package multi.touch;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
public class baslat extends Activity implements OnTouchListener {
TextView yazi;
TextView bir,iki;
Button buton1,buton2;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
yazi=(TextView)findViewById(R.id.textView1);
bir=(TextView)findViewById(R.id.textView2);
iki=(TextView)findViewById(R.id.textView3);
buton1=(Button)findViewById(R.id.button1);
buton2=(Button)findViewById(R.id.button2);
buton2.setOnTouchListener(this);
buton1.setOnTouchListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
yazi.setText(String.valueOf(event.getPointerCount()+"\n\n"));
bir.setText(String.valueOf("Birinci "
+ (int)event.getX(0)+"\n\n"+(int)event.getY(0)));
iki.setText(String.valueOf("Ikinci"+
(int)event.getX(1)+"\n\n"+(int)event.getY(1)));
//buton2.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX(0),
(int)event.getY(0))); return
super.onTouchEvent(event);
} @Override public boolean onTouch(View v, MotionEvent event) {
Button fds=(Button)v;
return false; }
}
答案 0 :(得分:6)
老问题但是我在这个问题上遇到了问题,直到我最终遇到了设置
android:splitMotionEvents="true"
在包含按钮视图的布局视图上,允许按下多个按钮,我发现在sdk演示下载中可用的ApiDemos中挖掘
答案 1 :(得分:4)
在Android UI框架中,所有触摸事件都属于触摸源自的视图。因此,如果您触摸按钮,则通过该按钮处理所有触摸事件,直到您抬起手指;这包括用于多点触控的其他触控指针。根据我的经验,在单独的View对象(您的2个按钮)上实现多点触控的唯一方法是在一个视图中捕获覆盖整个屏幕的所有触摸事件,并自己委派触摸事件。这有点工作,但可以做到。
一个例子可能是包含一个填充屏幕但没有可绘制源(或它完全透明)的ImageView。将它置于其他元素之上(可能使用FrameLayout),并在ImageView的onTouchEvent()方法中,分析触摸坐标,并将触摸事件传递给正确的按钮。多个触摸指针会变得相当复杂,但据我所知,这是将触摸事件传递给单独的View对象的唯一方法。
答案 2 :(得分:2)
我对这个主题感到有点厌倦,我用多点触控按钮做了一个处理程序类。随意使用和/或更新。
//CLASS TO HANDLE THE EVENT
public class MultitouchButtonHandler {
ArrayList<View> views_info = new ArrayList<View>();
public MultitouchButtonHandlerResult onTouchEvent(View v, MotionEvent ev) {
//GET EVENT INFO
final int action = ev.getAction();
int action_masked = action & MotionEvent.ACTION_MASK;
if(action_masked==MotionEvent.ACTION_MOVE){
return null;
}
//GET ABSOLUTE SIZE AND CHECK IF THIS ANY VIEW ATTACHED TO THIS POSITION
final int original_location[] = { 0, 0 };
v.getLocationOnScreen(original_location);
final int actionPointerIndex = ev.getActionIndex();
float rawX = (int) ev.getX(actionPointerIndex) + original_location[0];
float rawY = (int) ev.getY(actionPointerIndex) + original_location[1];
View eventView = getViewByLocation((int)rawX, (int)rawY);
if(eventView==null) return null;
MultitouchButtonHandlerResult result = new MultitouchButtonHandlerResult();
result.view = eventView;
//CHECK WHAT KIND OF EVENT HAPPENED
switch (action_masked) {
case MotionEvent.ACTION_DOWN: {
result.event_type = MotionEvent.ACTION_DOWN;
return result;
}
case MotionEvent.ACTION_UP: {
result.event_type = MotionEvent.ACTION_UP;
return result;
}
case MotionEvent.ACTION_CANCEL: {
result.event_type = MotionEvent.ACTION_CANCEL;
return result;
}
case MotionEvent.ACTION_POINTER_UP: {
result.event_type = MotionEvent.ACTION_UP;
return result;
}
case MotionEvent.ACTION_POINTER_DOWN: {
result.event_type = MotionEvent.ACTION_DOWN;
return result;
}
default:
break;
}
return null;
}
public void addMultiTouchView(View v){
views_info.add(v);;
}
public void removeMultiTouchView(View v){
views_info.remove(v);;
}
public View getViewByLocation(int x, int y){
for(int key=0; key!= views_info.size(); key++){
View v = views_info.get(key);
//GET BUTTON ABSOLUTE POSITION ON SCREEN
int[] v_location = { 0, 0 };
v.getLocationOnScreen(v_location);
//FIND THE BOUNDS
Point min = new Point(v_location[0], v_location[1]);
Point max = new Point(min.x + v.getWidth(), min.y + v.getHeight());
if(x>=min.x && x<=max.x && y>=min.y && y<=max.y){
//Log.d("mylog", "***Found a view: " + v.getId());
return v;
}
}
//Log.d("mylog", "Searching: " + x +", " + y + " but not found!");
return null;
}
}
//CLASS TO HANDLE THE EVENT
public class MultitouchButtonHandler {
ArrayList<View> views_info = new ArrayList<View>();
public MultitouchButtonHandlerResult onTouchEvent(View v, MotionEvent ev) {
//GET EVENT INFO
final int action = ev.getAction();
int action_masked = action & MotionEvent.ACTION_MASK;
if(action_masked==MotionEvent.ACTION_MOVE){
return null;
}
//GET ABSOLUTE SIZE AND CHECK IF THIS ANY VIEW ATTACHED TO THIS POSITION
final int original_location[] = { 0, 0 };
v.getLocationOnScreen(original_location);
final int actionPointerIndex = ev.getActionIndex();
float rawX = (int) ev.getX(actionPointerIndex) + original_location[0];
float rawY = (int) ev.getY(actionPointerIndex) + original_location[1];
View eventView = getViewByLocation((int)rawX, (int)rawY);
if(eventView==null) return null;
MultitouchButtonHandlerResult result = new MultitouchButtonHandlerResult();
result.view = eventView;
//CHECK WHAT KIND OF EVENT HAPPENED
switch (action_masked) {
case MotionEvent.ACTION_DOWN: {
result.event_type = MotionEvent.ACTION_DOWN;
return result;
}
case MotionEvent.ACTION_UP: {
result.event_type = MotionEvent.ACTION_UP;
return result;
}
case MotionEvent.ACTION_CANCEL: {
result.event_type = MotionEvent.ACTION_CANCEL;
return result;
}
case MotionEvent.ACTION_POINTER_UP: {
result.event_type = MotionEvent.ACTION_UP;
return result;
}
case MotionEvent.ACTION_POINTER_DOWN: {
result.event_type = MotionEvent.ACTION_DOWN;
return result;
}
default:
break;
}
return null;
}
public void addMultiTouchView(View v){
views_info.add(v);;
}
public void removeMultiTouchView(View v){
views_info.remove(v);;
}
public View getViewByLocation(int x, int y){
for(int key=0; key!= views_info.size(); key++){
View v = views_info.get(key);
//GET BUTTON ABSOLUTE POSITION ON SCREEN
int[] v_location = { 0, 0 };
v.getLocationOnScreen(v_location);
//FIND THE BOUNDS
Point min = new Point(v_location[0], v_location[1]);
Point max = new Point(min.x + v.getWidth(), min.y + v.getHeight());
if(x>=min.x && x<=max.x && y>=min.y && y<=max.y){
//Log.d("mylog", "***Found a view: " + v.getId());
return v;
}
}
//Log.d("mylog", "Searching: " + x +", " + y + " but not found!");
return null;
}
}
//CLASS TO FULLFILL WITH RESULT
public class MultitouchButtonHandlerResult {
public View view;
public int event_type;
}
//CLASS TO FULLFILL WITH RESULT
public class MultitouchButtonHandlerResult {
public View view;
public int event_type;
}
答案 3 :(得分:0)
您是否检查了这个链接 -
如何在Android 2中使用Multi-touch http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2/1747