我在这里完全糊涂了。我尝试根据根据约束布局的准则调整大小的按钮动态设置图像的高度和宽度。这将使应用程序能够根据屏幕大小将图像大小调整为正确的大小,以便在所有设备上都能工作,或者这是我的目标。
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位置,然后是高度和宽度,以及按钮所在位置的图像分层。在这个例子中,我得到按钮“屏幕”的宽度,然后将高度设置为原始图像大小的比率。然后使按钮消失,因为我使用它作为参考。
以下是我的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>
正如我之前所说,问题是按钮
layout_width
和
layout_height
layout_宽度
和
布局高度
对此有任何见解都将不胜感激,谢谢。
编辑:
在显示编辑之前,有一个可能相关也可能不相关的小旁注:我使用的图像非常大。我不仅要把它添加到我的
drawable-xhdpi
android:hardwareAccelerated="false"
多亏了Muthukrishnan Rajendran,我在onCreate中添加了一个帖子,等待视图完全加载,这帮助我获得了宽度和高度。不幸的是,当我在中添加时,我的图像不再显示。我把一切都放在跑步中,就像这样:
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);
}
});
这是正确的吗?跑步过程中有没有什么东西让我的形象没有出现?