2011-09-12 51 views
1

我的问题是布局的结果在x10 mini和翻盖上看起来不同。Android的ldpi布局在2种不同的ldpi设备(X10 Mini和FlipOut)上看起来不一样

的结果是这样的: enter image description here

左边的一个是从X10 mini和摩托罗拉FLIPOUT正确的结果。不幸的是,我没有另一个ldpi设备来检查哪个是“正确”的。

这里有问题,什么我试过到目前为止:

1. 文字气泡的大小是不一样的。但他们都使用相同的位图(我肯定会在位图上写入ldpi)。 主要问题似乎是X10 mini的密度对于小型设备来说相当高。 X10 mini具有156dpi(mdpi = 160),并且触发器具有120dpi。这个应用程序已集成admob和横幅的大小也不同。在X10 mini上它在FlipOut上看起来不错,它更大。它加载mdpi设备的大小。 (我刚刚提到它,因为它可能有助于解决此问题。)我将可绘制文件夹命名为“drawable-ldpi”。

2. 由于我将位图密度设置为DENSITIY_MEDIUM,因此第一个头像更小,锐利且清晰。 bitmap.setDensity(DisplayMetrics.DENSITY_MEDIUM);如果我将它设置为DENSITY_LOW,它看起来像其他位图。而宽度和高度都没有40像素(像我缩放它)

下面是代码:

布局的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); 
     }   
    } 
} 

回答

1

转换BitmapBitmapDrawable我必须设置targetDensitiy与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); 
相关问题