android - 重叠图像(扑克牌)
我试图使游戏中的扑克牌重叠,以便只能看到纸牌的前半部分,而另一半则被下一张扑克牌覆盖。唯一应该完全可见的卡片是最后/最右边的卡片。
我已将以下代码与框架布局和相对布局一起使用,但无济于事。有人可以提供一些建议吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这个问题上坚持了很长时间,花了 4 个小时搞乱代码和谷歌搜索。我终于找到了解决方案,而且非常简单。
您所要做的就是将下一张卡片与上一张卡片的顶部和左侧对齐。这将使第二张卡被放置在之前的卡的顶部。然后设置
leftMargin
将顶部的卡片向右“推”,创建部分重叠的效果。这是我的代码(以编程方式完成):
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):
您可以故意提供与图像大小不匹配的视图边界,并使用 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"