ポートレートモードを想定すると、画像のサイズが可変になるImageViewがあります(ただし、常に正方形です)。ImageView の幅を常にレイアウトの幅 (fill_parent) と等しくしたいと思います。
問題は、*layout_height* も "fill_parent" に設定すると、centerCrop が元の縦横比を保持するため、高さが優先され、画像が左右でトリミングされることです。(もちろん、これは幅よりも高さが高い画面を想定しています)。ただし、layout_height を「wrap_content」に設定すると、高さがスケーリングされなくなり、四角形が切り取られてしまいます。これは、画像の元の高さを尊重するためです。
「fill_parent」のlayout_widthを優先するlayout_height設定はありますか?
<ImageView
android:id="@+id/iv_image"
android:layout_width="fill_parent"
android:layout_height="?"
android:layout_below="@id/header"
android:layout_centerHorizontal="true"
android:contentDescription="@string/cd_image"
android:padding="0dp"
android:scaleType="centerCrop"
android:src="@drawable/blank_album" />
android:layout_height 属性を削除すると、クラッシュが発生します: java.lang.RuntimeException: Binary XML file line #16: You must supply a layout_height attribute.
android:layout_height="0dp" を設定すると、画像が垂直方向につぶれます。
回答: 他のユーザーからの回答を組み合わせて使用することで、自分に合った簡単な解決策を見つけることができました。
まず、イメージ ビューには次の構成があります ( fitStartに注意してください)。
<ImageView
android:id="@+id/iv_album"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/header"
android:layout_gravity="top"
android:background="#FF00FF00"
android:contentDescription="@string/app_name"
android:padding="0dp"
android:scaleType="fitStart"
android:src="@drawable/blank_album" />
次に、プログラムで、幅に合わせて画像の高さを少し変更します (Activity.onResume() 内)。
ImageView iv = (ImageView)findViewById(R.id.iv_album);
boolean bPost = iv.post(
new Runnable()
{
public void run()
{
ImageView iv = (ImageView)findViewById(R.id.iv_album);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)iv.getLayoutParams();
params.width = ASong.this.findViewById(R.id.song_view_layout_top).getWidth();
params.height = params.width;
iv.setLayoutParams(params);
}
});
if (bPost == false)
{
throw new RuntimeException("runnable not posted");
}