This is my requirement in my project,I am developing an application in android on canvas.The canvas covers the entire screen,I am drawing a bitmap on the canvas depends upon the aspect ration with the canvas.How to make restrict drawing outside of bitmap,Could any one help in this.
Your onTouch should look something like this, yes?
public boolean onTouch(View v, MotionEvent event) {
x = (int) event.getRawX();
y = (int) event.getRawY();
/* other code */
}
Well, what you can do is restrict the onTouch method from doing anything when X and Y are not on the bitmap.
onTouch also returns a boolean if an onTouch has "really" consumed an event. In this case, you want to return true when the X, Y values are on the bitmap, but false when X, Y values are off the bitmap.
Also, if you want the event to act like they are lifting their finger up when they are off the bitmap, you can even call onTouch again passing in an event like:
this.onTouch(v, MotionEvent.ACTION_UP);
I hope this helps!
Related
I'm writing a simple android game using Libgdx.
I had set boundary to my Actor like that:
setBounds(posX, posY, width, heigh);
I believe that all touch events will only occur in that zone. But touchUp event acts abnormally, when I touch inside actor's boundary, then drag out of that boundary, touchUp event still occurs.
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
//Touch up event still occur out of actor's boundary
}
Did I make anything wrong here?
Thanks in advance and sorry for my English :)
I assume you are talking about InputListener's touchUp method. According to docs this is called when a finger touch goes up anywhere! So no, you didn't do anything wrong: It is designed like this.
public void touchUp(InputEvent event,
float x,
float y,
int pointer,
int button)
Called when a mouse button or a finger touch goes up anywhere, but
only if touchDown previously returned true for the mouse button or
touch. The touchUp event is always handled.
I have developed a game that shoots when player touches the screen by using onTouchListener for my custom SurfaceView and Thread.
But now I want to change the approach and instead of onTouchListener I added onTouchEvent to the SurfaceView o my Activity.
The problem is that I get some kind of offset when I click on the emulator.
Everything is working great, except that offset keeps appearing and I don't understand why.
Also let me mention that my app is running in landscape mode, maybe this is relevant.
I suspect that it isn't working properly because onTouchListener was added to the view and depended on it, but onTouchEvent doesn't depend on the view.
Also my view doesn't have any padding or margin properties, it is full-screen view (fill_parent).
Does anyone have any ideas on this?
I have done my application, and everything works correctly now, but i still do not know what the problem was.
After lots of debugging of my application the onTouchEvent returned random Y values that were always higher than the ones that the onTouchListener returned. And i am not sure why this is hapening since my view that recognizes the onTouchListener is a full-screen view.
So i figured out a way to get passed this by doing some math.
The first function that the android calls is the onTouch method which gives the correct values but just for one touch. I needed it to give right values even on the MotionEvent.ACTION_MOVE so i noticed that MotionEvent.ACTION_MOVE is actually doing correctly depending on the first touch recognized by the onTouchEvent.
So i just got the coordinate Y from the onTouch and the different coordinate with the offset from onTouchEvent calculated the difference and in every onTouchEvent from there, until the user lifts up their finger, i just subtract that difference and that gives me the correct value.
If anyone else has this problem, and doesn't know how to fix it, here is my code, maybe it will be helpful.
#Override
public boolean onTouchEvent(MotionEvent arg1) {
/*you can only touch when the thread is running*/
if(game.state() != STATE_PAUSE){
if(arg1.getAction() == MotionEvent.ACTION_DOWN){
coordinateX = arg1.getX();
coordinateY = arg1.getY();
differenceY = Math.abs(coordinateY - touchedY);
coordinateY = coordinateY - differenceY;
shootingIsOkay = true;
game.setDrawCircle(coordinateX,coordinateY);
}
if(arg1.getAction() == MotionEvent.ACTION_MOVE){
coordinateX = arg1.getX();
coordinateY = arg1.getY();
coordinateY = coordinateY - differenceY;
shootingIsOkay = true;
game.setDrawCircle(coordinateX,coordinateY);
}
if(arg1.getAction() == MotionEvent.ACTION_UP){
shootingIsOkay = false;
}
}
return false;
}
And the onTouch method that is called from the onTouchListener that depends on the view is just simple, here:
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
touchedY = arg1.getY();
return false;
}
If anyone knows how to fix this problem, please post your answer, i am very curious why this is happening
I have the usual gesture detector for detecting fling , It is an instance attribute of a SurfaceView
GestureDetector flingDetector = new GestureDetector(getContext(),new SimpleOnGestureListener() {
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Fling implementation
return true;
}
});
I am drawing a lot of complex stuff on a canvas and I have a translate(dx,dy) method that I use with onScroll.
So my question is how do I implement the fling using the translate method ?
There seem to be a lot of questions on detecting fling , my question is on implementing it .
I am not sure this will answer your question, I'll give it a try.
Check http://developer.android.com/reference/android/view/MotionEvent.html for MotionEvent.
You can use the two events received as e1 and e2 on the onFling method, and calculate coordinate differences with e1.getX(), e2.getX(), e1.getY(), e2.getY().... With this you would have the dx and dy to use with translate(dx,dy).
Since the fling seems more of a dynamic gesture, you could decide that fling means an ampler movement, and apply an amplification factor to dx and dy, so that when the user scrolls, they get a precise movement, but on fling, the actual movement gets amplified.
If this factor depends on velocity, you have a custom response for every user input.
(A different thing would be animating the result, which I guess would depend on other things).
An example I might try if it were me:
User scrolls softly: Movement is dx,dy. Translate(dx,dy).
User flings:
Real motion: dx=(e2.getX()-e1.getX(). dy = (e2.getY()-e1.getY().
Fling factor: (Custom implementation).
Modified motion: dxModified = dx*velocityX*F. dyModified = dy*velocityY*F.
Finally: translate (dxModified,dyModified)
Hope this helps to some extent.
Edit: I did not realize this question was from 2012, hopefully this will help someone some time. It would be nice to know about the final implementation anyway!.
What should I use if I want to have buttons on an image? I mean, being able to click on certain points of an image and see some info.
What if I want to display text on that points? Imagine I am having a map of my neighbor for example, and I want some info to be displayed (for example my home). And if someone press on my home he could see further details, like my name, my phone and so on. The image(aka the map) will be stable, it wont change, like a background.
You'll have to implement the method onTouch from the interface OnTouchListener. For example:
OnTouchListener listener = new OnTouchListener() {
#Override
public false onTouch(View v, MotionEvent event) {
if(!event.getAction() == MotionEvent.ACTION_DOWN)
return false; //If the touch event was not putting the finger down on the screen, return false(Actions may be move, up, and so on)
final float x = event.getX();
final float y = event.getY();
if(x and y are in some range)
doFirstThing();
else if(x and y are in another range..)
doSecondThing();
//... and so on...
//In the end, you must return a boolean saying whether you "consumed" the event - if you handled the event or not.
}
}
And don't forget to set the listener as the OnTouchListener for your ImageView!
imageView.setOnTouchListener(listener);
I believe for what you are trying to achieve you don't need to use buttons. Instead, create a custom view, in your onDraw method draw the images and handle the onTouch events as Jong suggested. Once you detected a click on your home for example, you could create another view with info about it and display it to user.
For info on how to create custom view you may refer to this queestion for example. Google around for it
How to get image coordinates at mouse hover position.
Please let me know
Thank you
Quoted from the "android-porting" mailing list (end of August 2010):
Android currently doesn't support mouse input, so has no concept of mouse hover.
You set an onTouchListener for the image, and in the onTouch event, you can pull the x,y coordinates out of the MotionEvent. getX and getY will get you the x and y coordinates in relation to the image, and getRawX and getRawY will get the x,y coordinates of the screen.
public boolean onTouch(View arg0, MotionEvent arg1) {
System.out.println("X: "+arg1.getX());
System.out.println("Y: "+arg1.getY());
System.out.println("Raw X: "+arg1.getRawX());
System.out.println("Raw Y: "+arg1.getRawY());
return true;
}
As stakx said, Android doesn't support the mouse. However, if you are referring to the image that currently has focus, try this:
Image focusedImg = getViewById(R.id.YourMainLayout).findFocus();
int[] relativeToParentPixels = { focusedImg.getLeft(), focusedImg.getTop(),
focusedImg.getRight(), focusedImg.getBottom() };
Now you'll have the boundary positions of the image in an array.
Try this
http://developer.android.com/reference/android/view/View.OnHoverListener.html
Available from api 14.