Please had a look on the below links. It may give you an idea for your project.
Android: Multitouch on 2 Images in a View
-
21-09-2022 - |
Pergunta
Currently, I have a Problem while using a MultitouchView. I mainly use the Multitouchview how it is made here: http://www.vogella.com/articles/AndroidTouch/article.html My Problem is, that i have 2 Imageviews in my Layout and I want to only take action if both are touched. I tried finding them by getting the positions (top, left, bottom, right), but it always returns a nullpointer-exception. However, I already tried getting them in the multitouchView like this:
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();
Since that didn't work I also already tried getting the positions in my Main Activity the same way and returning them with getters like this:
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();
And in the Main Activity:
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;
}
I am still learning Android (this is a project for my college) so if someone has an easy solution I would be very glad.
Also I am using a very Old phone for Testing, so i can't really use the functions getX or getY for my Image (currently using API Level 9).
Currently I'm fiddling with onTouchListeners on my Images, but I feel like it should work like this, so I want to know why it doesn't for me.
If needed here's my MultiTouchView.class as it is:
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();
}
}
Solução