我在创建这个 android 浮动气泡动画时做错了什么?

What am I doing wrong in creating this android floating bubble animation?

我正在尝试翻译这段 Swift 代码:

@objc func heartFlurry()
{
    let heartImage = UIImage(named: "heartWhite")
    let heartImageView = UIImageView(image: heartImage)
    let screenSize = UIScreen.main.bounds
    let heartWidth = Int(heartImage!.size.width)
    let heartHeight = Int(heartImage!.size.height)
    let randomX = arc4random_uniform(UInt32(screenSize.width))
    heartImageView.frame = CGRect(x: Int(randomX) - Int(Double(heartWidth) * 0.5), y: Int(screenSize.height) + heartHeight, width: heartWidth, height: heartHeight)
    view.addSubview(heartImageView)
    let randomIntFrom0To4 = Int.random(in: 1..<6)
    print(randomIntFrom0To4)
    self.updateLove()
    self.playSound(sound: "pop_\(randomIntFrom0To4)")
    UIView.animate(withDuration: 1.5, animations: {
        heartImageView.center = CGPoint(x: heartImageView.center.x, y: CGFloat(-heartHeight))
    }) { (finished: Bool) in
        heartImageView.removeFromSuperview()
    }
}

进入 Java,代码(多次点击时)创建的效果如下所示:

到目前为止,我已经将其放入我的 Java 代码中:

 void heartFlurry() {

    Drawable heart = getResources().getDrawable( R.drawable.heart );
    View v = new ImageView(getBaseContext());
    ImageView imageView;
    imageView = new ImageView(v.getContext());
    imageView.setImageDrawable(heart);

    Integer heartWidth = heart.getIntrinsicWidth();
    Integer heartHeight = heart.getIntrinsicHeight();

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    Log.e("Width", "" + width);
    Log.e("height", "" + height);

    final int randomX = new Random().nextInt(size.x);
    Log.e("randomX", "" + randomX);

    // RelativeLayout. though you can use xml RelativeLayout here too by `findViewById()`
    final RelativeLayout relativeLayout = new RelativeLayout(this);

    // Setting layout params to our RelativeLayout
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(size.x, size.y);

    // Setting position of our ImageView
    layoutParams.leftMargin = randomX;
    layoutParams.topMargin = 500;

    // Finally Adding the imageView to RelativeLayout and its position
    relativeLayout.addView(imageView, layoutParams);

    ObjectAnimator animationY = ObjectAnimator.ofFloat(imageView, "translationY", -size.y);
    animationY.setDuration(500);
    animationY.start();

    new CountDownTimer(500, 1000) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
           relativeLayout.removeAllViews();

        }
    }.start();

}

我觉得我拥有所有元素,创建图像视图,添加相对布局并将其设置在屏幕底部,并使用随机 Int 作为屏幕宽度内的随机 X 位置,但没有任何反应当我运行它。我错过了什么?

谢谢。

问题出在我创建的相对布局上,它没有正确添加到视图中。

我在 XML 中将 RelativeLayout 添加到活动内容 XML 而不是以编程方式创建它,然后引用它:

RelativeLayout relativeLayout;
relativeLayout = findViewById(R.id.heartLayout);

然后我更新了 heartFlurry 函数,在 heartImageView 参数中添加了一条规则,从屏幕底部开始,并使用 randomX 作为 leftMargin。

void heartFlurry() {

    Drawable heart = getResources().getDrawable( R.drawable.heart );
    View v = new ImageView(getBaseContext());
    ImageView imageView;
    imageView = new ImageView(v.getContext());
    imageView.setImageDrawable(heart);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    Log.e("Width", "" + width);
    Log.e("height", "" + height);

    final int randomX = new Random().nextInt(size.x);
    Log.e("randomX", "" + randomX);

    RelativeLayout.LayoutParams paramsImage = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    paramsImage.addRule(RelativeLayout.CENTER_IN_PARENT);
    imageView.setLayoutParams(paramsImage);
    relativeLayout.addView(imageView);

    RelativeLayout.LayoutParams heartParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    heartParams.leftMargin = randomX;
    heartParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    imageView.setLayoutParams(heartParams);

    ObjectAnimator animationY = ObjectAnimator.ofFloat(imageView, "translationY", -size.y);
    animationY.setDuration(500);
    animationY.start();

    new CountDownTimer(1000, 1000) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
           relativeLayout.removeAllViews();
            Log.e("randomX", "Timer Done");

        }
    }.start();

}