Wednesday 30 October 2013

Gesture Example

Here i am going to explain you working with Gesture and storing Gesture in SD Card.   

Example:










Code:

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Create Your Gesture"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <android.gesture.GestureOverlayView
        android:id="@+id/gestureOverlayView1"
        android:layout_width="match_parent"
        android:layout_height="278dp"
        android:layout_weight="0.60"
        android:eventsInterceptionEnabled="true"
        android:gestureColor="#FF0000"
        android:gestureStrokeLengthThreshold="0.1"
        android:gestureStrokeType="multiple" >

    </android.gesture.GestureOverlayView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Save Gesture in SD Card"
        android:textStyle="bold" />


</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.gestureexample;

import java.io.File;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;

public class MainActivity extends Activity {

GestureOverlayView gestureView;
Bitmap bmimg;
Button save;
File f;
FileOutputStream fos;

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

gestureView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
save = (Button)findViewById(R.id.button1);

save.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
gestureView.setDrawingCacheEnabled(true);
       bmimg = Bitmap.createBitmap(gestureView.getDrawingCache());
       
       try{
       
       f = new File(Environment.getExternalStorageDirectory()+ File.separator + "gesture.png");
           f.createNewFile();
           fos = new FileOutputStream(f);
           fos = new FileOutputStream(f);
           bmimg.compress(Bitmap.CompressFormat.PNG, 100, fos);
       fos.close();
       
       Toast.makeText(MainActivity.this, "Gesture Created in SD Card", Toast.LENGTH_SHORT).show();
           
       }catch(Exception e){
        Log.v("Gestures", e.getMessage());
       e.printStackTrace();
       }
}
});

}


}

Manifest

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment