How can I drag a imagebutton in android - android

I'm using ImageButton's in my application. By using onTouch event and onTouchListener I'm responding to the touch events.
Once the touch event was completed I'm just displaying the ImageButton at target location.
Instead I wanted it to look like dragging of ImageButton. Can anyone point me in the right direction.

This blog has two very nice examples of how you can use touch events to move any kind of view around the screen.

I'm not sure I understand what the problem is. You wrote "Once the touch event was completed [...]" - does that mean you've got it to work in one situation but not others? Are you considering that the touch event is completed when the action of the MotionEvent is MotionEvent.ACTION_UP? If you've got that part working, then why not just re-use that same code for the action MotionEvent.ACTION_MOVE?

Define a own view wich is extending imagebutton, define a drawable for "active" state and set (or reset) the drawable onclick

Related

Android : How to detect onTouch event on a view, with touch down outside the view?

In an Android fragment, I have a GridLayout with views in it.
I would like to be able to detect when a user swipes over a view, even if he initially touches down outside of it. I've included a diagram to illustrate what I mean here :
Is there a simple way to do this (without using coordinates to see if the touch point is in the view)?
Thanks!
You solve this using View.OnTouchListeners.
Set OnTouchListeners both root view as well as your view.
Check for Events and actions ACTION_DOWN(touch event start),ACTION_UP(touch event over)
Set flags as per need for inside touch or outside touch.
basically you need capture events as well as points too.

blinking in custom drag/tap implementation

I have implemented custom view with some children. The view can be scrolled using standard drag gesture. Also every child can be clicked. The problem is, that when I start dragging the view, one of children gets 'down' event and it changes its state to 'pressed' for a second. I would prefer standard listview behavior - the child goes into pressed state when the user keeps pressing this child with his/her finger for like 50ms. It would reduce blinking caused by misread press event.
I know, that I need at least 2 events to detect if the user is tapping or dragging the view. For now I'm using TimerTask to shedule 'down' event. When I get 'move' event before my 'down' event is executed, I know that the user is dragging and I can cancel the sheduled event.
I know it's quite hacky. I also tried gesturedetector to detect drag and tap events, but it needs some additional work to properly implement changing view state from pressed to default when the user moves finger and starts to drag the view.
My question is - how this is implemented in android listview? I tried to copy their solution from listview implementation, but it's so huge I can't handle it. Simply I don't see the code responsible for handling such situation.
I managed to understand a gesture detection logic in ListView and, in general, in android views. I wrote my own gesture detector, which is somewhat better than the original one. It reports more gestures (multiple taps, dragging) and has some configurables (timeouts, move epsilon). You can find it open-sourced here: Better Gesture Detector on code.google
The library uses Handler class and postDelayed()/removeCallbacks() method combination to detect, handle and cancel motion events and gestures. It's quite simple and one should be able to get the idea by just reading the code.
This repository also contains a simple demo. Please note that this code is provided 'as is', contains some useless comments, logs and should be cleaned up a bit.

Android widget slide animation

I need to create a custom toggle-silder widget, like in the iphone, things like on-off items etc...
For the task i chose to inherit from LinearLayout, define it as Vertical and grab some xml attributes like the drawables that will be using for the buttons (one for on, one for off) and i just place two Views with the drawables as their backgrounds.
Now the issues i'm facing are:
i can catch onKeyDown and
onTouchEvent to discover swipe and
right-left-fire events so i'll
switch states for the button, but i
do wonder if there is a swipe event
read in the android system onSwipe
or should i try messing with the
GestureDetector ?
i want the sliding to be animated, i have no idea how to do that, any ideas where to start looking ?
ok if we take a look on this page:
http://developer.android.com/guide/topics/graphics/view-animation.html
we can see that we have simple way to create the animation, once onClick or onTouchStart is discovered (if not already processing), start the animation preloaded from the xml and disable touch on the View until animation ends(you cann add animation listener to animation object).
you should make sure fillAfter is true so the values will stick after animation ends.
the only issue with it is that it doesn't allow dragging like in iphone, possible solution would be to allow dragging up to certain delta and if that delta is passed ignore any onTouchMove events, disable touch and start the animation.
ofcourse deltas and animations should be updated according to the state of the button.
i might post code soon.

Starting on gesture events handling

I have been new to android. I have just gone through my slideshow app.
I want to make image slide using touch event just like facebook app. when we click on image and swipe it to left then new image shown and when swipe finger towards right then last old image is shown.
I don't know how this works and how to start with it. Can any one help me in this case to start with like providing tutorials and tips ?
I just want effect on the image.
Handling simple touch events works the following:
You need an OnTouchListener in your Activity. Add it by implementing the OnTouchListener interface.
You will have to override the onTouch(View v, MotionEvent event) method. This will be called if a touch gesture is recognized.
Now you get all the information about the Touch (in Android it is called MotionEvent) in the onTouch Method via the MotionEvent. Look at the MotionEvent-Class documentation to use the correct methods to handle your swipe gesture.
You could first ask what action really happened. Call getAction(). It will return the type of action. The easiest way would be to react to ACTION_MOVE.
Then ask for the origin of the touch event and the later position of the touch by using getHistoricalX(int)/getHistoricalY(int) and getX(int) and getY(int) (attention: these methods are only usable for ACTION_MOVE events - in your case it is ok).
Now as you have the start and end coords of the swipe gesture, you can calculate the length of the swipe in pixels and the direction of the swipe and that is all what you need.
Good Luck!
There's two bits of information that you might want. The first thing is, what you are referring to is called a gallery. There's several great examples of how to use a gallery, the basic idea is you get a BaseAdapter Class that gets the view and attach it to the gallery. See this tutorial for an example, or just google Android Gallery Example, you'll find alot of examples.
The second is gesture programming. Take a look at this tutorial for more information

Android "hover" event in custom layout

I have a custom layout that I have written that basically just displays a bunch of ImageViews. I am handing onTouch events for all the ImageViews in my layout.
However, if a user touches one imageView and then drags over another ImageView, I would like to be able to handle that as well.
How would I go about capturing this behaviour?
maybe this example is useful: http://www.anddev.org/viewtopic.php?p=11603
it relies on tracking the event with MotionEvent.ACTION_MOVE. the coordinates could be checked to determine when the element enters/exits a cell.
it uses drawview, but hopefully the same can be done w/imageview

Categories

Resources