Friday, 25 October 2013

Gesture Application

Gesture Application

Gesture allows users to interact with our  application with screen objects we provide.
Some Core Gesture Set that support by Android.
  1. Touch
  2. Long Press 
  3. Swipe
  4. Drag
  5. Double Touch
  6. Pinch open
  7. pinch close
Gestures are defined by a binary resources which can be created with an example program from the Android SDK. In your activity you can load Gestures via GestureLib.fromRawResource(). If a gesture is detected then the method "onGesturePerformedListener" is called. For this the activity must implement the interface "OnGesturePerformedListener" and must register itself at the GestureOverlayView with the method "addOnGesturePerformedListener()".

Now I am going to show very simple example of Gesture by this you can take your signature through application and store as a image.


Simple Example:

Create an Android Application project named GestureExample.


step1: Code for layout xml file: 



    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >


   
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.40"
        android:orientation="vertical" >

       
            android:id="@+id/gestureOverlayView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           android:eventsInterceptionEnabled="true"
            android:gestureStrokeLengthThreshold="0.1"
            android:gestureStrokeType="multiple" >
       

   

   
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.40"
        android:orientation="vertical" >

       
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />
   

   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

       
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Done"
             android:onClick="saveSig" />
   

step 2 : code for Activity source java file.


package in.androidshivendra.gestureexample;

import java.io.File;
import java.io.FileOutputStream;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class GetSign extends Activity {
ImageView img;


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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.get_sign, menu);
return true;
}
  public void saveSig(View view) {
       try {
           GestureOverlayView gestureView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
           gestureView.setDrawingCacheEnabled(true);
          Bitmap bm = Bitmap.createBitmap(gestureView.getDrawingCache());
          img  =(ImageView)findViewById(R.id.imageView1);
          img.setImageBitmap(bm);
          File f = new File(Environment.getExternalStorageDirectory()
                   + File.separator + "signature1.png");
           f.createNewFile();
           FileOutputStream os = new FileOutputStream(f);
          os = new FileOutputStream(f);
          
          //compress to specified format (PNG), quality - which is ignored for PNG, and out stream
         bm.compress(Bitmap.CompressFormat.PNG, 100, os);
         os.close();
         
     } catch (Exception e) {
          Log.v("Gestures", e.getMessage());
          e.printStackTrace();
      }
  }

}

Step 3: you have to add android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE  permission in manifest file by this way.


uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

step4: Now your application is ready for run. Just Test it.






Note : you can find your saved siganture image here.
click on Windows-> Show Views -> other->Android->file explorer .
within file explorer pane.
click on mnt->sdcard-> your sign saved.


Download Complete Example Code Here


No comments:

Today's Pageviews