2

I have an image with a resolution of 1024x1024 pixels.

original image

But, when I try to use this image in my Android app (background activity) it is squashed.

squashed image

Can anyone tell me how to avoid this?

Now this issue has been fixed by using following code:

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

But this solution can not use for following code:

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

Please help me.

3 Answers3

3

Don't set the image as the background of your root layout. Try this:

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

You can't set scale type in a RelativeLayout

zkminusck
  • 1,230
  • 1
  • 12
  • 23
  • I am trying to set the background image of my _RelativeLayout_. Can you suggest me how to do this for _RelativeLayout_. –  Aug 08 '15 at 09:31
  • Nope, RelativeLayout doesn't have scaleType attribute. – zkminusck Aug 08 '15 at 09:33
  • Actually I wanted to set background of the whole layout, that is why I am trying to set background image of my RelativeLayout. –  Aug 08 '15 at 09:37
  • I understand, that's why you have to add "match_parent" value as your ImageView width and hegiht, so it will be the background of your whole layout. See my code snippet above. – zkminusck Aug 08 '15 at 09:39
0

You have to set the Scale Type option of your ImageView to CENTER_CROP (see here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html)

you can do it programmatically using the setScaleType() method of ImageView or adding it to your xml layout using android:scaleType="centerCrop"

EDIT: If you want to add the background directly to your layout, you have to create a custom drawable object inside the /res/drawable directory. Try to use something like this:

<?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" />
SimoV8
  • 1,382
  • 1
  • 18
  • 32
  • I tried _android:scaleType="centerCrop"_ in my xml file. Ithink because I am not using ImageView, I am trying to set background of _RelativeLayout_. –  Aug 08 '15 at 09:33
0

Just use following code:

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

This should resolve your issue.

Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77