BitmapDrawable无法在LayerDrawable中正确缩放

时间:2014-04-22 03:27:07

标签: android scale layerdrawable bitmapdrawable

我正在进行二十一点游戏。我正在尝试使用图层可显示玩家的手牌。但是,卡片图像无法正确缩放。它们太宽了。我把我的代码放在下面。基本上,我将Bitmap放入BitmapDrawable中,我将其设置为LayerDrawable内的ImageView,并且BitmapDrawable的尺寸在我将其放入LayerDrawable中时似乎会被破坏。

我尝试过调整adjustViewBounds,设置最大宽度/高度,改变重力(使图像变小 - 加载图像时可能出错)并设置边界无效。

感谢您提供的任何帮助。

XML布局文件:

        <ImageView
            android:id="@+id/playerHand"
            android:contentDescription="@string/playerHand"
            android:adjustViewBounds="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="@dimen/lHandWidth"
            android:maxHeight="@dimen/lHandHeight"
            android:scaleType="centerInside"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="220dp"
            android:layout_marginLeft="150dp"
            android:src="@drawable/first_hand" />

first_hand.xml:

    <item  
    android:id="@+id/fh_1" >
    <bitmap android:src="@drawable/card_empty"
        android:gravity="center"/>
    </item>

<item
    android:left="@dimen/lCardOffsetOne"
    android:id="@+id/fh_2" >
    <bitmap android:src="@drawable/card_empty"
        android:gravity="center"/>
    </item>

代码:

     private void deal(Card aCard, int cardIndex, boolean player)
{
    LayerDrawable theHand = null;
    ImageView ldView = null;
     //Unrelated code
    ldView = (ImageView) findView(R.id.playerHand, "Player hand");
    theHand = (LayerDrawable) ldView.getDrawable();

    //Unrelated code     
    final int cardOffset = (int) getResources().getDimension(R.dimen.lCardOffsetOne);

  /*cardWidth is calculated, using the aspect ratio, from a card height of 100dp   
   (a constant stored in my R.dimens file),*/
    if (!terminalCard)
     ldView.setMaxWidth(cardWidth + cardOffset * cardIndex);
   else 
     ldView.setMaxWidth(cardWidth + cardOffset * (MAX_HAND_SIZE -1));


     Bitmap aBitmap = GraphicsLoader.getBitmap(aCard);

      Log.v("deal(card, int,boolean)", "Loaded card bitmap: height = " + 
       aBitmap.getHeight() + ", width = "
         + aBitmap.getWidth() );
      //This lists height 259, width 208, which is the correct aspect ratio. 


   BitmapDrawable thing = new BitmapDrawable(getResources(), aBitmap); 
 //thing.setGravity(Gravity.LEFT);
  // Makes it tiny and doesn't change the bounds 
 final int resourceIDOfLayer;
 if (terminalCard)
     resourceIDOfLayer = theHand.getId(MAX_HAND_SIZE);
 else resourceIDOfLayer = theHand.getId(cardIndex);

 if (!theHand.setDrawableByLayerId(resourceIDOfLayer, thing))
   System.err.println("Failed to replace graphic of " + resourceIDOfLayer);
  Rect aRect = theHand.getDrawable(cardIndex).getBounds();
  System.out.println("After I place the BMDrawable in the LD, its bounds are: Height = " + aRect.height() +
  ", width is " + aRect.width() );
   // The above line lists large and disproportionate bounds (height 519, varying width)
   // I set the max height of the ImageView to 100dp.

    ldView.setImageDrawable(theHand);

   ldView.requestLayout(); // ?? Needed because I changed the max width?
   ldView.invalidate();

   //Other code
   }

0 个答案:

没有答案