如何获取使用指南约束的按钮或图像的宽度和高度

How to get the width and height of a Button or Image that is constrained using Guidelines

我在这里彻底糊涂了。我试图根据已根据约束布局指南调整大小的按钮动态设置图像的高度和宽度。这将使应用程序能够根据屏幕大小将图像调整为正确的大小,以便它可以在所有设备上运行,或者这就是我的目标。

这是我使用的代码:

ImageView snellen;
Button ghostButton;

private final double HEIGHT_RATIO = 11/8.5;
//private final double DISTANCE_RATIO = 120/8.5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_snellencharttest);

    snellen = (ImageView) findViewById(R.id.snellen);
    ghostButton = (Button) findViewById(R.id.ghostButton);

    snellen.setX(ghostButton.getX());
    snellen.setY(ghostButton.getY());

    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) snellen.getLayoutParams();
    params.width = ghostButton.getWidth();
    params.height = (getHeightRatio(ghostButton.getHeight(), HEIGHT_RATIO));

    snellen.setLayoutParams(params);

    System.out.println("########################################################################### WIDTH: " + snellen.getWidth());
    System.out.println("########################################################################### HEIGHT: " + snellen.getHeight());


    ghostButton.setVisibility(View.INVISIBLE);
}

private int getHeightRatio(int ghostHeight, double ratio) {
    double calc = ghostHeight*ratio;
    return ((int) Math.round(calc));
}

为了解释我正在尝试做的事情:我有一个按约束放置在我的 xml 中的按钮,我将在下面提供代码。我想要做的是获取该按钮的 x 和 y 位置,然后是高度和宽度,并将图像分层到按钮所在的位置。在此示例中,我获取按钮的宽度 "screen",然后将高度设置为原始图像大小的比例。然后让按钮消失,因为我正在使用它作为参考。

但是如您所见,我包含了一些 SOP,它们会告诉我图像的高度和宽度是多少。这些都是 return 0。我很困惑,因为当我查看图像时,它肯定比 0 大 0。这是为了使图像尽可能大以适应屏幕吗?如果是这样,这就是我希望实现的,但我需要能够获得该图像的宽度和高度。

这是我的 xml,可能会有用:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- ____________________________________________ VERTICAL _________________________________ -->

<android.support.constraint.Guideline
    android:id="@+id/uno"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.05"/>

<android.support.constraint.Guideline
    android:id="@+id/dos"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.95"/>

<!-- __________________________________________ HORIZONTAL _________________________________ -->

<android.support.constraint.Guideline
    android:id="@+id/firstRail"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0"/>

<android.support.constraint.Guideline
    android:id="@+id/secondRail"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="1"/>

<!-- __________________________________________ ECT __________________________________________-->

<ImageView
    android:layout_width="56dp"
    android:layout_height="62dp"
    android:id="@+id/snellen"
    app:srcCompat="@drawable/eyechart"
    android:layout_marginLeft="-7dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toTopOf="@+id/secondRail" />

<Button
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/ghostButton"
    app:layout_constraintTop_toTopOf="@id/firstRail"
    app:layout_constraintLeft_toLeftOf="@id/uno"
    app:layout_constraintRight_toRightOf="@id/dos"
    app:layout_constraintBottom_toBottomOf="@id/secondRail"/>

</android.support.constraint.ConstraintLayout>

我只是将 ImageView 放在某个随机的角落,因为我稍后会调整它的大小,它实际上会改变位置和大小,所以我假设我的程序正在做我想要的。

正如我之前所说,问题是按钮 layout_widthlayout_height 设置为 0 并且它只是根据约束调整大小吗?如果是这样,我的问题变为:如何从受约束的 image/button 中获取宽度和高度,并且 layout_widthlayout_height 为 0?

感谢任何对此的见解,谢谢。

编辑:

在显示编辑之前,可能会或可能不会相关的小旁注:我使用的图像非常大。我不仅必须将它添加到我的 drawable-xhdpi 中,而且还必须将这一行添加到我的清单中,否则我的应用程序会崩溃:android:hardwareAccelerated="false"

感谢 Muthukrishnan Rajendran,我在我的 onCreate 中添加了一个 post 来等待视图完全加载,这帮助我获得了宽度和高度。不幸的是,当我添加它时,我的图像不再显示。我把所有东西都放在 运行 里面,像这样:

 ghostButton.post(new Runnable() {
        @Override
        public void run() {
            snellen.setX(ghostButton.getX());
            snellen.setY(ghostButton.getY());

            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) snellen.getLayoutParams();
            params.width = ghostButton.getWidth();
            params.height = (getHeightRatio(ghostButton.getHeight(), HEIGHT_RATIO));

            snellen.setLayoutParams(params);

            System.out.println("########################################################################### WIDTH: " + snellen.getWidth());
            System.out.println("########################################################################### HEIGHT: " + snellen.getHeight());

            ghostButton.setVisibility(View.INVISIBLE);
        }
    });

这是正确的吗? 运行 中是否有什么东西导致我的图片无法显示?

尝试获取view附加到屏幕后的宽高,经过onCreate方法应该可以完成,

那怎么办?

你可以这样使用

view.post(new Runnable() {
            @Override
            public void run() {

            }

    });

因此 运行 方法将在此项附加到 activity 屏幕后得到调用

所以对于你的情况,例如,如果你想获得 ghostButton 的 x 和 y,你应该这样做,

ghostButton.post(new Runnable() {
            @Override
            public void run() {
                snellen.setX(ghostButton.getX());
                snellen.setY(ghostButton.getY());
            }
        });