Android的 - 重叠图像(扑克牌) [英] android - overlap images (playing cards)

查看:1053
本文介绍了Android的 - 重叠图像(扑克牌)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使扑克牌在我的游戏的重叠,使得只有一张卡的前半部分被认为是,另一半由下一个扑克牌覆盖。这应该是完全可见的唯一卡将是最后一个/最右边的卡。

我用下面的code用的FrameLayout和RelativeLayout的无济于事。任何人都可以提供一些建议吗?

 公众诠释shouldShow(INT numberOfCards,诠释卡,INT ID)
{
    如果(卡== -1)
        隐藏(ID);
    其他
    {
        findViewById(ID).setBackgroundDrawable(deckimages [卡]);
        //findViewById(id).offsetLeftAndRight(findViewById(id).getWidth()* numberOfCards / 2);
        显示(ID);
        //findViewById(id).setPadding(findViewById(id).getWidth()* numberOfCards / 2,0,0,0);
        返回numberOfCards + 1;
    }
    返回numberOfCards;
}

我已经使用的填充和偏移方法,这两个国家都为我工作尝试。但我也注意到了的getWidth()和getmeasuredwidth()方法正在返回0。

任何建议,哪些布局我应该使用,为什么功能的getWidth工作的arent?

在XML code低于...会有比这更形象,但是这是我测试

 < RelativeLayout的机器人:layout_height =WRAP_CONTENT的android:layout_width =FILL_PARENT机器人:ID =@ + ID / RelativeLayout1>
            < ImageView的机器人:ID =@ + ID / imageView2的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / ImageView的>
            < ImageView的机器人:ID =@ + ID / imageView3的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / ImageView的>
        < / RelativeLayout的>


解决方案

我被困在此的年龄和4小时code和谷歌搜索后,搞乱。我终于想通了解决方案,它是pretty简单。

所有你需要做的就是对准下一个卡到顶部和左侧previous卡。这将使它所以第二卡将被前放置在卡片的上面。然后设置一个 LEFTMARGIN 来推上向右上方卡,创造了部分重叠的效果。

下面是我的code(编程完成):

 的for(int i = 0; I< hand.size();我++){
        INT C = hand.get(I).getCardResource();
        IB =新的ImageButton(本);
        RelativeLayout.LayoutParams PARAMS =新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(真);
        ib.setPadding(3,3,3,3);
        ib.setId(ⅰ);
        ib.setLayoutParams(PARAMS);
        ll.addView(IB);    }

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>

解决方案

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

    }

这篇关于Android的 - 重叠图像(扑克牌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆