认识Android手势

时间:2012-05-10 21:32:48

标签: java android

您好我正在构建一个应用程序,用户在屏幕上绘制手势然后比较图像视图中的图像是否与绘制的手势相同并显示完成的消息或现在再试一次我的问题是每当一个图像出现,用户绘制的东西不同于它在图像中显示的内容,它说得好我怎样才能解决这个问题?...无论如何这里是代码

package draw.letters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;

public class DrawlettersActivity extends Activity implements OnGesturePerformedListener {
    private SoundPool soundPool;
    private HashMap<Integer, Integer> soundsMap;
    private GestureLibrary gestureLib;
    int soundOk=1;
    int soundNot=2;

    ImageView ShowImage;

    public Integer[] showLetters={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,
        R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.i
       ,R.drawable.j,R.drawable.k,R.drawable.l,R.drawable.m,R.drawable.n,
R.drawable.o,R.drawable.p,R.drawable.q,R.drawable.r,R.drawable.s,R.drawable.t
       ,R.drawable.u,R.drawable.v,R.drawable.w,R.drawable.x,R.drawable.y,R.drawable.z};

    Random r = new Random();
    public int index = 0;
    public String idGesture;

/** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundsMap = new HashMap<Integer, Integer>();
        soundsMap.put(soundOk, soundPool.load(this, R.raw.htc_notify_weight, 1));
        soundsMap.put(soundNot,soundPool.load(this, R.raw.buzzybuz, 1));
        gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);

        //Loads the gesture library
        if (!gestureLib.load()) 
        {
            finish();
        }
        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
        gestures.addOnGesturePerformedListener(this);
    }//end of OnCreate() 

    //Method to create the sounds

    public void playSound(int sound) {
        AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = streamVolumeCurrent / streamVolumeMax; 
        soundPool.play(soundsMap.get(sound), volume, volume, 1, 0, 1);
    }

    public String idImage (ImageView compara) {
    String letterGesture = null;
    switch(compara.getId()) {
    case R.drawable.a:
        letterGesture="A";
    case R.drawable.b:
        letterGesture="B";
    case R.drawable.c:
        letterGesture="C";  
    }
    return letterGesture;
    }

    public void malEscrito() {
    Toast.makeText(this, "Wrong Try Again", Toast.LENGTH_SHORT).show();
    playSound(soundNot);
    }


    public void onGesturePerformed(GestureOverlayView overlay, Gesture gestures) {
    //Calls the image to be displayed
        index= r.nextInt(26);
        ShowImage = (ImageView) findViewById(R.id.imageView1);
        ShowImage.setImageResource(showLetters[index]);
        idGesture=idImage(ShowImage);

    ArrayList<Prediction> predictions = gestureLib.recognize(gestures);
    if (predictions.size() > 0 && predictions.get(0).score > 1.0)  
        {  
            String prediction = predictions.get(0).name; 
            //compares the letters with the name of the gesture  
    if ("A".equals(prediction)) {  
                Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                //index = r.nextInt(3);
                ShowImage.setImageResource(showLetters[index]);
                playSound(soundOk);
            } else if ("B".equals(prediction)) {  
                Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                //index = r.nextInt(3);
                ShowImage.setImageResource(showLetters[index]);
                playSound(soundOk);
            } else if ("C".equals(prediction)) {  
                Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                //index = r.nextInt(3);
                ShowImage.setImageResource(showLetters[index]);
                playSound(soundOk);
            } else if ("D".equals(prediction)) {  
                Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                //index = r.nextInt(3);
                ShowImage.setImageResource(showLetters[index]);
                playSound(soundOk);
            } else if ("E".equals(prediction)) {  
                Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                //index = r.nextInt(3);
                ShowImage.setImageResource(showLetters[index]);
                playSound(soundOk);
            } else if ("F".equals(prediction)) {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("G".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("H".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("I".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("J".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("K".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("L".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("M".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("N".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
              else if ("~n".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("O".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("P".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("Q".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("R".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("S".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("T".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("U".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("V".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("W".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("X".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }
             else if ("Y".equals(prediction))  
             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }  else if ("Z".equals(prediction))  

             {  
                 Toast.makeText(this,"Well Done",Toast.LENGTH_SHORT).show();
                 //index = r.nextInt(3);
                 ShowImage.setImageResource(showLetters[index]);
                 playSound(soundOk);
             }

             else 
                {
                 malEscrito();
                }

           }
         }

}

我有些你不太明白的事情,请事先告诉我们!

1 个答案:

答案 0 :(得分:0)

我非常肯定说"I".equals(prediction)是不正确的,因为你在一个完全独立的对象类型android.gesture.Prediction上调用字符串等于。

我挖了一些代码,我写了一段关于这个历法的日历,并且能够拉出这个:

    public void onGesturePerformed(GestureOverlayView overlay,
            Gesture gesture) {
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

        // We want at least one prediction
        if (predictions.size() > 0) {
            Prediction prediction = predictions.get(0);
            // We want at least some confidence in the result
            if (prediction.score > 1.0) {
                if (prediction.name.equals("gest_right")) {
                    // Toast.makeText(this, "previous month",
                    // Toast.LENGTH_SHORT).show();

                    userMonth.set(Calendar.DAY_OF_MONTH, 1);
                    userMonth.add(Calendar.DATE, -1);

                    // goingForward = false;
                } else {
                    // Toast.makeText(this, "next month",
                    // Toast.LENGTH_SHORT).show();

                    userMonth.set(Calendar.DAY_OF_MONTH, 1);
                    userMonth.add(Calendar.DATE, userMonth
                            .getActualMaximum(Calendar.DAY_OF_MONTH));

                    // goingForward = true;
                }
                updateCalendarUI();
            }
        }

我实际上使用谷歌的一个示例应用来创建手势库,我将其加载到mLibrary中;我希望这在某种程度上有所帮助......