Tuesday 6 October 2015

Gesture Detection in Android Part-2

In the previous post, I have explained briefly how to use the Gesture Detection in api in android. If you want to  have a look, then please go the this link.


In this post, we will run a live example. In this example, there is an imageview inside the main layout. when you scroll fast or fling the imageview, image will changed whenever speed of scroll exceeds the value mentioned inside the application.


fling.png
MainActivity.java

package test.example.gestureflingactivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

@SuppressLint("ClickableViewAccessibility")
public class MainActivity extends Activity {

private ImageView imageView;
private GestureDetector mDetector;
int imgId=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.mic_red);
mDetector = new GestureDetector(this, new GestureDetection());
imageView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}
});

}

class GestureDetection extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onDown(MotionEvent event) {
// Log.d(TAG, "onDown: " + event.toString());
// it must be return true to get any gesture.
return true;
}

@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {

if (velocityX > 200) {

if(imgId==0){
imageView.setImageResource(R.drawable.micro_black);
imgId=1;
}else{
imageView.setImageResource(R.drawable.mic_red);
imgId=0;
}
}
return false;
}

}
}


activity_main


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="test.example.gestureflingactivity.MainActivity" >

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Fling Gesture Test"
       android:textSize="20sp"
       android:textStyle="bold"
       android:typeface="serif" />

   <ImageView
       android:id="@+id/imageView1"
       android:layout_width="200dp"
       android:layout_height="200dp"
       android:layout_below="@+id/textView1"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="75dp"
       android:src="@drawable/ic_launcher" />

   <TextView
       android:id="@+id/textView2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/imageView1"
       android:layout_alignParentBottom="true"
       android:layout_marginBottom="15dp"
       android:layout_marginLeft="18dp"
       android:text="Fast Scroll The Image"
       android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

No comments:

Post a Comment