android - 重叠图像(扑克牌)

发布于 2024-12-12 12:54:50 字数 1253 浏览 0 评论 0原文

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

我已将以下代码与框架布局和相对布局一起使用,但无济于事。有人可以提供一些建议吗?

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>

I am trying to make the playing cards in my game overlap such that only the first half of a card is seen and the other half is covered by the next playing card. The only card that should be completely visible will be the last/rightmost card.

I have used the following code with both framelayout and relativelayout to no avail. can anyone offer some suggestions?

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

i have tried using the padding and the offset methods neither of which are working for me. but i have also noticed that the getwidth() and getmeasuredwidth() methods are returning 0.

any suggestions on which layout i should use and why the getwidth functions arent working?

the xml code is below...there would be more images than this but this is what i was testing

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2024-12-19 12:54:50

我在这个问题上坚持了很长时间,花了 4 个小时搞乱代码和谷歌搜索。我终于找到了解决方案,而且非常简单。

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

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

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

    }

I was stuck on this for ages and after 4 hours messing with code and Googling. I finally figured out the solution and it's pretty simple.

All you have to do is align the next card to the top and left of the previous card. This will make it so the second card will be placed on top of the card before. Then set a leftMargin to "push" the card on top towards the right, creating the partial overlapping effect.

Here's my code (done programmatically):

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

    }
小嗲 2024-12-19 12:54:50

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

You can provide view bounds intentionally mismatched with image size, and use ScaleType of an Imageview: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html. For example, android:scaleType="fitStart"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文