1

質問を見ていただきありがとうございます。私はいくつかのアンドロイドプログラミングを試していますが、壁にぶつかりました。それを解決する方法がわからない。タッチ カウントが奇数の場合にのみ、特定のエンティティのアニメーションを有効にしようとしています。それはtouchCount%2 != 0です。

public boolean onTouch(View v, MotionEvent event){

    ArrayList<TextView> textToDance = new ArrayList<TextView>();
    textToDance.add((TextView)findViewById(R.id.CAD5));
    textToDance.add((TextView)findViewById(R.id.CAD10));
    textToDance.add((TextView)findViewById(R.id.CAD20));
    textToDance.add((TextView)findViewById(R.id.CAD50));
    textToDance.add((TextView)findViewById(R.id.CAD100));

    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            for(TextView txtAnimate: textToDance){

                if(event.getRawX()<= txtAnimate.getX()+txtAnimate.getMeasuredWidth() && event.getRawX()>=txtAnimate.getX()){
                    if(event.getRawY()<= txtAnimate.getY()+105+txtAnimate.getMeasuredHeight() && event.getRawY()>=txtAnimate.getY()+105){
                        helpAnimate(txtAnimate, 0);
                    }
                }

            }

        break;

        case MotionEvent.ACTION_MOVE:
            Log.d("MOVE","MOVE");
        break;

        case MotionEvent.ACTION_UP:
            Log.d("UP","UP");
        break;

        default:
            break;
    }
    return true;
}

HashMap を実装しようとしましたが、onTouch が呼び出されたすべてのマップがリセットされます。助言がありますか?

4

1 に答える 1

0

まず、メソッドの外で配列を初期化しますonTouch

ArrayList<TextView> textToDance = new ArrayList<TextView>();
textToDance.add((TextView)findViewById(R.id.CAD5));
textToDance.add((TextView)findViewById(R.id.CAD10));
textToDance.add((TextView)findViewById(R.id.CAD20));
textToDance.add((TextView)findViewById(R.id.CAD50));
textToDance.add((TextView)findViewById(R.id.CAD100));

次に、マップを作成し、それをゼロカウント値で初期化し、onTouchListener を登録できます (それがあなたのものであると仮定しましょうActivity) 。

HashMap<TextView,Integer> myMap = new HashMap<TextView,Integer>();
for (TextView tv : textToDance){
    tv.setOnTouchListener(this);
    myMap.put(tv,0);

} 

次に、onTouch次のようなものがあります...

public boolean onTouch(View v, MotionEvent event){
    switch (event.getAction()){
       case MotionEvent.ACTION_DOWN:
          if (myMap.contains(v)){
              myMap.put(v, myMap.get(v) + 1;
          }
       break;

これはおそらく最適な方法ではありませんが、やりたいことの達成を開始するには十分なはずです。

于 2012-05-10T21:30:54.893 に答える