Avi Kumar
Avi Kumar

Reputation: 4433

How to set finger touch functionality in android?

I am developing a small application in android in which i have few image in my application i want when user touch with one finger images can move left or right side and when user can touch with two fingers it could be zoom how can i do this please refer some tutorial code for me. here is my code and i used view flipper in xml v fdjf

public class Jaap extends Activity implements OnTouchListener{

float downXValue;
int counter = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

// Set main.XML as the layout for this Activity
setContentView(R.layout.jaap);

// Add these two lines
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this); 

// Add a few countries to the spinner

}

public boolean onTouch(View arg0, MotionEvent arg1) {

// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}

case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX(); 

// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 131){
vf.showNext();
counter++;
}
}
break;
}
}

// if you return false, these actions will not be recorded
return true;
}
}

Upvotes: 2

Views: 3048

Answers (3)

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53647

getPointerCount() of motionEvent tells you how many fingers are touch. In gives number of touch

Thanks

Upvotes: 1

iLikeAndroid
iLikeAndroid

Reputation: 1106

Handle the MotionEvent in the onTouchEvent(MotionEvent) of the Activity. In this check for the MotionEvent.getAction().

switch(MotionEvent.GetAction()) {
case ACTION_DOWN:
//handle the finger down functionality here
break;

case ACTION_POINTER_DOWN:
//handle the second finger down functionality here
break;
}

A sequence of events would be issued, mostly with the actions as follows:

  • ACTION_DOWN - one finger touch down
  • ACTION_MOVE -> the finger is moved
  • ACTION_UP - one finger touch is removed
  • ACTION_POINTER_DOWN - second finger touch down
  • ACTION_POINTER_UP - second finger touch up

You will have to check the X,Y positions in the event and determine what should be donw... will see if there are any good tutorials/samples to explain these better...

Upvotes: 1

iDroid
iDroid

Reputation: 10533

Check out this step-by-step tutorial, it contains code examples how to zoom in/out a picture and move it around.

Upvotes: 3

Related Questions