我在动画函数中以编程方式创建ImageView和TextView,ImageView大小根据值而变化。
我只需要将TextView的中心与图像视图对齐,我使用RelativeLayout参数leftMargin来确定它在X轴上的位置。
我没有尝试似乎工作,我试图使用imageView和TextView的计算大小,但我真的不明白那种数学。
我怎样才能简单地将这两个视图的中心对齐?在swift中它会像'imageView.centerXAxis'一样简单,是否有任何相当于我可以使用的而不是'leftMargin'?
这是函数,当我试图找到如何使视图在中心对齐时,还有很多需要。
void heartFlurry(String username, Integer value) {
Drawable heart = getResources().getDrawable( R.drawable.heart );
View v = new ImageView(getBaseContext());
ImageView imageView;
imageView = new ImageView(v.getContext());
imageView.setImageDrawable(heart);
final TextView usernameLabel = new TextView(this);
usernameLabel.setText(username);
usernameLabel.setTextColor(Color.WHITE);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Double widthMax = size.x * 0.8;
Double widthMin = size.x * 0.2;
int max = (int) Math.round(widthMax);
int min = (int) Math.round(widthMin);
Log.e("Width", "" + width);
Log.e("height", "" + height);
int heartWidthOriginal = heart.getIntrinsicWidth();
int heartHeightOriginal = heart.getIntrinsicHeight();
int newValue = value * 25;
int heartWidth = (heart.getIntrinsicWidth() / 2 + newValue);
int heartHeight = (heart.getIntrinsicHeight() / 2 + newValue);
Log.e("HeartWidth", "" + heartWidth);
Log.e("HeartHeight", "" + heartHeight);
Log.e("HeartWidthOriginal", "" + heartWidthOriginal);
Log.e("HeartHeightOriginal", "" + heartHeightOriginal);
final int randomX = new Random().nextInt((max - min) + 1) + min;
Log.e("randomX", "" + randomX);
relativeLayout.addView(imageView);
imageView.setId(View.generateViewId());
relativeLayout.addView(usernameLabel);
usernameLabel.setId(View.generateViewId());
usernameLabel.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
usernameLabel.setGravity(Gravity.CENTER);
usernameLabel.setTextSize(22);
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);
RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
imageView.setLayoutParams(heartParams);
imageView.getLayoutParams().height = heartHeight;
imageView.getLayoutParams().width = heartWidth;
imageView.requestLayout(); // Think the important stuff starts here:
usernameLabel.measure(0, 0); //must call measure!
usernameLabel.getMeasuredWidth();
Integer textWidth = usernameLabel.getMeasuredWidth();
Integer halfHeartWidth = heartWidth/2;
System.out.println("TEXTWIDTH" + textWidth);
textParams.leftMargin = randomX + (textWidth*halfHeartWidth / randomX);
textParams.addRule(RelativeLayout.BELOW, imageView.getId());
textParams.addRule(RelativeLayout.CENTER_VERTICAL, imageView.getId());
textParams.topMargin = 25;
usernameLabel.setLayoutParams(textParams);
ObjectAnimator animationHeartY = ObjectAnimator.ofFloat(imageView, "translationY", -size.y);
animationHeartY.setDuration(2000);
ObjectAnimator animationTextViewY = ObjectAnimator.ofFloat(usernameLabel, "translationY", -size.y);
animationTextViewY.setDuration(2000);
animationHeartY.start();
animationTextViewY.start();
animationTextViewY.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
usernameLabel.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
我希望您的视图在X轴上对齐,它们在Y轴上应该具有相同的值,并且这样做的方法是使用addRule()
的RelativeLayout.LayoutParams
方法:
textParams.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
textParams.addRule(RelativeLayout.ALIGN_BOTTOM, imageView.getId());
在这里,你告诉RelativeLayout
将你正在使用这个参数的视图与imageView
相对应,相对于它的顶部和底部。视图现在将是垂直的imageView
的大小,但是如果你用setGravity(Gravity.CENTER)
(我看到你做的那样)将文本居中,那么它将在x轴上与imageView
完美对齐。