我想制作一个游戏,图像从左向右移动,当你点击它时会发生一些事情。我把它移动了但是当我点击它时没有任何反应。这是代码:
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 :(得分:1)
您可以尝试使用ViewPropertyAnimator代替动画,但您可以检测点击次数:
***