简体   繁体   English

Android:如何将透明图像以其确切的宽度和高度放置在另一个图像上

[英]Android: How to place a transparent image over another image with its exact width and height

I have 2 images main_bg and a transparent image ship. 我有2张图片main_bg和一张透明图片。 Ship is cropped from main_bg and I want to place cropped image ship on main_bg on the exact position where ship appears on main_bg. 船舶从main_bg中裁剪,我想将裁剪后的图像船舶放置在main_bg上船出现在main_bg上的确切位置上。 When I use 当我使用

android:layout_width="match_parent" android:layout_height="match_parent"

ship appear as an elongated one. 船似乎是细长的。 When I use "wrap_content" instead of "match_parent", it appear as a small image. 当我使用“ wrap_content”而不是“ match_parent”时,它显示为小图像。 How can I set ship on main_bg with its exact width and height. 如何在main_bg上设置船的确切宽度和高度。

This is my xml code. 这是我的xml代码。

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/main_bg" 
android:gravity="left" 
android:orientation="horizontal">
     <LinearLayout 
    android:id="@+id/ship" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/ship" 
    android:orientation="vertical">
</LinearLayout>

How can I place it properly? 如何正确放置?

You should try using FrameLayouts to superimpose one image over the other. 您应该尝试使用FrameLayouts将一个图像叠加在另一个图像上。 Like: 喜欢:

FrameLayout
    Image
FrameLayout
    Image

And set the dimensions of the FrameLayouts as you want to. 并根据需要设置FrameLayouts的尺寸。

You can use two FrameLayout and do something like this : 您可以使用两个FrameLayout并执行以下操作:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/main_bg" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ship" >

    </FrameLayout>

</FrameLayout>

use LayeredImageView from this post How to maintain multi layers of ImageViews and keep their aspect ratio based on the largest one? 使用本文中的LayeredImageView 如何维护多层ImageView,并根据最大的纵横比来保持其长宽比? , see the example with a flag how to place layers ,请参见带有标志的示例如何放置图层

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM