【问题标题】:How to move a image across the screen on Android如何在 Android 上跨屏幕移动图像
【发布时间】:2012-01-01 22:33:35
【问题描述】:

android如何在屏幕上移动图像?

伙计们,我正在为我的课程作业创建 MoleMash 游戏,我真的需要帮助!我想尝试以不同的随机坐标在屏幕上移动图像(痣),以便用户可以触摸它以获得分数。我创建了制作图像视图的背景。我有痣,是另一个图像视图吗?还是图片按钮?

我如何将它移动到屏幕的不同部分以供用户交互?

非常感谢您的帮助!

【问题讨论】:

    标签: android animation


    【解决方案1】:

    对于游戏开发你需要学习如何使用canvas和SurfaceView:How can I use the animation framework inside the canvas?

    使用 onTouch 事件监听器,您可以将触摸位置与动画图像的位置进行比较...

    【讨论】:

    • 如果你正在做简单的动画,你不需要 SurfaceView。在这种情况下,你需要了解 Android 的动画框架:developer.android.com/training/animation/index.html - 另一种选择是 OpenGL - 虽然你必须知道关于这种情况下的 OpenGLSurface 视图。
    【解决方案2】:

    痣将是一个图像按钮,因此可以单击,但要使其移动,您必须使用动画。请记住,我也是 Android 动画的初学者 :)。您可能会使用 TranslateAnimation 并将参数中的 x 和 y 设置为随机变量,持续时间可能是游戏开始后时间/计数器变量达到某个点的时间。这篇文章有更多关于动画的信息:How to move an image from left to right in android.

    【讨论】:

      【解决方案3】:

      试试这个代码,

      声明以下变量

      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) {
      
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-04
        • 1970-01-01
        • 2016-05-15
        • 1970-01-01
        • 2018-03-28
        • 1970-01-01
        • 2012-01-07
        相关资源
        最近更新 更多