How to move something from a virtual dpad on-screen - android

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);
}

Related

Android Listener to detect when the screen is being pressed

I have a view in my Android app that should be updated constantly when the screen is being pressed. Most listeners I found only invalidate the view once when the user touches the screen for the first time, or when the user removes his hand from the screen.
Is there such a thing as a listener that is constantly triggered as long as the screen is touched ? Or is there any other way to do this ? I know, that sounds like something really simple, but I haven't found a way to do it.
Override onTouch() and set a flag in ACTION_DOWN. As long as ACTION_UP isn't called after that, the user is touching the screen.
boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
pressed = true;
break;
case MotionEvent.ACTION_MOVE:
//User is moving around on the screen
break;
case MotionEvent.ACTION_UP:
pressed = false;
break;
}
return pressed;
}

How to draw or write a text like MS-Paint in android using onTouch Event?

I have created panel with collection of different colors, pencil, eraser and different shapes similar to MS-Paint. I could be able to draw or write on a screen using Touch Event method. But when I draw something on a screen (when I touch the screen), MotionEvent.ACTION_Down method is calling. So it works fine. When I release my finger from the screen, MotionEvent.ACTION_up method is calling and works fine.
So, my problem is, like MS-PAINT I am not able to see what I drew or wrote before I release my finger from the screen. For an example, see this video. User can see when he dragged the shapes or trying to draw a pencil. Also, in this link user draws using pencil and it is visible without releasing a finger on the screen.
But, when I draw something on the screen, once I released the finger only it appears.
What I need is, when user touch the screen itself if he/she moves the finger on the screen, user must be able to see what they are trying to draw or write on the screen.
For an example: if I try to write some word like "Apple" on a screen, I am trying to put "A" . But when I write letter "A", it is invisible unless I take my finger from the screen. Once if I released my finger from the screen after I drew letter"A" then only the text or picture has been appeared on a screen what I drew.
So, I have done MotionEvent.ACTION_DOWN and MotionEVent.ACTION_UP. It works fine.
But, MotionEvent.ACTION_MOVE is not working properly at all.
This is my Code,
#Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(Shape == ShapeLine)
{
GraphicObject = new Line();
((Line) GraphicObject).getBegin().setX(event.getX());
((Line) GraphicObject).getBegin().setY(event.getY());
}
if(Shape== ShapeRect)
{
GraphicObject = new Rectangle();
Point temp = new Point(event.getX(), event.getY());
endPoint = new Point();
((Rectangle) GraphicObject).settemppointOfOneEndRectangle(temp);
}
else if(event.getAction() == MotionEvent.ACTION_MOVE){
if(Shape== ShapeLine)
{
final float x=event.getX();
final float y=event.getY();
}
if(Shape == ShapeRect)
{
endPoint.x=event.getX();
endPoint.y=event.getY();
invalidate();
}
Anyone suggest me, about ACTION_MOVE. I have tried a lot in a code but no changes and I didn't find any solution while moving.
Basic idea is when you tap record that point in a variable,then inside ACTION_MOVE record the current point and draw a line in between these 2 points.Once done save this point in the previous point. Sudo code:
Point last;
Point current;
...
case ACTION_DOWN:
last=mouse.position;
break;
case ACTION_MOVE:
current=mouse.position;
drawLine(current,last);
last=current;
break;
Do this way,your drawing should be fine.
N.B. Remember,this is a sudo code. :P
EDIT. Example from one of my app. Basically I pointed out what you should do:
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch(action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
initial.x=(int)event.getX();
initial.y=(int)event.getY();
break;
case MotionEvent.ACTION_MOVE:
current.x=(int)event.getX();
current.y=(int)event.getY();
//draw line using initial as start and current as end point
//sudo code: drawLine(initial,current)
//now set initial to current
initial=current// for the continuity of drawing.
break;
}
return true;
}
initial and current both are Point objects.

Android development, how to get approximate coordinates?

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;
}

"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;
}

Categories

Resources