简体   繁体   English

如何放大图像并将其居中放置在容器底部?

[英]How can I scale an image up and centre it at the bottom of its container?

I'm trying to make a very simple layout with the following properties: 我正在尝试使用以下属性制作一个非常简单的布局:

  • Just contains a single image that should be: 仅包含一个图像,应为:
    • Scaled up as much as possible while maintaining its original aspect ratio 在保持其原始长宽比的同时尽可能地放大
    • Horizontally centred 水平居中
    • At the bottom of its container 在其容器的底部

Here's an example of what it should look like: 这是它的外观示例:

纵向视图,显示图像居中于底部但按比例放大横向视图显示图像居中于底部但按比例放大

I can get it to scale up and centre horizontally and vertically by setting the image to match_parent width and height and specifying scaleType of fitCentre . 通过将图像设置为match_parent宽度和高度并指定scaleTypefitCentre我可以使其在水平和垂直方向上进行缩放和居中。 I could also get it to align to the bottom-right by using fitEnd . 我还可以通过使用fitEnd与右下角对齐。

I tried using RelativeLayout to centre it at the bottom, but I could only get this to work if I set the image's width and height to wrapContent , which caused it to not scale up. 我尝试使用RelativeLayout将其居中放置在底部,但是如果将图像的宽度和高度设置为wrapContent ,这将导致它无法按比例wrapContent ,那么我只能使其工作。

Here's a sample layout to use as a starting point: 这是一个示例布局,可以用作起点:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom|center_horizontal"
        android:scaleType="fitStart"
        android:src="@drawable/abc_ic_search" />

</RelativeLayout>

Try this: 尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/abc_ic_search"
            android:adjustViewBounds="true"
            android:layout_alignParentBottom="true"/>

</RelativeLayout>

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

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