2016-11-17 47 views
1

我希望我的動畫在從圖片庫中選取圖片後啓動。我以爲我可以用隨機選擇開關來做到這一點,但是我得到一個錯誤,因爲'球'無法解決。我希望這不是完全錯了,但我不知道如何解決它既不android - 帶隨機圖片的動畫

protected void onDraw(Canvas c) { 

    Random randLan = new Random(); 
    int imag = randLan.nextInt(4) + 1; 

    int image = 0; 
    switch (imag) 
     { 
     case 1: 
      image = R.drawable.ballgel; 
      BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgel); 
      break; 

     case 2: 
      image = R.drawable.ballgel; 
      BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballbl); 
      break; 

     case 3: 
      image = R.drawable.ballgel; 
      BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgrue); 
      break; 

     case 4: 
      image = R.drawable.ballgel; 
      BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballrot); 
      break; 
      } 


    if (x<0) { 
     x = this.getWidth()/2; 
     y = this.getHeight()/3; 
    } 

     else { 
      x += xVelocity; 

       if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) { 
        boolean continueAnimation = false; 
       } 
     } 

    c.drawBitmap(ball.getBitmap(), x, y, null); 

    if(continueAnimation) 
    { 
     h.postDelayed(r, FRAME_RATE); 
    } 

    else { 
      x = this.getWidth()-ball.getBitmap().getWidth(); 
    } 

} 
+0

你試過了嗎'BitmapDrawable ball'你的開關盒外面? –

+0

當我把它放在開關櫃外時,'(R.drawable。...)的最後部分是什麼;'然後?這應該是變量,但我認爲它需要一個明確的參考,不是嗎? –

回答

0

你應該就在一開始就把:

private BitmapDrawable ball; 

然後你ball替換BitmapDrawable ball的開關。 那樣,ball在開始時處於初始狀態,並在開關中指定。

我已經做到了爲您:

private BitmapDrawable ball; 

protected void onDraw(Canvas c) { 

      Random randLan = new Random(); 
      int imag = randLan.nextInt(4) + 1; 

      int image = 0; 
      switch (imag) 
       { 
       case 1: 
        image = R.drawable.ballgel; 
        ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgel); 
        break; 

       case 2: 
        image = R.drawable.ballgel; 
        ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballbl); 
        break; 

       case 3: 
        image = R.drawable.ballgel; 
        ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgrue); 
        break; 

       case 4: 
        image = R.drawable.ballgel; 
        ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballrot); 
        break; 
        } 


      if (x<0) { 
       x = this.getWidth()/2; 
       y = this.getHeight()/3; 
      } 

       else { 
        x += xVelocity; 

         if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) { 
          boolean continueAnimation = false; 
         } 
       } 

      c.drawBitmap(ball.getBitmap(), x, y, null); 

      if(continueAnimation) 
      { 
       h.postDelayed(r, FRAME_RATE); 
      } 

      else { 
        x = this.getWidth()-ball.getBitmap().getWidth(); 
      } 

     } 
+0

謝謝!這是工作 –

+1

嗯,它不工作,錯誤消失了,但現在程序崩潰。你知道問題是什麼嗎? –

0

放一些圖片在你drawble 然後在drawble name.xml的 使用創建一個XML文件,此代碼:

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/a" 
    android:duration="1000"></item> 
<item android:drawable="@drawable/b" 
    android:duration="1000"></item> 
<item android:drawable="@drawable/c" 
    android:duration="1000"></item> 
</animation-list> 

後在你的主要活動中使用此代碼:

iv= (ImageView) v.findViewById(R.id.imageView); 
iv.setBackgroundResource(R.drawable.frameanimation); 
    animationDrawable=(AnimationDrawable) iv.getBackground(); 
    animationDrawable.start(); 
+0

我該如何隨機挑選呢? –