【问题标题】:Android image squashedAndroid 图像被压扁
【发布时间】:2015-08-08 09:08:09
【问题描述】:

我有一张分辨率为 1024x1024 像素的图像。

但是,当我尝试在我的 Android 应用(后台活动)中使用此图像时,它被压扁了。

谁能告诉我如何避免这种情况?

现在这个问题已经通过使用下面的代码解决了:

<RelativeLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:src="@drawable/image" />
</RelativeLayout>

但此解决方案不能用于以下代码:

<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_centerInParent="true"
        android:scaleType="centerCrop"
        android:background="@drawable/frontscreenbg"/>

    <ScrollView android:id="@+id/scrollviewParentLoginContainer"
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:wheel="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true">

        <LinearLayout
            android:id="@+id/linearLayoutLoginContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

请帮帮我。

【问题讨论】:

  • 您可以通过在后台使用ImageViewandroid:scaleType="centerCrop" 来实现此目的
  • 到底想要什么
  • @zkminusck 我是使用 ImageView 的机器人,我正在尝试设置 Relaytive Layout 的背景。
  • @Albert,您已经为多设备屏幕支持制作了各种图像,请查看:stackoverflow.com/questions/19875158/…
  • 我们没有其他选择吗?

标签: android image android-image android-background


【解决方案1】:

不要将图像设置为根布局的背景。试试这个:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/your_background_image"
        android:scaleType="centerCrop"/>
      ...
     /* Your other UI elements */
      ...
</RelativeLayout>

您不能在 RelativeLayout 中设置比例类型

【讨论】:

  • 我正在尝试设置我的 RelativeLayout 的背景图像。你能建议我如何为 RelativeLayout 执行此操作吗?
  • 不,RelativeLayout 没有 scaleType 属性。
  • 其实我想设置整个布局的背景,这就是为什么我试图设置我的RelativeLayout的背景图像。
  • 我明白了,这就是为什么您必须添加“match_parent”值作为您的 ImageView 宽度和高度,因此它将成为您整个布局的背景。参见我上面的代码 sn-p。
【解决方案2】:

您必须将 ImageView 的 Scale Type 选项设置为 CENTER_CROP(参见此处:http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

您可以使用 ImageView 的 setScaleType() 方法以编程方式执行此操作,也可以使用 android:scaleType="centerCrop" 将其添加到您的 xml 布局中

编辑: 如果要将背景直接添加到布局中,则必须在 /res/drawable 目录中创建自定义可绘制对象。尝试使用这样的东西:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background_image"
    android:tileMode="repeat" />

【讨论】:

  • 我在我的 xml 文件中尝试了 android:scaleType="centerCrop"。我想因为我没有使用 ImageView,所以我正在尝试设置 RelativeLayout 的背景。
【解决方案3】:

只需使用以下代码:

<RelativeLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:src="@drawable/image" />
</RelativeLayout>

这应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    相关资源
    最近更新 更多