【问题标题】:Android:Put an image over another imageAndroid:将图像放在另一个图像上
【发布时间】:2014-02-26 06:18:32
【问题描述】:
我有三个 ImageViews,我希望 1 个 ImageView 在背景中,另外 2 个图像应该放在那个图像上。我无法使用任何其他视图,因为图像以无法设置为Linear 或Relative 布局背景的图像形式下载。
第一张图片
这是我真正想要的第二张图片
【问题讨论】:
标签:
android
view
android-imageview
【解决方案1】:
你可以试试下面的xml布局...
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/ic_launcher"
android:scaleType="fitXY" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="100dip"
android:background="@drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="100dip"
android:background="@drawable/ic_launcher" />
</FrameLayout>
【解决方案2】:
你可以这样使用:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
【解决方案3】:
重叠视图或阻止单个项目的显示区域可以使用框架布局(here)。
你可以使用
<FrameLayout
android:layout_width="..."
android:layout_height="..."
android:background="Image1"
<ImageView
android:layout_width="..."
android:layout_height="..."
android:background="image2">
<ImageView
android:layout_width="..."
android:layout_height="..."
android:background="image3">
</FrameLayout>
【解决方案4】:
这是我发现的将多张图片合并为一张的最简单方法。
如果我们想要共享合并文件,这将非常有用。
Resources r = getResources();
Drawable[] layers = new Drawable[3];
layers[0] = r.getDrawable(R.drawable.image3);
layers[1] = r.getDrawable(R.drawable.image1);
layers[2] = r.getDrawable(R.drawable.image2);
LayerDrawable layerDrawable= new LayerDrawable(layers);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// align image1 over image3
layerDrawable.setLayerGravity(1, Gravity.TOP);
layerDrawable.setLayerInsetTop(1, 250);
// align image2 over image3
layerDrawable.setLayerGravity(2, Gravity.BOTTOM);
layerDrawable.setLayerInsetBottom(2, 250);
}
// both image1 & 2 placed over image3 in layerDrawable.
imageView.setImageDrawable(layerDrawable);
希望这会奏效。
【解决方案5】:
首先使用 relativie 布局,添加宽度和高度设置为匹配父级的图像视图。然后将第二个图像视图的 alignParentTop 属性设为 true,第三个图像视图将其属性 parentbottom 对齐,宽度和高度设置为包装内容
【解决方案6】:
你可以在你的drawable中使用layer-list make layers.xml
像这样的
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item>
<bitmap android:src="@drawable/img1"
android:gravity="center"
/>
</item>
<item>
<bitmap android:src="@drawable/img2"
android:gravity="center"
/>
</item>
</layer-list>