Joe S Joe S - 19 days ago 4
Java Question

android - animation with random picture

I want my animation to start after picking an image out of a pool of pictures. I thought I could do that with a randomly picking switch, but then I get an error, because 'ball' cannot be resolved. I hope it's not entirely wrong, but i don't know how to fix it neither

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

}

Answer

You should put right at the beginning:

private BitmapDrawable ball;

Then you replace BitmapDrawable ball in the switch with ball. like that, ball is initalized at the beginning and is specified in the switch.

I've done it for you:

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

        }
Comments