繁体   English   中英

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

[英]Android ldpi-layout looks different on 2 different ldpi-devices (X10 Mini and FlipOut)

我的问题是,x10 mini和翻转板上的布局结果看起来不同。

结果看起来像这样: 在此处输入图片说明

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

这里是问题和到目前为止我尝试过的事情:

1.文字提示框的大小不同。 但它们都使用相同的位图(我肯定在位图上写了ldpi)。 主要问题似乎是对于小型设备,X10 mini的密度非常高。 X10 mini的分辨率为156dpi(mdpi = 160),翻盖的分辨率为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);设置BitmapDrawable.setTargetDensity(dpi);

我这样做是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM