首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在创建这个android浮动气泡动画时,我做错了什么?

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

Stack Overflow用户
提问于 2019-04-24 01:26:16
回答 1查看 665关注 0票数 0

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

代码语言:javascript
运行
复制
@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代码中实现了以下内容:

代码语言:javascript
运行
复制
 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();

}

我感觉我已经拥有了所有的元素,创建了图像视图,添加了相对布局并将其设置在屏幕底部,并对屏幕宽度内的随机X位置使用了一个随机的Int,但当我运行它时什么也没有发生。我遗漏了什么?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-24 07:49:34

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

我在活动content XML中添加了一个RelativeLayout,而不是以编程方式创建它,然后引用它:

代码语言:javascript
运行
复制
RelativeLayout relativeLayout;
relativeLayout = findViewById(R.id.heartLayout);

然后,我更新了heartFlurry函数,将规则添加到heartImageView参数中,从屏幕底部开始,并使用leftMargin的randomX

代码语言:javascript
运行
复制
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();

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55816485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档