务实地弹跳球动画

时间:2016-04-04 12:06:37

标签: java android android-animation

我尝试实现我的球动画,如给定的图像和代码所示,但我无法按照要求获得结果。

任何人都可以帮我解决这个问题吗?

我还必须控制弹跳部分,因为实际上我想将这个部分集成到我的应用程序中,这样当从服务器加载数据时,球必须停止在中间弹跳并按照显示的路径行进。

代码:

package tedd.example.com.aprilbouncing;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new BallBounce(this));
    }
}


class BallBounce extends View {
    int screenW;
    int screenH;
    float X;
    int Y;
    int initialY;
    int initialX;
    int ballW;
    int ballH;
    int angle;
    float dY;
    float acc;
    Bitmap ball, bgr;
    int count;

    public BallBounce(Context context) {
        super(context);
        ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball); //load a ball image
        //bgr = BitmapFactory.decodeResource(getResources(),R.drawable.sky_bgr); //load a background
        ballW = ball.getWidth();
        ballH = ball.getHeight();
        acc = 0.2f; //acceleration
        dY = 0; //vertical speed
        initialY = 400; //Initial vertical position.
        initialX = 0;
        angle = 0; //Start value for rotation angle.
        count=0;
    }

    @Override
    public void onSizeChanged (int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        screenW = w;
        screenH = h;
       // bgr = Bitmap.createScaledBitmap(bgr, w, h, true); //Resize background to fit the screen.
        //X = (int) (screenW /2) - (ballW / 2) ; //Centre ball into the centre of the screen.
        X = initialX;
        Y = initialY;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //Draw background.
       // canvas.drawBitmap(bgr, 0, 0, null);

        //Compute roughly ball speed and location.
        X =(screenW /2) - (ballW / 2) ;

        if(count==1){
        X =(screenW /2) - (ballW / 2) ;
        }

        if(count>4){

        }
        if(count>=4){
            X+=1;
        }
        Y+= (int) dY; //Increase or decrease vertical position.
        if (Y > (screenH - ballH)) {
            dY=(-1)*dY; //Reverse speed when bottom hit.
            count++;
        }
        dY+= acc; //Increase or decrease speed.

        //Increase rotating angle.
        if (angle++ >360)
            angle =0;
        canvas.save();
        canvas.rotate(angle*8, X + (ballW / 2), Y + (ballH / 2));
        canvas.drawBitmap(ball, X, Y, null);
        canvas.restore();

        invalidate();
    }
}

图片:

Ball's path image

as shown here

0 个答案:

没有答案