0

嗨我正在构建一个应用程序,其中用户在屏幕上绘制一个手势,然后比较 imageview 中的图像是否与绘制的手势相同并显示一条消息,如做得好或现在重试我的问题是每当图像出现并且用户绘制的内容与它在图像中显示的内容不同,它说做得很好我该如何解决这个问题?...无论如何这里是代码

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();
                }

           }
         }

}

我有一些你不太明白的事情,请提前告诉我谢谢!

4

1 回答 1

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 中的手势库;我希望这在某种程度上有所帮助......

于 2012-05-11T23:04:19.240 回答