Thursday, October 9, 2014

Basic of Gesture

how to use gesture in Android
---------------------------------

Some examples of common multi-touch gestures and actions you might use include:
ØPinch to zoom in, spread to zoom out.ØBasic dragging in order to move, adjust, scroll, and position.ØFlick to jump to the next screen or scroll extra fast.ØTap and hold to open an item or context menu.ØMulti-finger drag often scrolls faster!
ØHandling multi touch gestureØDetecting common gestureØManaging touch eventØAnimating a scroll gestureØTracking movementØDragging & scalling
ØAndroid provides special types of touch screen events such as pinch , double tap, scrolls , long presses and flinch. These are all known as gestures.
ØAndroid provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not.
ØTo use it , you need to create an object of GestureDetector and then extend another class with GestureDetector.SimpleOnGestureListener to act as a listener and override some methods.

Different methods in gesture :-
-----------------------------------------------
GestureDetector myG;
myG = new GestureDetector(this,new Gesture());

   class Gesture extends GestureDetector.SimpleOnGestureListener{
   public boolean onSingleTapUp(MotionEvent ev) {
   }
   public void onLongPress(MotionEvent ev) {
   } 
   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
   }
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
   }
}
}



Gesture.java

---------------------

package com.example.gesture;

import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

public class MainActivity extends Activity {

  private ImageView img;
  private Matrix matrix = new Matrix();
  private float scale = 1f;
  private ScaleGestureDetector SGD;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     img = (ImageView)findViewById(R.id.imageView1);
     SGD = new ScaleGestureDetector(this,new ScaleListener());
  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {
     SGD.onTouchEvent(ev);
     return true;
  }

  private class ScaleListener extends ScaleGestureDetector.
  SimpleOnScaleGestureListener {
  @Override
  public boolean onScale(ScaleGestureDetector detector) {
     scale *= detector.getScaleFactor();
     scale = Math.max(0.1f, Math.min(scale, 5.0f));
     matrix.setScale(scale, scale);
     img.setImageMatrix(matrix);
     return true;
  }
}

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.main, menu);
     return true;
  }

}

activity_main.xml
------------------------


<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"
    tools:context=".MainActivity" >

 

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="@string/hello_world" />

     <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/textview1"
        android:scaleType="matrix"
        android:src="@android:drawable/sym_def_app_icon" />
</RelativeLayout>


AndroidMenifest.xml
-----------------------------


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gesture"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name="com.example.gesture.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>


</manifest>



string.xml
--------------

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Gesture</string>
    <string name="hello_world">Pinch to zoom in or out!</string>
    <string name="menu_settings">Settings</string>

</resources>