公式のGoogleドキュメントに従って、さまざまなデバイス用の画像を設定しています.Googleドキュメントに従って、ピクセルはデバイスによって異なる場合があるため、常にdp(密度に依存しないピクセル)を使用する必要があります. だから私はdp(密度に依存しないピクセル)に従って画像を管理しました。描画可能な xhdpi、hdpi、mdpi、および ldpi に画像を配置しました。ほとんどのデバイスでうまく機能しますが、デバイスごとにppiピクセルが異なる場合があるため、dp(密度に依存しないピクセル)は固定されていないため、dp(密度に依存しないピクセル)に基づくすべての計算がうまくいかず、適切に設定できません。
例を挙げて説明しましょう。ここに私が設定している私のxmlがあります:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ft_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/ico_test" />
<ImageView
android:id="@+id/ft_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/ico_test" />
<ImageView
android:id="@+id/ft_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/ico_test" />
<ImageView
android:id="@+id/ft_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/ico_test" />
<ImageView
android:id="@+id/ft_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/ico_test" />
<ImageView
android:id="@+id/ft_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ico_test" />
</LinearLayout>
Micromax canvas 4 (294 ppi ピクセル) でこのレイアウトを見ると、完璧に見えます。しかし、Google Nexus 4 (318 ppi ピクセル) では、右側からより多くのスペースを残します (添付した画像で確認できます)。
私は次の詳細を取得しようとしました
density :
dpHeight :
dpWidth :
以下のJavaコードを使用:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
nexus 4 と canvas 4 で次の結果が得られます。
(canvas 4)
density : 2.0
dpHeight : 640
dpWidth : 360
(nexus 4)
density : 2.0
dpHeight : 592
dpWidth : 384
ここでわかるように、これらのデバイスでは dp(密度に依存しないピクセル) が異なります。ppi ピクセルが異なるためだと思います。したがって、dp(密度に依存しないピクセル) に基づくすべての計算がうまくいきません。dpが固定されていない場合、どうすれば画像を管理できますか??
キャンバス 4 と nexus 4 でレイアウトがどのように見えるかの画像のスクリーン ショットも添付しました。
この質問も参照しました Android画像のppiをdpiに変換するにはどうすればよいですか?
レイアウトの重みを使用してレイアウトを調整できることは知っていますが、この問題には別の解決策が必要だと思います。
見て、問題を解決するのを手伝ってください。