java - 如何对弹力球实现声明?

标签 java android acceleration

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

这是我的代码

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


} 

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

提前致谢

最佳答案

加速度与速度的关系就像速度与位置的关系一样。你只需要根据你的位置和速度做你正在做的事情。

最简单的解决方案是添加

yVelocity += yAccel;

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

private int yAccel = -2;

可能是合适的。

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

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

    }
}

关于java - 如何对弹力球实现声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20471531/

相关文章:

java - 用于 block Blob 的 Azure 存储服务 REST API : Content Length Issue

android - 如何获取以前版本构建的 ProGuard 映射文件

arduino - 使用arduino中的accelStepper以恒定速度前往特定位置

python - 在贝塞尔曲线的开始/结束处强制法向加速度为零

android - 运动事件android的速度或加速度

java - 如何同步两个Listview位置

java - 在 jdk 9 中打包为 jar 时出现没有 module-info.class 错误的 --module-version 或 --hash-dependencies 之一

java - 两个不同的 jar x,y 依赖于不同版本的 z。用户如何在他的应用程序中同时拥有 x,y ?

android - 如何正确使用 registerForActivityResult?获取 "LifecycleOwners must call register before they are STARTED"

Android Wifi监听策略