Tuesday 17 September 2013

Gallery View using Gesture Detector on touch event

Implementing gallery view for touch event using gesture detector and gesture listener.


In all Application development you compulsory need a gallery view. And for smart Phone you need touch gallery change image. so here is an best example to implement the gallery view.

Copy the code in your code.

1.MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnTouchListener {

ImageView im;
int i = 0;
int images[] = { R.drawable.img1, R.drawable.img2, R.drawable.img3,
R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7,
R.drawable.img8, R.drawable.img9 };
private final GestureDetector gestureDetector = new GestureDetector(
new GestureListener());

public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView) findViewById(R.id.image_place_holder);
im.setOnTouchListener(this);
}

private final class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD
&& Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
i--;
if (i < 0)
i = images.length;
im.setImageResource(images[i]);
} else {
i++;
if (i > images.length)
i = 0;
im.setImageResource(images[i]);
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
}

2. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_place_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="10dip"
        android:gravity="center" 
        android:src="@drawable/img1"/>

</LinearLayout>

3.save all your images in drawables, and edit the names in MainActivity.java in int images[];

4. That's it.

Thank you 

No comments:

Post a Comment