2016-09-01 6 views
1

Ich möchte ein Spiel machen, wo ein Bild von links nach rechts bewegt und wenn Sie darauf klicken passiert etwas. Ich habe es in Bewegung gebracht, aber wenn ich darauf klicke, passiert nichts. Hier ist der Code:OnClick funktioniert nicht, während Animation läuft

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

Sie haben die OnTouchListener für diesen Job verwenden –

+0

Danke, dass du nicht darüber nachgedacht hast – 404response

Antwort

1

Statt Animation Sie können ViewPropertyAnimator versuchen und Sie werden in der Lage Klicks zu erkennen:

image.animate().xBy(screenWidth).setDuration(5000).start();