【问题标题】:android - overlap images (playing cards)android - 重叠图像(扑克牌)
【发布时间】:2011-10-27 15:54:18
【问题描述】:

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

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

【问题讨论】:

  • 它已更新 --- 它是另一个相对布局中的相对布局

标签: android layout android-relativelayout overlap android-framelayout


【解决方案1】:

我在这个问题上停留了很长时间,在搞乱代码和谷歌搜索 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);

    }

【讨论】:

  • 什么是ll in ll.addView(ib);这是否意味着父容器具有线性布局?如果您能解释和分享布局文件的代码,将不胜感激。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 2011-07-09
    • 1970-01-01
    相关资源
    最近更新 更多