【问题标题】:How can I implement Declaration to bouncy ball?如何对弹力球实施声明?
【发布时间】:2013-12-09 13:12:10
【问题描述】:

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

这是我的代码

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


} 

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

提前致谢

【问题讨论】:

  • 加速度只是速度随时间的变化......
  • 是的,我知道,但我不明白我怎么能每次都撞到地面,这样当我的球回来时它的速度会降低。

标签: java android acceleration


【解决方案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);

    }
}

【讨论】:

  • 我尝试了你的解决方案,但我无法实现减速,它在不接触地面的情况下开始加速,最后它挂了。
  • 我将课程的完整代码添加到答案中。我测试时效果很好。
  • 现在它以一定的高度着地,但每次弹跳时速度并没有降低(减速)。
  • 啊。我错过了这个要求,对不起。您可以通过将yVelocity = yVelocity * -1; 更改为yVelocity = (int)(yVelocity * -0.8f); 在反弹期间降低速度,我会相应地更新帖子。
  • 哇!!这对我很有用。现在我正试图用我的手指弹球,球向任何方向移动,你能指导我如何继续。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 2013-05-05
  • 2015-10-27
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多