0

我正在尝试在弹力球上实施减速。我可以上下移动球,但我想添加声明,所以当我从高处放下球时,它会跳跃然后再次撞击地面反弹,然后慢慢地慢慢降低速度并在最后停止。

这是我的代码

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


} 

希望任何人都可以帮助我。

提前致谢

4

1 回答 1

1

加速度与速度之比,就像速度与位置之比一样。你只需要根据你的速度来做你正在做的事情。

最简单的解决方案是添加

yVelocity += yAccel;

到 onDraw 的顶部,其中 yAccel 是一个 int,您希望每个刻度都添加到 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);

    }
}
于 2013-12-09T13:47:34.660 回答