【发布时间】:2012-01-01 22:33:35
【问题描述】:
android如何在屏幕上移动图像?
伙计们,我正在为我的课程作业创建 MoleMash 游戏,我真的需要帮助!我想尝试以不同的随机坐标在屏幕上移动图像(痣),以便用户可以触摸它以获得分数。我创建了制作图像视图的背景。我有痣,是另一个图像视图吗?还是图片按钮?
我如何将它移动到屏幕的不同部分以供用户交互?
非常感谢您的帮助!
【问题讨论】:
android如何在屏幕上移动图像?
伙计们,我正在为我的课程作业创建 MoleMash 游戏,我真的需要帮助!我想尝试以不同的随机坐标在屏幕上移动图像(痣),以便用户可以触摸它以获得分数。我创建了制作图像视图的背景。我有痣,是另一个图像视图吗?还是图片按钮?
我如何将它移动到屏幕的不同部分以供用户交互?
非常感谢您的帮助!
【问题讨论】:
对于游戏开发你需要学习如何使用canvas和SurfaceView:How can I use the animation framework inside the canvas?
使用 onTouch 事件监听器,您可以将触摸位置与动画图像的位置进行比较...
【讨论】:
痣将是一个图像按钮,因此可以单击,但要使其移动,您必须使用动画。请记住,我也是 Android 动画的初学者 :)。您可能会使用 TranslateAnimation 并将参数中的 x 和 y 设置为随机变量,持续时间可能是游戏开始后时间/计数器变量达到某个点的时间。这篇文章有更多关于动画的信息:How to move an image from left to right in android.
【讨论】:
试试这个代码,
声明以下变量
private Random random=new Random();
private int screenwidth, screenhgt;
在 onCreate() 中:
screenwidth= getyourScreenWidth;
screenhgt=getyourscreenheight;
将您的视图传递给此方法(在我的情况下,我为 textview 设置动画......您传递了 imageview)
private void screenRandomAnimator(final TextView textView) {
final AnimatorSet mAnimatorSet = new AnimatorSet();
mAnimatorSet.playTogether(ObjectAnimator.ofFloat(textView, "x", (float) random.nextInt(screenwidth), (float) random.nextInt(screenwidth)),
ObjectAnimator.ofFloat(textView, "y", (float) random.nextInt(screenhgt), (float) random.nextInt(screenhgt)), ObjectAnimator.ofFloat(textView, "rotation", 360)
/* ObjectAnimator.ofFloat(textView, "scaleX", 1, 0.8f, 1, 1.1f, 1), ObjectAnimator.ofFloat(textView, "scaleY", 1, 0.8f, 1, 1.1f, 1)*/);
int Low = 1500;
int High = 2500;
int Result = random.nextInt(High - Low) + Low;
mAnimatorSet.setDuration(Result);
mAnimatorSet.start();
mAnimatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mAnimatorSet.playTogether(ObjectAnimator.ofFloat(textView, "x", textView.getX(), (float) random.nextInt(screenwidth)),
ObjectAnimator.ofFloat(textView, "y", textView.getY(), (float) random.nextInt(screenhgt)));
int Low = 1500;
int High = 2500;
int Result = random.nextInt(High - Low) + Low;
mAnimatorSet.setDuration(Result);
mAnimatorSet.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
【讨论】: