나는 여기서 완전히 혼란 스럽습니다. Constraint Layout의 지침에 따라 크기가 조정 된 버튼을 기반으로 이미지의 높이와 너비를 동적으로 설정하려고했습니다. 이렇게하면 응용 프로그램이 화면 크기에 따라 이미지 크기를 올바른 크기로 조정할 수 있으므로 모든 장치에서 작동하거나 그것이 내 목표입니다.
사용중인 코드는 다음과 같습니다.
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 위치를 얻은 다음 높이와 너비를 얻고 버튼이있는 곳에 이미지를 거의 레이어링하는 것입니다. 이 예에서는 "화면"버튼의 너비를 얻은 다음 높이를 원래 이미지 크기의 비율로 설정합니다. 그런 다음 참조 용으로 사용하고 있으므로 버튼을 사라지게합니다.
그러나 보시다시피 이미지의 높이와 너비를 알려주는 SOP를 포함했습니다. 둘 다 0을 반환합니다. 이미지를 볼 때 확실히 0 x 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_width
과 버튼 layout_height
이 0으로 설정되어 있고 제약 조건에 따라 크기가 조정 된다는 사실이 문제 입니까? 그렇다면, 내 질문 변경 : 어떻게 구속되어 이미지 / 버튼의 폭과 높이를 얻을 수 있습니다 layout_width
및 layout_height
0입니까?
이에 대한 통찰력을 높이 평가합니다.
편집하다:
편집 내용을 표시하기 전에 적절할 수도 있고 아닐 수도있는 작은 사이드 노트 : 사용중인 이미지가 매우 큽니다. 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);
}
});
이 올바른지? 내 이미지가 표시되지 않게하는 실행 내부에 무언가가 있습니까?
화면에 뷰를 첨부 한 후 너비와 높이를 구해 보시고, onCreate
방법을 통해 완성되어야합니다 .
그러면 어떻게?
이렇게 사용할 수 있습니다
view.post(new Runnable() {
@Override
public void run() {
}
});
따라서이 항목이 활동 화면에 첨부 된 후 실행 메소드가 호출을받습니다.
따라서 예를 들어 x와 y를 얻으려면 다음과 ghostButton
같이해야합니다.
ghostButton.post(new Runnable() {
@Override
public void run() {
snellen.setX(ghostButton.getX());
snellen.setY(ghostButton.getY());
}
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다