0

公式の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に変換するにはどうすればよいですか?

レイアウトの重みを使用してレイアウトを調整できることは知っていますが、この問題には別の解決策が必要だと思います。

見て、問題を解決するのを手伝ってください。

Nexus 4 のスクリーンショット キャンバス 4 のスクリーンショット

4

7 に答える 7

4

すべてのデバイスをサポートすることはできません。異なるレイアウトまたは異なるドローアブルを作成して、各デバイスをターゲットにすることができます。

あなたができる最善のことは、ビューをプロポーションで分割する柔軟な UI を作成し、このWEIGHTを実現することです。このようにウェイトを使用してUIを比率に分割するだけです

<?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" 
    android:weightSum="6">

    <ImageView
        android:layout_weight="1"
        android:id="@+id/ft_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:layout_weight="1"
        android:id="@+id/ft_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:layout_weight="1"
        android:id="@+id/ft_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:layout_weight="1"
        android:id="@+id/ft_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:layout_weight="1"
        android:id="@+id/ft_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:layout_weight="1"
        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/ic_launcher" />

</LinearLayout>
于 2013-10-21T09:33:21.820 に答える
1

dp ピクセルは密度に依存せず、比率に依存しません。2 つのデバイス間の高さと幅の比率の違いは、非標準の dp の発生ではなく、発生している問題です。唯一の適切な解決策は、比率の違いに基づいて重量を調整することです。

密度に依存しないピクセルが、密度に依存する単位であるインチあたりのピクセルに依存している場合、密度に依存することになります。

于 2013-10-30T12:59:09.617 に答える
1

これを試して

<?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_margin="3dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ft_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ft_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ft_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ft_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ft_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
于 2013-10-21T09:30:41.970 に答える
1

スクリーンショットに見られる問題の場合、背景には繰り返し可能なテクスチャがあります。テクスチャ用に画像をトリミングし、単一の画像の代わりに繰り返し可能な背景を使用します。繰り返し可能な背景 bg_repeat.xml には、次の XML を使用できます。

 <?xml version="1.0" encoding="utf-8"?>
 <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
 android:src="@drawable/background" 
 android:tileMode="repeat" /> 

バックグラウンド

background.png

ここでトリミングした画像を src として指定し、この XML ファイルをドローアブルに配置します。さて、レイアウトでこれを行います-

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >

        <ImageView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_repeat"/>

    </LinearLayout>

IMOこれはあなたの目的を果たし、またいくつかの咬傷を節約します.

于 2013-10-30T11:29:03.593 に答える
1

ここで重みを扱う必要があると思います.XMLに同じコードを入れて違いを見てください:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:gravity="center">

    <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" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:gravity="center">

    <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" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center" >

    <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" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center" >

    <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" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:gravity="center">

    <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" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:gravity="center">

    <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>

于 2013-10-30T13:00:19.890 に答える
0

すべてのデバイスをサポートすることはできません。異なるレイアウトまたは異なるドローアブルを作成して、各デバイスをターゲットにすることができます。

線形レイアウトの代わりに、すべてのデバイスに適した相対レイアウトを使用します。

于 2013-10-30T11:35:07.213 に答える