비율을 16 : 9로 유지하려면 Bitmap을 ImageView로 설정해야합니다. 조언 있니? 주요 솔루션은 아마도 사용자 정의 ImageView의 onMeasure 오버라이드 방법이지만 어떻게? 당신의 XML에서 다음Android ImageView - 16 : 9
답변
public class CustomImageView extends ImageView {
// some other necessary things
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
//force a 16:9 aspect ratio
int height = Math.round(width * .5625f);
setMeasuredDimension(width, height);
}
}
<path.to.package.CustomImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img"/>
편집 : 구글은 공식적으로 API (26)을 시작 퍼센트 지원 라이브러리를 사용 중지하고 당신은 가능한 한 빨리 그것에서 멀리 이동해야합니다. 뷰 치수 중 적어도 하나가 "제약 일치"로 설정된 경우,도 9의 (0dp) : 한 비율과 같은 16보기 크기를 설정할 수 비로
세트 크기. 비율을 사용하려면 가로 세로 비율 강제 설정 (그림 10의 설명 선 1)을 클릭 한 다음 나타나는 입력에 폭 : 높이 비율을 입력하십시오.
너비와 높이가 모두 구속 조건과 일치하도록 설정된 경우 Toggle Aspect Ratio Constraint를 클릭하여 다른 차원의 비율을 기준으로 차원을 선택할 수 있습니다. 뷰 속성은 해당 가장자리를 실선으로 연결하여 비율로 설정된 뷰를 나타냅니다.
예를 들어 양면을 "일치 제한 조건"으로 설정 한 경우 가로/세로 비율 강제 전환을 두 번 클릭하여 높이 비율로 설정하십시오.
여기 추가 정보 : https://developer.android.com/training/constraint-layout/index.html#adjust-the-view-size
아래 그림과 같이 지금 전체 크기 (어떠한 방식으로 정의 될 수있다) 뷰의 높이에 의해 결정되고
PercentFrameLayout
및 PercentRelativeLayout
이 지원 라이브러리에 있습니다. 위의 레이아웃에 대해 자세히 살펴 경우
<android.support.percent.PercentFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
app:layout_widthPercent="100%"
app:layout_aspectRatio="178%"
android:scaleType="centerCrop"
android:src="@drawable/header_background"/>
<!-- The rest of your layout -->
</android.support.percent.PercentRelativeLayout>
, 당신은 문제의 ImageView
(필요 (16)에 고정 될 : 9) 볼거야 PercentFrameLayout
감싸되고 설정 두 가지 속성이있다
app:layout_widthPercent="100%"
app:layout_aspectRatio="178%"
그래서 (1) 문제의 우리
ImageView
에 대한
최고의 치수로 치수 (폭 또는 높이 중 하나) 중 하나를 정의해야합니다 : 당신이 전에 보지 않았을 수 있습니다
ImageView
. 이 경우
ImageView
은 최대 너비 (예 :
android:layout_width="match_parent"
)로 확대됩니다. (2) 가로 세로 비율 (따라서 라이브러리 이름)을 설정해야합니다.이 경우 178 % (16/9 = 1.77777777778 이상은 단순히 1.78 : 1 또는 178 %를 사용).
퍼센트 지원 라이브러리 here에 대해 자세히 알아보십시오.
위 답변은 나를 위해 작동하지 않았다, 나는이를 발견했다 :
주public class AspectRatioImageView extends ImageView {
// NOTE: These must be kept in sync with the AspectRatioImageView attributes in attrs.xml.
public static final int MEASUREMENT_WIDTH = 0;
public static final int MEASUREMENT_HEIGHT = 1;
private static final float DEFAULT_ASPECT_RATIO = 1f;
private static final boolean DEFAULT_ASPECT_RATIO_ENABLED = false;
private static final int DEFAULT_DOMINANT_MEASUREMENT = MEASUREMENT_WIDTH;
private float aspectRatio;
private boolean aspectRatioEnabled;
private int dominantMeasurement;
public AspectRatioImageView(Context context) {
this(context, null);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AspectRatioImageView);
aspectRatio = a.getFloat(R.styleable.AspectRatioImageView_aspectRatio, DEFAULT_ASPECT_RATIO);
aspectRatioEnabled = a.getBoolean(R.styleable.AspectRatioImageView_aspectRatioEnabled,
DEFAULT_ASPECT_RATIO_ENABLED);
dominantMeasurement = a.getInt(R.styleable.AspectRatioImageView_dominantMeasurement,
DEFAULT_DOMINANT_MEASUREMENT);
a.recycle();
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!aspectRatioEnabled) return;
int newWidth;
int newHeight;
switch (dominantMeasurement) {
case MEASUREMENT_WIDTH:
newWidth = getMeasuredWidth();
newHeight = (int) (newWidth/aspectRatio);
break;
case MEASUREMENT_HEIGHT:
newHeight = getMeasuredHeight();
newWidth = (int) (newHeight * aspectRatio);
break;
default:
throw new IllegalStateException("Unknown measurement with ID " + dominantMeasurement);
}
setMeasuredDimension(newWidth, newHeight);
}
/** Get the aspect ratio for this image view. */
public float getAspectRatio() {
return aspectRatio;
}
/** Set the aspect ratio for this image view. This will update the view instantly. */
public void setAspectRatio(float aspectRatio) {
this.aspectRatio = aspectRatio;
if (aspectRatioEnabled) {
requestLayout();
}
}
/** Get whether or not forcing the aspect ratio is enabled. */
public boolean getAspectRatioEnabled() {
return aspectRatioEnabled;
}
/** set whether or not forcing the aspect ratio is enabled. This will re-layout the view. */
public void setAspectRatioEnabled(boolean aspectRatioEnabled) {
this.aspectRatioEnabled = aspectRatioEnabled;
requestLayout();
}
/** Get the dominant measurement for the aspect ratio. */
public int getDominantMeasurement() {
return dominantMeasurement;
}
/**
* Set the dominant measurement for the aspect ratio.
*
* @see #MEASUREMENT_WIDTH
* @see #MEASUREMENT_HEIGHT
*/
public void setDominantMeasurement(int dominantMeasurement) {
if (dominantMeasurement != MEASUREMENT_HEIGHT && dominantMeasurement != MEASUREMENT_WIDTH) {
throw new IllegalArgumentException("Invalid measurement type.");
}
this.dominantMeasurement = dominantMeasurement;
requestLayout();
}
}
: 하나 simbol와 *
이 /
로 변경됩니다 (소스의 의견을 읽고) 그리고이 당신의 /values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AspectRatioImageView">
<attr name="aspectRatio" format="float" />
<attr name="aspectRatioEnabled" format="boolean" />
<attr name="dominantMeasurement">
<enum name="width" value="0" />
<enum name="height" value="1" />
</attr>
</declare-styleable>
</resources>
그런 다음 당신은 당신의 레이아웃 (aspectRatio = 너비/높이)에서이 방법을 사용할 수 있습니다 당신의 ImageView
에 대한 9 : PercentFrameLayout
및 PercentRelativeLayout
이후 10
내가 비 (16)를 유지하기 위해 ConstraintLayout
의 사용을 고려하도록 건의 할 것, API 레벨 26.0.0에서 사용되지 않는. ConstraintLayout
은 안드로이드 플랫폼 용 Responsive UI를 구축하기위한 강력한 도구입니다. 자세한 내용은 Build a Responsive UI with ConstraintLayout에서 확인할 수 있습니다. 여기
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
app:srcCompat="@mipmap/ic_launcher"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
모듈의 build.gradle
파일에 constraint-layout
종속성을 추가하는 것을 잊지 마세요
implementation "com.android.support.constraint:constraint-layout:1.0.2"
또는 XML 파일을 편집하는 대신 레이아웃 편집기에서 직접 레이아웃을 편집하십시오.
- 1. Xamarin, Android : 16 : 9 화면 비율 잠금
- 2. Android : ImageView API 16 대신 이미지 색상 표시
- 3. iPad 캡처 16 : 9 사진
- 4. imageview android 2.1
- 5. Android : ImageView 백그라운드에서 다른 ImageView 뒤에 있음
- 6. Android : Custom ImageView
- 7. Android 9 패치 배치
- 8. Android WebView 또는 ImageView?
- 9. Android ImageView 레이아웃 문제
- 10. Android : 이미지 캐싱 imageView
- 11. Android ImageView 흔들기 애니메이션
- 12. Android ImageView NullPointerException
- 13. ImageView onImageChangedListener Android
- 14. Android ID 및 ImageView
- 15. Android ImageView cordinates
- 16. Android ImageView ScaleType * FIT_TOP *
- 17. LinearLayout의 Android ImageView 위치
- 18. Android ImageView 관련 문제
- 19. Android, ArrayList에있는 ImageView 객체
- 20. Android Studio ImageView onDraw
- 21. Android ImageView Loop
- 22. 공유 Android ImageView 이미지
- 23. Dynamic ImageView in android
- 24. Android ImageView 가로 - 세로
- 25. android - scaling ImageView
- 26. Android ImageView 동적 ScaleType
- 27. Android - Scrollable ImageView Lag
- 28. Android ImageView 문제
- 29. ImageView 및 Listview - Android
- 30. Android ImageView scalling
[장치 화면 크기, 16 : 9,1 : 1,3 : 2,2 : 1,4 : 3]에 따라 백분율 상대 레이아웃의 이미지 뷰를 설정하는 방법 (https : //stackoverflow.com/questions/36305054/how-to-set-imageview-in-percent-relative-layout-to-following-ratios-based-on-de) –