2012-04-29 13 views
1

xmlファイルにImageViewがあり、クリックしたときに画像を回転します。ImageViewは画像をクリックした後はトリガーしませんが、TextViewをクリックするとトリガーされます

私はこのアーカイブする次のコードを使用します。

@Override 
    public boolean onTouchEvent(MotionEvent event) { 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      img = (ImageView) findViewById(R.id.imageView1); 
      Animation an = new RotateAnimation(0.0f, 360.0f, img.getWidth()/2, 
        img.getHeight()/2); 
      an.reset(); 
      // Set the animation's parameters 
      an.setDuration(1000); // duration in ms 
      an.setRepeatCount(0); // -1 = infinite repeated 
      an.setRepeatMode(Animation.REVERSE); // reverses each repeat 
      an.setFillAfter(true); // keep rotation after animation 
      //an.start(); 
      img.setAnimation(an); 


     } 
     return true; 
    } 

をしかし、TEの問題は、私は何も起こりません画像に押すと、画像が入らない、です。しかし、画像をクリックしてTextViewをクリックすると、画像は回転します。

これは非常にランダムです。私が間違っているのは何

?どうすればこの問題を解決できますか?

ありがとうございました。

答えて

0

まあ、あなたの活動全体に対してonTouchEvent関数が呼び出されているようです。したがって、アクティビティウィンドウ内のビューによって「消費されない」タッチ操作があれば、この機能がトリガされます。したがって、あなたのTextViewのようなあなたのアクティビティのどこかに触れて、このイメージの回転を引き起こすのは理にかなっています。

あなたのコードを見るのが一番いいと思うのですが、全体のアクティビティではなく、ImageView自体にtouch/clickイベントリスナーを実装するのが最善でしょう。

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    /*YOUR CUSTOM CODE AT ACTIVITY CREATION HERE*/ 

    /*here we implement a click listener for your ImageView*/ 
    final ImageView img = (ImageView)findViewById(R.id.imageView1); 
    img.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      Animation an = new RotateAnimation(0.0f, 360.0f, img.getWidth()/2, img.getHeight()/2); 
      an.reset(); 
      /* Set the animation's parameters*/ 
      an.setDuration(1000); // duration in ms 
      an.setRepeatCount(0); // -1 = infinite repeated 
      an.setRepeatMode(Animation.REVERSE); // reverses each repeat 
      an.setFillAfter(true); // keep rotation after animation 
      //an.start(); 
      img.setAnimation(an); 
      img.invalidate(); //IMPORTANT: force image refresh 

     } 
    }); 
} 
+0

注: 'IMGが含まれている場合、あなたのonTouchコードが働いているだろうし。あなたの関数の最後に 'invalidate()'を呼びますが、あなたの 'TextView'をクリックしてもアニメーションを開始しても問題は解決しません。 – epichorns

+0

あなたの答えをありがとうが、それはそれを解決しませんでした。私はまだ画像をクリックしてアニメーションをトリガすることはできず、TextViewをクリックすると画像がトリガされます。 – MeesterPatat

+0

私はちょうど私のコードベースでそれをテストし、うまくいきました。上記のようにimg.setOnClickListenerを使用しましたか?アニメーションを設定した直後に無効化を呼び出しましたか? – epichorns

0

私は二epichomsの勧告(OnClickListenerを使用)になります。ここではこれを行うコードスニペットです。また、あなたのImageViewのは、クリックを受信できることを確認してください。

final ImageView img = (ImageView)findViewById(R.id.imageView1); 
img.setClickable(true); 
img.setFocusable(true); 
img.setOnClickListener(new View.OnClickListener(){ 
    ... 

あなたは同様にあなたのXMLレイアウトでこれらの値を設定することができます。

android:clickable="true" 
android:focusable="true" 
関連する問題