Android图像被压扁了

时间:2015-08-08 09:08:10

标签: android image android-image android-background

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

original image

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

squashed image

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

现在使用以下代码解决了这个问题:

<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">

请帮帮我。

3 个答案:

答案 0 :(得分: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

中设置比例类型

答案 1 :(得分:0)

您必须将ImageView的“缩放类型”选项设置为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" />

答案 2 :(得分:0)

只需使用以下代码:

<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>

这可以解决您的问题。