Android ldpi 布局在 2 种不同的 ldpi 设备(X10 Mini 和 FlipOut)上看起来不同

标签 android layout

我的问题是布局结果在 x10 mini 和 flipout 上看起来不同。

结果是这样的: enter image description here

左边是 X10 mini 的结果,右边是 Motorola Flipout 的结果。不幸的是,我没有另一个 ldpi 设备来检查哪个是“正确的”。

以下是问题以及我目前尝试过的方法:

1. 文本气泡的大小不一样。但它们都使用相同的位图(我在位图上写了 ldpi 以确保)。 主要问题似乎是 X10 mini 的密度对于小型设备来说相当高。 X10 mini 有 156dpi (mdpi=160),flipout 有 120dpi。这个应用程序集成了 admob,横幅的大小也不同。在 X10 mini 上,它在 FlipOut 上看起来不错,它更大。它加载 mdpi 设备的大小。 (我刚才提到它是因为它可能有助于解决这个问题。)我将 drawable 文件夹命名为“drawable-ldpi”。

2。 第一个头像更小、更锐利、更清晰,因为我将位图密度设置为 DENSITIY_MEDIUM。 bitmap.setDensity(DisplayMetrics.DENSITY_MEDIUM); 如果我将它设置为 DENSITY_LOW,它看起来像其他位图。而且宽度和高度不是 40px(就像我将它缩放到的那样)

代码如下:

布局xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/orange"
    android:paddingTop="15dp"
    >
    <!-- Content -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <!-- Text Bubble -->
        <FrameLayout 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            >
            <TextView
                android:id="@+id/text_avater"
                style="@style/bubble_normal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/avatar"
                />
        </FrameLayout>
        <!-- Avatar -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:gravity="center" android:layout_weight="0.5">
            <!-- Avatar 1st row -->
            <LinearLayout 
                android:orientation="horizontal"
                android:layout_weight="1"
                android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content">
                <FrameLayout 
                    android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
                    <Button
                        android:id="@+id/button_avater1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/avatar1"
                        />
                </FrameLayout>
                <FrameLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                    <Button 
                        android:id="@+id/button_avater2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/avatar2"
                        />
                </FrameLayout>
                <FrameLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                    <Button 
                        android:id="@+id/button_avater3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/avatar3"
                        />
                </FrameLayout>
            </LinearLayout>
            <!-- /Avatar 1st row -->
            <!-- Avatar 2nd row -->
            <LinearLayout 
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                >
                <FrameLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                    <Button 
                        android:id="@+id/button_avater4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/avatar0"
                        />
                </FrameLayout>
                <FrameLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                    <Button 
                        android:id="@+id/button_avater5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/avatar0"
                        />
                </FrameLayout>
                <FrameLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                    <Button 
                        android:id="@+id/button_avater6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/avatar0"
                        />
                </FrameLayout>
            </LinearLayout>
            <!-- /Avatar 2nd row -->
            <!-- Avatar 3rd row -->
            <LinearLayout 
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                >
                <FrameLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                    <Button 
                        android:id="@+id/button_avater7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/avatar0"
                        />
                </FrameLayout>
                <FrameLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                    <Button 
                        android:id="@+id/button_avater8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/avatar0"
                        />
                </FrameLayout>
            </LinearLayout>
            <!-- /Avatar 3rdst row -->
        </LinearLayout>
        <!-- /Avatar -->
        <!-- Button -->
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:layout_weight="1.15"
            android:gravity="center"
            >
            <FrameLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                >
                <Button
                    android:id="@+id/button_mainmenu"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center|bottom"
                    android:layout_weight="1"
                    android:text="@string/mainmenu"
                    style="@style/button_small"
                    />
            </FrameLayout>
            <FrameLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                >
                <Button
                    android:id="@+id/button_done"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center|bottom"
                    android:layout_weight="1"
                    android:text="@string/done"
                    style="@style/button_small"
                    />
            </FrameLayout>
        </LinearLayout>
        <!-- /Button -->
    </LinearLayout>
    <!-- /Content -->
</RelativeLayout>

样式看起来像这样:

<style name="bubble_normal">
    <item name="android:gravity">center</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:textStyle">bold</item>
    <item name="android:background">@drawable/text_bubble_normal</item>
    <item name="android:textSize">@dimen/standard_text_size</item>
</style>

位图的大小为 210 x 93。

我以编程方式加载的头像位图:

private void setupView() {
    byte numberOfPlayer = (byte) StbApp.getPlayer().size();
    int avatarSize = Player.avatarSize;
    Log.d(TAG, "avatarSize: " + avatarSize); // avatarSize: 40px

    Bitmap bitmap = Bitmap.createScaledBitmap(
            StbApp.getPlayer().get(0).getAvatar(), 
            avatarSize, 
            avatarSize, 
            true
            );
    bitmap.setDensity(DisplayMetrics.DENSITY_MEDIUM);

    BitmapDrawable bitmapDrawable = new BitmapDrawable(
            bitmap
            );

    avatarBtn[0] = (Button) findViewById(R.id.button_avater1);
    avatarBtn[0].setBackgroundDrawable(bitmapDrawable);
    avatarBtn[0].setOnClickListener(this);


    bitmapDrawable = new BitmapDrawable(
            Bitmap.createScaledBitmap(
                    StbApp.getPlayer().get(1).getAvatar(), 
                    avatarSize,
                    avatarSize, 
                    true
                    )
            );
    avatarBtn[1] = (Button) findViewById(R.id.button_avater2);
    avatarBtn[1].setBackgroundDrawable(bitmapDrawable);
    avatarBtn[1].setOnClickListener(this);

    bitmapDrawable = new BitmapDrawable(
            Bitmap.createScaledBitmap(
                    StbApp.getPlayer().get(2).getAvatar(), 
                    avatarSize, 
                    avatarSize, 
                    true
                    )
            );
    avatarBtn[2] = (Button) findViewById(R.id.button_avater3);
    avatarBtn[2].setBackgroundDrawable(bitmapDrawable);
    avatarBtn[2].setOnClickListener(this);

    avatarBtn[3] = (Button) findViewById(R.id.button_avater4);
    avatarBtn[3].setOnClickListener(this);

    avatarBtn[4] = (Button) findViewById(R.id.button_avater5);
    avatarBtn[4].setOnClickListener(this);

    avatarBtn[5] = (Button) findViewById(R.id.button_avater6);
    avatarBtn[5].setOnClickListener(this);

    avatarBtn[6] = (Button) findViewById(R.id.button_avater7);
    avatarBtn[6].setOnClickListener(this);

    avatarBtn[7] = (Button) findViewById(R.id.button_avater8);
    avatarBtn[7].setOnClickListener(this);

    mainMenuBtn = (Button) findViewById(R.id.button_mainmenu);
    mainMenuBtn.setOnClickListener(this);

    doneBtn = (Button) findViewById(R.id.button_done);
    doneBtn.setOnClickListener(this);

    //prepare avatar_0 if needed
    if (numberOfPlayer < 8){
        Bitmap avatarNull = BitmapFactory.decodeResource(getResources(), R.drawable.avatar0);
        avatarNull = Player.strokeBitmap(avatarNull);
        avatarNull = Bitmap.createScaledBitmap(
                avatarNull, 
                avatarSize, 
                avatarSize, 
                true
            );

        for (byte i = 3; i < 8; i++){
            if (i < numberOfPlayer){
                bitmapDrawable = new BitmapDrawable(
                            Bitmap.createScaledBitmap(StbApp.getPlayer().get(i).getAvatar(), 
                            avatarSize, 
                            avatarSize, 
                            true)
                        );
            }
            else{
                bitmapDrawable = new BitmapDrawable(avatarNull);
            }
            avatarBtn[i].setBackgroundDrawable(bitmapDrawable);
        }
    } 
    //8 player no avatar0 needed
    else{
        for (byte i = 3; i < numberOfPlayer; i++){
            bitmapDrawable = new BitmapDrawable(StbApp.getPlayer().get(i).getAvatar());
            avatarBtn[i].setBackgroundDrawable(bitmapDrawable);
        }           
    }
}

最佳答案

Bitmap 转换为 BitmapDrawable 后,我必须使用 BitmapDrawable.setTargetDensity(dpi);

设置 targetDensitiy

我是这样做的:

metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

tempAvatarBitmap = BitmapFactory.decodeStream(fis, null, opt);
tempAvatarBitmap = Bitmap.createScaledBitmap(
            StbApp.getPlayer().get(requestCode-1).getAvatar(), 
                Player.avatarSize, 
                Player.avatarSize, 
                true
            );

BitmapDrawable drawable = new BitmapDrawable(tempAvatarBitmap);
drawable.setTargetDensity(metrics.densityDpi);

关于Android ldpi 布局在 2 种不同的 ldpi 设备(X10 Mini 和 FlipOut)上看起来不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7386601/

相关文章:

android - Handler.dispatchMessage(msg) 和 Handler.sendMessage(msg) 的区别

Android:完全控制手机(kiosk模式),有可能吗?如何?

android - 如何避免默认选择 BottomNavigationView 设置中的第一项

java - 无法正确显示 Java GUI 布局

java JPanel setSize 和 setLocation

java - 从 Jenkins 运行 Android 模拟器以使用 Robotium 运行 Junit 测试

java - 如何从 SQLite 表中获取所有行?

html - 从绝对切换回静态?相对的?

html - 当文本超出预期时将框保持在原位

c++ - Qt QHBoxLayout 问题?