How to implement custom gestures by Android programming _android

Source: Internet
Author: User
Tags event listener

An example of this article describes the Android programming approach to implementing custom gestures. Share to everyone for your reference, specific as follows:

I've described how to use gestures in an Android program, mainly the system's default gestures, this time about how to customize gestures and how to manage them.

To introduce the Android system to the management of gestures, the Android system allows the application to put the user's gestures in the form of files before the use of these gestures only need to load the gesture library file, while the Android system also provides such as gesture recognition, search and delete function interface , specifically as follows:

One, loading gesture library file:

Staticgesturelibrary fromfile (String path);
Staticgesturelibrary fromfile (File path);
Staticgesturelibrary fromprivatefile (context, String name);//load gesture Library from name file in the specified application's Data folder
Staticgesturelibrary Fromrawresource (context context, int ResourceID);

Second, the management gesture library:

Voidaddgesture (String entryname, gesture gesture);//Add a gesture entryname named Set<string>getgestureentries
(); /Gets the name Arraylist<gesture>getgestures (String entryname) of all gestures in the gesture Library;//
gets all the gestures that correspond to the EntryName name
ArrayList <prediction>recognize (gesture gesture);//To identify all gestures
voidremoveentry (String entryname) that match gesture from the current gesture library;/ Deletes the entryname corresponding gesture
voidremovegesture (String entryname, gesture gesture) in the gesture Library;//delete gestures from entryname, gesture
in the gesture library Booleansave ()//gesture library file has changed to call this method to save the gesture library

Next, on how to customize gestures, Android provides a component named Gestureoverlayview designed to draw custom gestures and provide Ongesturelistener, Ongestureperformedlistener , Ongesturinglistener three listening interfaces, which are used in response to the start, end, completion, cancellation of gesture events, and in general, Ongestureperformedlistener are the most commonly used, and can be used to provide responses when gesture events are completed. The following is an example of how to customize gestures by a program instance.

The layout files used are as follows:

First, Main.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent"
  android:gravity= "Center_horizontal"
  >
<textview
    android:layout_ Width= "Fill_parent"
    android:layout_height= "wrap_content"
  android:text= "please draw gestures on the screen below"
/>
<android.gesture.gestureoverlayview
    android:id= "@+id/gesture"
    android:layout_width= "Fill_parent"
    android:layout_height= "Fill_parent"
    <!--This parameter specifies whether to complete a single stroke-->
android: Gesturestroketype= "multiple"
/>
</LinearLayout>

Second, Save.xml:

<?xmlversionxmlversion= "1.0" encoding= "Utf-8"?> <linearlayoutxmlns:androidlinearlayoutxmlns:android= "
    Http://schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" fill_parent " android:layout_height= "Fill_parent" > <linearlayout android:orientation= "Horizontal" Android:layout_wid Th= "Fill_parent" android:layout_height= "wrap_content" > <textview android:layout_width= "Wrap_conten" T "android:layout_height=" wrap_content "android:layout_marginright=" 8dip "android:text=" @ String/gesture_name "/> <!--defines a text box to allow the user to enter a gesture name--> <edittext android:id=" @+id/gesture_name "Android:layout_width=" fill_parent "android:layout_height=" wrap_content "/> </linearlayout&gt
; <!--define a picture frame to display gestures--> <imageview android:id= "@+id/show" android:layout_width= "128DP" Android : layout_height= "128DP" android:layout_margintop= "10DP"/> </LinearLayout>

 

Java code is as follows:

public class Addgesture extends activity {edittext edittext;
  Gestureoverlayview Gestureview;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    EditText = (edittext) Findviewbyid (r.id.gesture_name);
    Gestureview = (Gestureoverlayview) Findviewbyid (r.id.gesture);
    Sets the drawing color of the gesture Gestureview.setgesturecolor (color.red);
    Set the drawing width of the gesture gestureview.setgesturestrokewidth (4);
        The event-binding event listener Gestureview.addongestureperformedlistener (new Ongestureperformedlistener () {gesture for gesture completion)
        @Override public void ongestureperformed (Gestureoverlayview overlay, final gesture gesture) {//Load Save.xml Interface Layout representative View Savedialog = Getlayoutinflater (). Inflate (R.layout.save, Nu
          ll);
          Get the show component in Savedialog imageview ImageView = (imageview) savedialog. Findviewbyid (R.id.show); // Gets the Gesture_name component in Savedialog final edittext gesturename = (edittext) savedialog. Findviewbyid (r.id.
          Gesture_name);
          Create a bitmap based on the gestures contained in gesture Bitmap Bitmap = gesture.tobitmap (128, 128, 0xffff0000);
          Imageview.setimagebitmap (bitmap); Use the dialog box to display the Savedialog component new Alertdialog.builder (Addgesture.this). Setview (Savedialog). Set Positivebutton ("Save", new Onclicklistener () {@Override public void OnClick (Dialogi  Nterface dialog, int which) {//Get the gesture library corresponding to the specified file Gesturelibrary
                Gesturelib = gesturelibraries. FromFile ("/mnt/sdcard/mygestures");
                Add Gesture Gesturelib.addgesture (Gesturename.gettext (). ToString (), gesture);
              Save gesture Library Gesturelib.save (); }). Setnegativebutton ("Cancel",NULL). Show ();
  }
      });

 }
}

Run the above program to complete the addition of custom gestures, the results of the operation as shown in the figure:

When you need to use this gesture, just load the corresponding gesture library and call the recognition function interface given above to identify it, no longer detailed here, the above content learning from the Crazy Android book

More interested readers of Android related content can view this site: "Android scroll bar and Rolling tips summary", "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", " Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls

I hope this article will help you with the Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.