tags:

views:

1513

answers:

3

How can i make my page slide as the user slides finger on the screen in android? Any example code?

I just require the same feel as it is on my android g-phone's home screen.The screen moves as the finger moves(also includes the elastic effect).

+7  A: 

Check out this tutorial and its follow up on warriorpoint. They explain how to use the ViewFlipper to smoothly animate the transition from one screen to another, and then in the second part how to do this using touch control. Note, these are whole-screen transitions not panning around on an existing page. For panning, e.g. on an oversized image that doesn't fit in the screen, check out Android BigImage. Depending on what you're trying to do these might be overkill, but it's not 100% clear what you're trying to achieve.

Steve H
These 2 methods are not recognized because animations are not provided perhaps.vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));Also when i comment these 2 lines, and run the code...i just get the switching effect and not the sliding one. Please explain
Maxood
Did you follow step 4 and import the animations from ApiDemos? And you just get the switching effect if you comment out those two lines because those two lines are what tells it to use the sliding effect...
Steve H
Fine. I did that and got the sliding affect but am not able to hold the page with my finger as one can do it on the home touch screen.How am i able to hold the page while dragging/sliding with my finger?As one can hold it with one's finger as when the page is half way through.Thanks
Maxood
That's probably a bit more complicated to do. You could try combining the techniques of the two tutorials: use the BigImage technique to allow the user to scroll around on a screen, but if once they have moved past a certain point (i.e. moved it 70% off the screen), then assume that's a page change and use ViewFlipper. I'm not sure that would work though... You'll have to experiment or wait for another answer!
Steve H
How can i calculate that a screen/page has moved past a certain point (i.e. moved it 70% off the screen)?
Maxood
Maxood: You'll have to handle touch event - DOWN and MOVE actions and note the deltas / changes. Get the window size and calculate the %age of movement.
MasterGaurav
+1  A: 

use gallery view

Kantesh
Are you sure gallery view would work? Would i be able to hold the page half way through with my finger?
Maxood
A: 

You will need Gesture detector - the classes / interfaces under considerations are:

 1. android.view.GestureDetector
 2. android.view.GestureDetector.SimpleOnGestureListener

Method of interest is onFling.

Now, based on the X, Y and the velocity in respective directions, you can "draw your view" / "reposition the nested views in case of activity or view-group" at corresponding location.

An example of how to redraw can be found in any game-example like LunarLander in Android at http://developer.android.com/resources/samples/LunarLander/src/com/example/android/lunarlander/LunarView.html

MasterGaurav