Android:视图中的2个图像上的多点触控

时间:2013-12-26 12:00:15

标签: android imageview multi-touch

目前,我在使用MultitouchView时遇到了问题。我主要使用Multitouchview如何在这里制作:http://www.vogella.com/articles/AndroidTouch/article.html 我的问题是,我的布局中有2个图像视图,我想只触及两个图像视图。我尝试通过获取位置(顶部,左侧,底部,右侧)来找到它们,但它总是返回一个nullpointer-exception。但是,我已经尝试过将它们放入multitouchView中,如下所示:

ImageView leftImage =(ImageView) findViewById(R.id.leftAlert);
int topLeft = leftImage.getTop();
int botLeft = leftImage.getBottom();
int leftLeft = leftImage.getLeft();
int rightLeft = leftImage.getRight();
ImageView rightImage =(ImageView) findViewById(R.id.rightAlert);
int topRight = rightImage.getTop();
int botRight = rightImage.getBottom();
int leftRight = rightImage.getLeft();
int rightRight = rightImage.getRight();

由于这不起作用,我也已尝试以同样的方式获取我的主活动中的位置并使用这样的getter返回它们:

    MainActivity Views = new MainActivity();

int topLeft= Views.getTopLeft(); 
int botLeft=Views.getBotLeft();
int leftLeft=Views.getleftLeft();
int rightLeft=Views.getRightLeft();
int topRight=Views.getTopRight();
int botRight=Views.getBotRight();
int leftRight=Views.getleftRight();
int rightRight=Views.getRightRight();

在主要活动中:

ImageView leftImage =(ImageView) findViewById(R.id.leftAlert);
int topLeft = leftImage.getTop();
int botLeft = leftImage.getBottom();
int leftLeft = leftImage.getLeft();
int rightLeft = leftImage.getRight();
ImageView rightImage =(ImageView) findViewById(R.id.rightAlert);
int topRight = rightImage.getTop();
int botRight = rightImage.getBottom();
int leftRight = rightImage.getLeft();
int rightRight = rightImage.getRight();

public int getTopLeft(){
    return topLeft;
}

public int getBotLeft(){
    return botLeft;
}

public int getleftLeft(){
    return leftLeft;
}

public int getRightLeft(){
    return rightLeft;
}

public int getTopRight(){
    return topLeft;
}

public int getBotRight(){
    return botLeft;
}

public int getleftRight(){
    return leftLeft;
}

public int getRightRight(){
    return rightLeft;
}

我还在学习Android(这是我大学的一个项目),所以如果有人有一个简单的解决方案,我会很高兴。

此外,我正在使用非常旧的手机进行测试,因此我无法真正使用函数getX或getY作为我的图像(目前使用API​​级别9)。

目前我正在摆弄我的图片上的onTouchListeners,但我觉得它应该像这样工作,所以我想知道它为什么不适合我。

如果需要,这是我的MultiTouchView.class:

     package com.example.webanwendungengps;
     import android.content.Context;
     import android.graphics.PointF;
     import android.util.AttributeSet;
     import android.util.SparseArray;
     import android.view.MotionEvent;
     import android.view.View;
     import android.widget.Toast;
     //this entire class is a View, that is able to handle multiple touches on a Screen
     public class MultitouchView extends View {

     MainActivity Views = new MainActivity();

    int topLeft= Views.getTopLeft(); 
    int botLeft=Views.getBotLeft();
    int leftLeft=Views.getleftLeft();
    int rightLeft=Views.getRightLeft();
    int topRight=Views.getTopRight();
    int botRight=Views.getBotRight();
    int leftRight=Views.getleftRight();
    int rightRight=Views.getRightRight();

     private SparseArray<PointF> mActivePointers;

     public MultitouchView(Context context, AttributeSet attrs) {
     super(context, attrs);
     initView();   
     } 

     private void initView() {
      mActivePointers = new SparseArray<PointF>();
     }

     @Override
     public boolean onTouchEvent(MotionEvent event) {

     // get pointer index from the event object
     int pointerIndex = event.getActionIndex();

     // get pointer ID
     int pointerId = event.getPointerId(pointerIndex);

     // get masked (not specific to a pointer) action
     int maskedAction = event.getActionMasked();

     switch (maskedAction) {

     case MotionEvent.ACTION_DOWN:{
      float posX=event.getX();
      float posY=event.getY();


      if ((posX > leftLeft && posX <rightLeft && posY>botLeft && posY>topLeft)|| 
              (posX > leftRight && posX <rightRight && posY>botRight && posY>topRight)){
          ShowToast();  
      }
    }
    case MotionEvent.ACTION_POINTER_DOWN: {
      if(event.getPointerCount()>=2){
          float posX2=event.getX();
          float posY2=event.getY();
          if ((posX2 > leftLeft && posX2 <rightLeft && posY2>botLeft && posY2>topLeft)|| 
                  (posX2 > leftRight && posX2 <rightRight && posY2>botRight && posY2>topRight) ){
              ShowToast();  
          }
      }      
      break;
     }

    case MotionEvent.ACTION_MOVE: { // a pointer was moved
    }
    case MotionEvent.ACTION_UP: {
   //Do Nothing
    }
    case MotionEvent.ACTION_POINTER_UP: {
        //Do Nothing

    }
    case MotionEvent.ACTION_CANCEL: {
      mActivePointers.remove(pointerId);

      break;
    }
    }

    invalidate();
    return true;
  }

private void ShowToast() {
            Toast.makeText(getContext(), "Test", Toast.LENGTH_SHORT).show();
}

} 

1 个答案:

答案 0 :(得分:2)

请查看以下链接。它可以为您的项目提供一个想法。

Android Multitouch

multitouch demo

multitouch handling

Multitouch in android