如何在Android上的屏幕上移动图像
问题描述:
如何在屏幕上移动图像android?如何在Android上的屏幕上移动图像
伙计们,即时为我的课程创建MoleMash游戏,我真的需要帮助!我想尝试将图像(痣)以不同的随机坐标移动到屏幕上,以便用户可以触摸它来获得分数。我创建了我制作图像视图的背景。我有痣,这是另一种图像视图吗?或者它是一个图像按钮?
我该如何将它移动到屏幕的不同部分以便用户进行交互?
我将不胜感激您的帮助!
答
对于游戏开发,你需要学习如何使用帆布和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) {
}
});
}
你不需要SurfaceView如果你正在做简单的animation.In但是你这样做的话需要了解Android的动画框架:http://developer.android.com/training/animation/index.html - 另一种选择是OpenGL - 尽管在这种情况下您必须了解OpenGLSurface视图。 – AgentKnopf 2015-05-09 21:24:08