如何以编程方式使TextView与ImageView的中心对齐?

时间:2019-04-25 19:14:25

标签: java android textview relativelayout

我正在通过动画功能以编程方式制作ImageView和TextView,ImageView的大小根据值而变化。

我只需要将TextView的中心与图像视图对齐,我使用RelativeLayout参数leftMargin来确定其在X轴上的位置。

我尝试的一切似乎都没有用,我试图使用imageView和TextView的计算出的大小,但是我并不真正理解这种数学。

如何简单地使这两个视图的中心对齐?很快,它就像“ 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) {

        }
    });

}

1 个答案:

答案 0 :(得分:1)

我希望视图在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轴上与{{ 1}}。