![](/img/trans.png)
[英]How to address QVGA (320x240) devices (HTC Wildfire, SE x10 mini, Motorola Flipout) in the xml files of Android?
[英]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.