我想制作一个图像从左向右移动的游戏,当您单击它时会发生某些事情。我让它移动了,但是当我单击它时,什么也没有发生。这是代码:
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);
}
}
您可以尝试使用ViewPropertyAnimator而不是动画来检测点击:
image.animate().xBy(screenWidth).setDuration(5000).start();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句