android - overlap images (playing cards)

喜欢而已 提交于 2019-12-05 07:49:55
Wayne Yun Leung

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

    }

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"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!