怎么做实用弹跳球

时间:2016-04-04 10:06:29

标签: 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

0 个答案:

没有答案