Android development, how to get approximate coordinates? - android

Ok, so I am making a simple app on a surfaceView where I have a bitmap of a ball that goes from the top of the screen to the bottom. Once it gets to the bottom it appears at the top again where it starts to fall back down again.
Next I tried to make it so that when I clicked the ball when it was falling down it would go back to the top of the screen. However, I am having a problem with this because I can't click it (because its only one pixel, I think). I set an onTouchListener to the getX() and getY() coordinates of the click, and if the x and y coordinatesof the click are equal to the x and y of the current position of the ball then the ball goes back to the top of the screen.
This doesnt work though. Because in order for me to be able to click the ball I would have to click the exact center pixle that the ball was on at that time. So my question is how do I say: If the click is close to, or approximately equal to the current position of the ball then go back to the top. I am a noob with this so if I am asking dumb questions I apologize, I'm trying my best to learn. And thank you very much for the help. I appreciate it.

It should work for you. Just override the onTouch method.
#Override
public boolean onTouch(View v, MotionEvent me)
{
try
{
Thread.sleep(50);
} catch (InterruptedException e)
{
e.printStackTrace();
}
switch (me.getAction())
{
case MotionEvent.ACTION_DOWN:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_UP:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_MOVE:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_OUTSIDE:
x = 100;
y = 200;
break;
}
return true;
}

Related

how to use multi touch in Motion event?

This is what I want to achieve:
In the below image there is a white dot on the screen and i want to move the white dot to the RIGHT when right part of the screen is touched and to the LEFT when left part of the screen is touched.
This is my code:
protected void onDraw(Canvas canvas)
{
final Bitmap mball = ball;
if (pressed){
if (touch_x>canvas.getWidth()/2)
left += 2;
else left -= 2;
}
canvas.drawBitmap(mball, left, canvas.getHeight()*0.7f, null);
invalidate();
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
final int action = event.getAction();
int pointerIndex;
switch (action ){
case MotionEvent.ACTION_DOWN:
touch_x = event.getX();
pressed = true;
break;
case MotionEvent.ACTION_POINTER_DOWN:
touch_x = event.getX(event.getActionIndex());
pressed = true;
break;
case MotionEvent.ACTION_UP:
mActivePointerId = -1;
pressed = false;
break;
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = -1;
break;
}
}
return pressed;
}
It is not working the way I want it to work. When the user touches the right part the white ball should move towards right and if LEFT part is touched without lifting off the RIGHT finger the ball should move towards left and vice-versa.
What is happening is if RIGHT part of screen is touched ball keeps moving towards right and when left part is touched without removing the right finger the ball continues to move towards right.
I have also tried using pointer Index for primary and non-primary pointers but still no luck.
Any help would be appreciated.

How to set position for image matrix?

I have a GridView with images. On long click the screen gets dark (with black half transparent image that gets visible) and another image become visible that has the same resource of the image long clicked from the grid.
What i want is to be able to drag that image (which i have succeeded), but the image shows at the top corner (as i designed the layout in the XML) and I want it to show up where I clicked (To be precised, i want the center of the dragable picture to be where I perfumed the long click).
My image is fill_parent on FrameLayout and is set on matrix scale to control position...
this is the image part of the XML:
<ImageView
android:id="#+id/imgMainSelectedMovie"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:visibility="invisible"
android:src="#drawable/escape" />
and this is the OnTouch() part:
public boolean onTouch(View v, MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
X = ev.getX();
Y = ev.getY();
if (longClicked) return true;
break;
case MotionEvent.ACTION_MOVE:
if (longClicked) {
if (!dragStarted) {
dragStarted = true;
myImageView.setX(X);
myImageView.setY(Y);
}
matrix.postTranslate((ev.getX() - X), (ev.getY() - Y));
X = ev.getX();
Y = ev.getY();
myImageView.setImageMatrix(matrix);
return true;
}
break;
case MotionEvent.ACTION_UP:
if (longClicked) {
dark.setVisibility(View.INVISIBLE);
myImageView.setVisibility(View.INVISIBLE);
longClicked = false;
return true;
}
}
return false;
}
thist is what I tried to solve the problem:
if (!dragStarted) {
dragStarted = true; //Changed to true when OnItemLongClick called
myImageView.setX(X);
myImageView.setY(Y);
}
But it only changes the "start point" of the image layout to the entered X and Y.
Problem solved :)
What I did is when onItemLongClick calls I took the X and Y measured from the ACTION_DOWN case in the onTouch, took the matrix current location and did postTranslate to the coordinates from the ACTION_DOWN minus the location of the image.
this is the solution part from the onItemLongCliick:
matrix.getValues(values);
matrixX = values[Matrix.MTRANS_X];
matrixY = values[Matrix.MTRANS_Y];
matrix.postTranslate(X-matrixX, Y-matrixY);
myImageView.setImageMatrix(matrix);
I'm wondering why they didn't put some kind of "setPlace" method for matrix...

"Live" get(x) and get(Y) values (for MotionEvents) ****UPDATE****

I was wondering how to get accurate, live get(x) and get(y) values for a MotionEvent? What is happening is that when I touch a specific area on the screen, I tell an action to happen.
The problem is that once I touch the screen and take my finger off, it still thinks my finger is at the same location (since that was the last location I touched). So when I have more than one Down event (for multitouch) it throws everything off. Is there a way to reset the X and Y values so when I let off the screen, they go back to 0 or null (or whatever)? Thanks
What you are describing isn't a problem. You the programmer are responsible for keeping track of the touch locations and what they mean. If you care about motion you need to keep track of the previous touch and the current touch each time a touch occurs. I find something like this works great:
public int x=-1,y=-1,prevX=-1, prevY=-1;
public boolean onTouch(View v, MotionEvent event)
{
prevX = x;
prevY = y;
int x = (int)event.getX();
int y = (int)event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// there is no prev touch
prevX = -1;
prevY = -1;
// touch down code
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
Just catch the MotionEvent.ACTION_UP or ACTION_MOVE event, depending on when the action needs to happen (since you want it live, you should use the ACTION_MOVE event). You should handle setting external variables when ACTION_DOWN occurs, and handle the results on ACTION_MOVE or ACTION_UP.

rotate image depending on user touch on screen

i am developing game.i am displaying gun object center bottom of the screen.when user tap on screen i need to rotate gun that direction.i done rotating image.but when user tap on screen i need to rotate image that direction.can you please help me
Thanks in advance
Aswan
is your image a bitmap? could you convert it to one? Would an onClick listener not work and when the user clicks do something like.. http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
Seeing some code as to what you've attempting would be nice also. The following will help you with checking if the bitmap has been clicked in case you're stuck on that also http://developer.android.com/reference/android/view/View.html .
I've never attemped this myself but I think that would work. Try redrawing the bitmap when you have resized it.
Just use onTouchListener for your View and use this code for that
#Override
public boolean onTouch(View v, MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
Log.i(TAG, "action type is"+event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
Log.i(TAG, "Entering in onTouch");
double rotationAngleRadians = Math.atan2(currentX - dialer.centerX, dialer.centerY - currentY);
dialer.rotationAngle = (int) Math.toDegrees(rotationAngleRadians);
Log.i(TAG, "rotaion angle"+dialer.rotationAngle);
dialer.invalidate();
return true;
}
}
return true;
}

How to move something from a virtual dpad on-screen

I've started a little game, and so far I'm moving a little guy with onKeyDown() and the DPAD from Android Emulator. Now what I want to do is to add 4 buttons on the screen (like in a GAMEBOY emulator for example) and these buttons should move my little guy. With a clickListener and onClick() (or touchListener and onTouch()), it's ok for one move but how to do if I want that my little guy continues moving when I stay clicked on the button ??? Buttons are enough or should I make a 4 arrows soft keyboard or anything else ??
Thanks
So with some searchs, I've now a virtual joystick on screen using :
public boolean onTouchEvent(MotionEvent event) {
float positionX = event.getX();
float positionY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Screen is pressed for the first time
break;
case MotionEvent.ACTION_MOVE:
// Screen is still pressed, float have been updated
break;
case MotionEvent.ACTION_UP:
// Screen is not anymore touched
break;
}
return true;
}
return super.onTouchEvent(event);
}

Categories

Resources