Android:在视图中对2张图像进行多点触控

曲奇疯狂

目前,我在使用MultitouchView时遇到问题。我主要在这里使用Multitouchview的制作方式:http : //www.vogella.com/articles/AndroidTouch/article.html我的问题是,我的布局中有2个Imageview,并且我只想在两个都同时执行操作感动。我尝试通过获取位置(上,左,下,右)来找到它们,但是它总是返回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)。

目前,我在我的Images上摆弄了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();
}

} 
RIJO房车

请查看以下链接。它可能会给您一个项目构想。

Android多点触控

多点触控演示

多点触控

Android中的多点触控

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章