Java 如何实现对bouncy ball的声明?

Java 如何实现对bouncy ball的声明?,java,android,acceleration,Java,Android,Acceleration,我试图在有弹性的球上减速。我可以上下移动球,但我想添加声明,因此当我从高处落下球时,球会跳跃,然后再次撞击地面反弹,慢慢地,它的速度降低,并在最后停止 这是我的代码 import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.os.Handler; import android.util.Attribu

我试图在有弹性的球上减速。我可以上下移动球,但我想添加声明,因此当我从高处落下球时,球会跳跃,然后再次撞击地面反弹,慢慢地,它的速度降低,并在最后停止

这是我的代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;

public class AnimatedView extends ImageView{
private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 0;
private int yVelocity = 25;
private Handler h;
private final int FRAME_RATE = 20;


public AnimatedView(Context context, AttributeSet attrs)  {  
    super(context, attrs);  
    mContext = context;  
    h = new Handler();
} 

private Runnable r = new Runnable() {
    @Override
    public void run() {
        invalidate(); 
    }
};

protected void onDraw(Canvas c) {  

    BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);  
    if (x<0 && y <0) {
        x = this.getWidth()/2;
        y = this.getHeight()/2;
    } else {
        x += xVelocity;
        y += yVelocity;
        if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
            xVelocity = xVelocity*-1;
        }
        if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) {
            yVelocity = yVelocity*-1;
        }
    }
    c.drawBitmap(ball.getBitmap(), x, y, null);  

    h.postDelayed(r, FRAME_RATE);


} 
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.drawable.BitmapDrawable;
导入android.os.Handler;
导入android.util.AttributeSet;
导入android.widget.ImageView;
公共类AnimatedView扩展了ImageView{
私有上下文;
int x=-1;
int y=-1;
私有int xVelocity=0;
私人国际城市=25;
私有处理器h;
专用最终整数帧_率=20;
公共动画视图(上下文,属性集属性){
超级(上下文,attrs);
mContext=上下文;
h=新处理程序();
} 
private Runnable r=new Runnable(){
@凌驾
公开募捐{
使无效();
}
};
受保护的空白onDraw(画布c){
BitmapDrawable ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ball);
如果(x this.getHeight()-ball.getBitmap().getHeight())| |(y<0)){
yVelocity=yVelocity*-1;
}
}
c、 drawBitmap(ball.getBitmap(),x,y,null);
h、 后延迟(r,帧速率);
} 
希望有人能帮助我


提前感谢

加速度之于速度,正如速度之于位置。你只需要做你正在做的事情,把你的位置调整到你的速度

最简单的解决方案是添加

yVelocity += yAccel;
到onDraw的顶部,其中yAccel是一个整数,您希望在每个刻度处添加到yVelocity。基于你的速度值

private int yAccel = -2;
可能是合适的

编辑:您还应该添加检查,以确保球不会进入地面。我将这些代码添加到反转速度的块中,下面的代码适用于我。如果它不适合你,你的项目的另一部分可能有问题

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;

public class AnimatedView extends ImageView {
    private Context mContext;
    int x = -1;
    int y = -1;
    private int xVelocity = 0;
    private int yVelocity = 25;
    private int yAccel = 2;
    private Handler h;
    private final int FRAME_RATE = 20;

    public AnimatedView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        h = new Handler();
    }

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };



    protected void onDraw(Canvas c) {
        yVelocity += yAccel;

        BitmapDrawable ball = (BitmapDrawable) mContext.getResources()
                .getDrawable(R.drawable.cakes);
        if (x < 0 && y < 0) {
            x = this.getWidth() / 2;
            y = this.getHeight() / 2;
        } else {
            x += xVelocity;
            y += yVelocity;
            if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
                xVelocity = xVelocity * -1;
                x = Math.min(this.getWidth() - ball.getBitmap().getWidth(), x);
                x = Math.max(0, x);
            }
            if ((y > this.getHeight() - ball.getBitmap().getHeight())
                    || (y < 0)) {
                yVelocity = (int)(yVelocity * -0.8f);

                y = Math.min(this.getHeight() - ball.getBitmap().getHeight(), y);
                y = Math.max(0, y);
            }
        }
        c.drawBitmap(ball.getBitmap(), x, y, null);

        h.postDelayed(r, FRAME_RATE);

    }
}
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.drawable.BitmapDrawable;
导入android.os.Handler;
导入android.util.AttributeSet;
导入android.widget.ImageView;
公共类AnimatedView扩展了ImageView{
私有上下文;
int x=-1;
int y=-1;
私有int xVelocity=0;
私人国际城市=25;
私有int yAccel=2;
私有处理器h;
专用最终整数帧_率=20;
公共动画视图(上下文、属性集属性){
超级(上下文,attrs);
mContext=上下文;
h=新处理程序();
}
private Runnable r=new Runnable(){
@凌驾
公开募捐{
使无效();
}
};
受保护的void onDraw(画布c){
yVelocity+=yAccel;
BitmapDrawable ball=(BitmapDrawable)mContext.getResources()
.可提取(R.可提取蛋糕);
if(x<0&&y<0){
x=this.getWidth()/2;
y=this.getHeight()/2;
}否则{
x+=x速度;
y+=y线性度;
如果((x>this.getWidth()-ball.getBitmap().getWidth())| |(x<0)){
xVelocity=xVelocity*-1;
x=Math.min(this.getWidth()-ball.getBitmap().getWidth(),x);
x=数学最大值(0,x);
}
如果((y>this.getHeight()-ball.getBitmap().getHeight())
||(y<0)){
yVelocity=(int)(yVelocity*-0.8f);
y=Math.min(this.getHeight()-ball.getBitmap().getHeight(),y);
y=数学最大值(0,y);
}
}
c、 drawBitmap(ball.getBitmap(),x,y,null);
h、 后延迟(r,帧速率);
}
}

加速度只是速度随时间的变化……是的,我知道,但我不明白我怎么能每次都击中地面,这样当我的球回来时,它的速度就会降低。我尝试了你的解决方案,但我无法实现降低速度,它会在不触地的情况下开始提高速度,最后它挂起。我在答案中添加了该类的完整代码。当我测试它时,它工作得很好。现在它以一定的高度撞击地面,但它的速度不会随着每次弹跳而降低(减速)。啊。对不起,我错过了那个要求。通过更改
yVelocity=yVelocity*-1,可以在反弹过程中降低速度
yVelocity=(int)(yVelocity*-0.8f)我会相应地更新帖子。哇!!这对我来说很好。现在我试着用我的手指弹起球,球向任何方向移动。你能指导我如何继续吗。