2012-01-01 97 views
2

如何在屏幕上移動圖像android?如何在Android上的屏幕上移動圖像

夥計們,即時爲我的課程創建MoleMash遊戲,我真的需要幫助!我想嘗試將圖像(痣)以不同的隨機座標移動到屏幕上,以便用戶可以觸摸它來獲得分數。我創建了我製作圖像視圖的背景。我有痣,這是另一種圖像視圖嗎?或者它是一個圖像按鈕?

我該如何將它移動到屏幕的不同部分以便用戶進行交互?

我將不勝感激您的幫助!

回答

2

對於遊戲開發,你需要學習如何使用帆布和SurfaceView:How can I use the animation framework inside the canvas?

使用onTouch事件監聽器,你將與你的動畫圖像的位置與觸摸位置...

+0

你不需要SurfaceView如果你正在做簡單的animation.In但是你這樣做的話需要了解Android的動畫框架:http://developer.android.com/training/animation/index.html - 另一種選擇是OpenGL - 儘管在這種情況下您必須瞭解OpenGLSurface視圖。 – AgentKnopf 2015-05-09 21:24:08

0

鼴鼠將是一個圖像按鈕,所以它可以被點擊,但要使它移動,你將不得不使用動畫。請記住,我也是Android動畫的初學者:)。您可能會使用TranslateAnimation並將參數中的x和y設置爲隨機變量,持續時間可能是在遊戲開始後時間/計數器變量達到某個點時。這篇文章有更多關於動畫的信息:How to move an image from left to right in android

0

試試這個代碼,

聲明以下變量

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

     } 
    }); 
}