【问题标题】:How to fill image in background without stretching?如何在不拉伸的情况下在背景中填充图像?
【发布时间】:2017-05-24 18:00:06
【问题描述】:

我想从我的图像中获得以下类型的背景:

这是不是重复的问题,因为我尝试了以下解决方案:

1) First one 2)Second 3)third

这些答案建议使用图像视图的以下属性:

android:scaleType="..."

我已经为此尝试了每个可用的值。我得到的是:

如果我使用android:scaleType="center",或者在中心有一个非常小的图像。

以下是我为此活动的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.georeminder.MainActivity">


<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/view"
    android:src="@mipmap/landscape"
    android:scaleType="centerCrop" />

</RelativeLayout>

那么我在这里错过了什么?任何帮助将不胜感激!谢谢

编辑

我也尝试过以下组合:

android:scaleType="fitXY"
android:adjustViewBounds="true"

【问题讨论】:

  • 宽度和高度不要使用match_parent,而是使用wrap_content
  • 然后它在左上角显示非常小的图像。
  • 这是因为图片尺寸(宽高)小于屏幕尺寸。您需要一个更大尺寸的图片图像文件。
  • 不!图像尺寸为 4288*2848 像素。大小为 1.45 MB
  • 您是在布局设计器上还是在设备上看到这个?此外,您应该将图像放置在 Res/drawable 中,而不是 Res/mipmap,mipmap 仅适用于应用启动器图标。

标签: android xml image android-layout


【解决方案1】:

将您的图像文件放在 Res/drawable 中,而不是 Res/mipmap。

Res/mipmap 仅适用于应用启动器图标

试试这个:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/view"
    android:src="@drawable/landscape"
    android:scaleType="center" />

【讨论】:

  • 是的,只是将这张图片替换为drawable文件夹,但结果是一样的。
  • @Kaushal28 更新
【解决方案2】:

使用 fitxy 并使用 adjustviewbond=true

【讨论】:

    【解决方案3】:

    在您的图像视图中使用 centerCrop 和 centerInParent 设置 scaleType

        <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:id="@+id/view"
        android:src="@mipmap/landscape"       
        />
    

    【讨论】:

    【解决方案4】:

    你可以使用 FrameLayout 和 ImageView 作为背景

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    
    <ImageView
        android:id="@+id/imgPlaylistItemBg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:maxHeight="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/img_dsh" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
    </FrameLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      相关资源
      最近更新 更多