1

I wan't to use a image view like below, to create a virtual cross controller or d-pad (It's not possible in my case to do this with different buttons).

When the cross image is displayed on the screen, I wan't to check on wich side the user has pressed to call a function like up(), down(), left() and right().

Cross image:

The image of the cross

Yaz
  • 492
  • 1
  • 6
  • 20
CodeWhisperer
  • 1,143
  • 2
  • 19
  • 39
  • 2
    Do you read this post: http://stackoverflow.com/questions/6038867/android-how-to-detect-touch-location-on-imageview-if-the-image-view-is-scaled-b ? – Simone Sessa Mar 13 '16 at 13:17
  • Thanks for your posted link. But Is there an easy calculation that I can decide which coordinates are associated with each side of the cross? – CodeWhisperer Mar 13 '16 at 16:42
  • Use an [OnTouchListener](https://developer.android.com/reference/android/view/View.OnTouchListener.html) and calculate the position. It should be pretty simple, if X is more than half of your width, you're on the right. – DariusL May 31 '16 at 09:59
  • @CodeWhisperer Yes, have you tried the link posted by @simone_s1994? GetX and GetY? You should do that calculation yourself, because you know the areas which should react. – RvdK May 31 '16 at 10:00

1 Answers1

1
   imageView1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int x = (int)event.getX();
            int y = (int)event.getY();

            if((x>170&&x<230)&&(y>0&&y<170))
            {//up
                Toast.makeText(MainActivity.this,"up",Toast.LENGTH_SHORT).show();
                 }
            else if((x>170&&x<230)&&(y>230&&y<400))
            {//down
                Toast.makeText(MainActivity.this,"down",Toast.LENGTH_SHORT).show();
            }
            else if((x>0&&x<170)&&(y>170&&y<230))
            {//left
                Toast.makeText(MainActivity.this,"left",Toast.LENGTH_SHORT).show();
            }
            else if((x>230&&x<400)&&(y>170&&y<230))
            {//right
                Toast.makeText(MainActivity.this,"right",Toast.LENGTH_SHORT).show();
            }
            return false;

        }
    });

//take iamge view 400px*400px

Madhav Gor
  • 211
  • 2
  • 12