如果没有足够的空间显示它们,重叠图像
问题描述:
我正在编写一个小型纸牌游戏,其中用户有五张纸牌,它们应位于屏幕的左侧。假设每张卡片的宽度设置为100dp。然后我想让这些卡片相应地裁剪并放置在彼此之下。如果卡片的总高度太大而无法显示,我希望它们重叠,而不是让它们变小。如果没有足够的空间显示它们,重叠图像
例如,左侧可能是平板电脑,其中空间足够,右侧是我想要卡片重叠的智能手机。
任何想法,我该怎么办呢?
答
我得到它的工作:
代码
public class HandCardsLayout extends RelativeLayout {
// constructors etc..
public void init() {
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int heightLandscape = size.y;
int viewDistance = heightLandscape/5;
for (int i = 0; i < 5; i++) {
ImageView imageView = getImageViewAt(i);
if(i < 4) {
imageView.setPadding(0, 0, 0, -viewDistance);
}
}
}
}
版式文件
<de.memorian.HandCardsLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:color/transparent"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_above="@+id/handCard2"
android:id="@+id/handCard1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_above="@+id/handCard3"
android:id="@+id/handCard2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_above="@+id/handCard4"
android:id="@+id/handCard3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_above="@+id/handCard5"
android:id="@+id/handCard4" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"
android:id="@+id/handCard5" />
</de.memorian.HandCardsLayout>
这段代码与ImageView以一致的距离重叠。
答
你可以计算出你的imageviews的高度,然后裁剪名片图像这样的:
bm = Bitmap.createBitmap(originalbitmap, 0, 0, fullViewWidth, calculatedViewHeight);
imageview.setImageBitmap(bm);