背景画像付きのレイアウトがあります。画像の幅は、画面の幅 (match_parent) と同じにする必要があります。しかし、それは醜いです.OSは画像の高さを引き伸ばしません. ImageView scaleType について聞いたので、レイアウトの背景ではなく ImageView に背景画像がありますが、希望どおりに構成できません。
レイアウトまたは ImageView を設定して、画面の幅に合わせて画像を (幅で) スケーリングし、高さを同じスケールでスケーリングするにはどうすればよいですか?
写真の 2 番目の画面が必要です。
アップデート:
コードから試していますが、幅が 0 になり、ImageView を使用して xml からこれを実行できないとは信じられません。
Drawable imageDrawable = getResources().getDrawable(imageResource);
contentContainer.setBackgroundDrawable(imageDrawable);
Rect rect = imageDrawable.getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
contentContainer.post(new Runnable() {
@Override
public void run() {
contentContainer.setScaleY(contentContainer.getScaleX());
Rect rect = contentContainer.getBackground().getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
}
}