android - 随机图片动画

时间:2016-11-17 10:17:49

标签: java android animation graphics

我希望在从图片池中挑选图片后启动动画。我以为我可以用随机选择开关做到这一点,但后来我得到一个错误,因为'球'无法解决。我希望这不完全错,但我不知道如何解决它

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

} 

2 个答案:

答案 0 :(得分:0)

你应该把它放在开头:

private BitmapDrawable ball;

然后用BitmapDrawable ball替换开关中的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();
            }

        }

答案 1 :(得分:0)

将一些图像放入您的绘图中 然后在drawble name.xml中创建一个xml文件 使用此代码:

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

之后,在您的mainactivity中使用此代码:

{{1}}