2016-09-01 10 views
1

イメージが左から右に移動するゲームを作りたいと思っています。クリックすると何か起こります。私はそれを動かしましたが、私がクリックすると何も起こりません。ここでは、コードは次のようになります。アニメーションの実行中にOnClickが動作しない

代わりにアニメーションの
package com.game.luc08.game; 

import android.content.res.Resources; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.TranslateAnimation; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class Game extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game); 

    final ImageView image= (ImageView) findViewById(R.id.image); 

    final TextView test = (TextView) findViewById(R.id.test); 

    int screenWidth = this.getResources().getDisplayMetrics().widthPixels; 

    image.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        test.setText("Clicked"); 
       } 
      } 
    ); 

    Animation animation = new TranslateAnimation(0, screenWidth, 0, 0); 
    animation.setDuration(5000); 
    animation.setFillAfter(true); 
    image.startAnimation(animation); 

} 

}

+0

あなたがこの仕事のためにOnTouchListenerを使用する必要が –

+0

ありがとうございました – 404response

答えて

1

あなたがViewPropertyAnimatorを試して、あなたはクリックを検出することができます:

image.animate().xBy(screenWidth).setDuration(5000).start(); 
関連する問題