我试图翻译这个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代码:
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();
}
我觉得我拥有所有元素,创建了imageview,添加了相对布局并将其设置在屏幕底部,并在屏幕宽度内使用随机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();
}