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)完全に別のオブジェクト type で文字列 equals を呼び出しているため、この言い方が間違っていると確信しています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();
            }
        }

私は実際にGoogleのサンプルアプリの1つを使用してジェスチャーライブラリを作成し、それをmLibraryにロードしました。これが何らかの形で役立つことを願っています...

于 2012-05-11T23:04:19.240 に答える