2016-11-17 3 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

そしてそれをランダムに選ぶにはどうすればいいですか? –

関連する問題