Android图像压缩

Android图像压缩,android,image,android-image,android-background,Android,Image,Android Image,Android Background,我有一张分辨率为1024x1024像素的图像 但是,当我尝试在我的Android应用程序(后台活动)中使用此图像时,它被压扁了 有人能告诉我如何避免这种情况吗 现在,已使用以下代码修复此问题: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_wid

我有一张分辨率为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">
<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"
    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">
<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>


请帮助我。

您必须将ImageView的“缩放类型”选项设置为“居中裁剪”(请参见此处:)

您可以使用ImageView的
setScaleType()
方法以编程方式执行此操作,或者使用
android:scaleType=“centerCrop”

编辑: 如果要将背景直接添加到布局中,则必须在/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" />

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

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

...
/*您的其他UI元素*/
...

您不能在
RelativeLayout

中设置刻度类型,只需使用以下代码:

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


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

您可以通过使用
android:scaleType=“centerCrop”
在后台放置一个
ImageView
来实现这一点want@zkminusck我正在使用ImageView,我正在尝试设置Relaytive布局的背景。@Albert,您已经为多设备屏幕支持制作了各种图像。请检查:我们没有其他选项吗?我正在尝试设置我的RelativeLayout的背景图像。你能建议我如何为RelativeLayout这样做吗?不,RelativeLayout没有scaleType属性。实际上我想设置整个布局的背景,这就是为什么我要设置RelativeLayout的背景图像。我明白,这就是为什么你必须添加“match_parent”值作为你的ImageView宽度和hegiht,因此,它将成为整个布局的背景。请参阅上面的代码片段。我在xml文件中尝试了android:scaleType=“centerCrop”。我想因为我没有使用ImageView,所以我正在尝试设置RelativeLayout的背景。