6

我试图让我的游戏中的扑克牌重叠,以便只看到一张牌的前半部分,而另一半被下一张牌覆盖。唯一应该完全可见的卡片将是最后一张/最右边的卡片。

我已将以下代码与 framelayout 和 relativelayout 一起使用,但无济于事。谁能提供一些建议?

public int shouldShow(int numberOfCards, int card, int id)
{
    if(card == -1)
        hide(id);
    else
    {
        findViewById(id).setBackgroundDrawable(deckimages[card]);
        //findViewById(id).offsetLeftAndRight(findViewById(id).getWidth()* numberOfCards / 2);
        show(id);
        //findViewById(id).setPadding(findViewById(id).getWidth()* numberOfCards / 2, 0,0,0);
        return numberOfCards+1;
    }
    return numberOfCards;
}

我尝试使用填充和偏移方法,这两种方法都不适合我。但我也注意到 getwidth() 和 getmeasuredwidth() 方法返回 0。

关于我应该使用哪种布局以及为什么 getwidth 函数不起作用的任何建议?

xml代码在下面......会有比这更多的图像,但这是我正在测试的

<RelativeLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/RelativeLayout1">
            <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
            <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
        </RelativeLayout>
4

2 回答 2

8

我被困在这个问题上很长时间了,在搞乱代码和谷歌搜索 4 小时后。我终于想出了解决方案,而且非常简单。

您所要做的就是将下一张卡片与前一张卡片的顶部和左侧对齐。这将使第二张卡之前放置在卡的顶部。然后设置 aleftMargin将顶部的卡片向右“推”,产生部分重叠的效果。

这是我的代码(以编程方式完成):

for(int i=0; i<hand.size();i++){
        int c = hand.get(i).getCardResource();
        ib = new ImageButton(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_TOP, i-1);
        params.addRule(RelativeLayout.ALIGN_LEFT,i-1);
        params.leftMargin= 40;
        ib.setImageResource(c);
        ib.setClickable(true);
        ib.setPadding( 3, 3, 3, 3);
        ib.setId(i);    
        ib.setLayoutParams(params);
        ll.addView(ib);

    }
于 2013-02-04T21:47:43.583 回答
1

您可以提供故意与图像大小不匹配的视图边界,并使用 Imageview 的 ScaleType:http: //developer.android.com/reference/android/widget/ImageView.ScaleType.html。例如,android:scaleType="fitStart"

于 2011-10-27T16:07:14.160 回答