Image doesn't scale properly in Android. - android

I'm using a xml drawable:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/splashscreen"
android:tileMode="disabled" android:gravity="top" >
</bitmap>
this is my imageView in the layout:
<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">
<ImageView
android:id="#+id/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
/>
</RelativeLayout>
but no matter what I do, it doesn't fit the screen, and only appears in the centre as a small image. Where am I going wrong?

Maybe not the best solution since different devices has different resolutions, but "wrap_content" will display the image in its original resolution.
<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"
>
<ImageView
android:id="#+id/splash_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/splashscreen"
/>
</RelativeLayout>

Try using fill_parent
Each device have different screen size so you need to make different splash image for each size and resolution i guess the problem is you are using image of small size(height width) and the display is big
so you need to scale image to fit to display size, this might pixelate the image

Related

How to make an image of splashscreen fit all mobile screen sizes?

The image I used for my splash screen with (gravity:center) doesn't fit my mobile screen, even if I changed gravity to fill.
<item>
<bitmap
android:gravity="fill"
android:src="#drawable/drawable"/>
</item>
Normally I used common single image for all kind of devices, which size is 720px*1280px.
Then in my splash_activity.xml i usually used it like following way:
<LinearLayout 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"
android:layout_gravity="center"
android:background="#drawable/bg_splash"
android:gravity="center"
android:orientation="vertical" />
use the bitmap in the XML file as a background
android:background="#drawable/bg_splash"
Use FrameLayout as your Root
Put an ImageView in it and set ScaleType to fitXY and set adjustViewBounds to true
So change your layout like below:
<FramLayout 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"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/bg_splash"
/>
</FrameLayout>

How to resize Image to button?

So I have an ImageButton but the image is a very big fit for it. I tried android:scaleType="center" and it failed because the image centered but it was too small and tried fitXY but same result.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f1e8e8"
tools:context="com.example.bassam.myapplication123.MainActivity">
<ImageButton android:id="#+id/myButton"
android:layout_width="match_parent"
android:layout_height="150dp"
android:padding="100dip"
android:background="#ffffff"
android:src="#drawable/Work"
android:scaleType="fitXY"
></ImageButton>
</RelativeLayout>
If how I understand your concern is correct, you simply just want to have the image scale to the size of the ImageButton itself, simply use
android:scaleType="fitCenter"
The image is adjusted depending on the size of the ImageButton. Try it out and let me know if it works. Cheers! :)

How to set the ScaleType on a LinearLayout's background image

I have a linear layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#drawable/bgapp" >
What I need now is the possibility to set the background image ScaleType value in order to fill the screen everytime, this because I'm going to download the image and then I'm going to set it as background but without ScaleType the images will not keep it's aspect ratio based on the screen densities.
What I found is this solution:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/bgapp"
android:scaleType="centerCrop"
android:gravity="bottom|left" />
But It doesn't work as expected.. Is there a way to set a LinearLayout scaleType property or do I need to change my layout?
View's background is stretched depending on the size of the View.
Image scaling is supported by ImageView, and to use it together with the LinearLayout you need an additional container, that will overlay the 2 children (e.g. a FrameLayout):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:src="#drawable/bgapp"
android:scaleType="centerCrop"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
...
</LinearLayout>
</FrameLayout>

Bitmap gravity set to center not filling the screen even though the image is big enough

I have a RelativeLayout in my Android project. This has it's background set to a Bitmap:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="#drawable/splash2" />
splash2 is a PNG image sized 2560x1440 pixels. I haven't set the background of the layout directly to the image, because the default scale mode (or gravity) is fill which stretches the image to fit the screen. With center it should take correct size image from the center and show it unscaled. In case of a vertical 1080x1920 screen, it should take that big piece and center it in the layout.
However, I have a problem. The image is bigger than any screen out in the market today. Still, with my Nexus 7, which has a 1920x1080 screen, it has borders around the image. The layout is set to full screen. The image is shrinked vertically.
How do I fix this?
You can use android:gravity="fill" to cover vertical & horizontal direction
For splash image try gravity="center|fill"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/black" />
<item>
<bitmap
android:gravity="center|fill"
android:src="#drawable/fondo" />
</item>
</layer-list>
ImageView's scale type centerCrop was what I wanted. Unfortunately I couldn't specify this property for bitmaps. I changed my splash screen layout to FrameLayout and added an ImageView and TextView overlapping each other. This way I was able to achieve what I wanted.
<FrameLayout 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=".SplashScreen"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/splash2"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/roadSignName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#FFF"
android:gravity="center_vertical|center_horizontal"
android:padding="10dp"
android:text="My program"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="40sp" />
</RelativeLayout>
</FrameLayout>
Try adding this to the bitmap:
android:gravity="fill_vertical"
This should fix it.
P.S. Sorry for the first answer, I edited it now.

Improper Header Placement in Layout

I have a large header image that I'd like to use for both tablet devices and phones. The image looks fine on a tablet but when viewed on a phone it looks like this:
Here is my layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/searchresults_header" />
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
How can I get this header image to scale correctly on both screen sizes?
Your image is getting resized automatically, but the viewbounds stay as they were originally.
Add this to your ImageView and you should see the difference:
android:adjustViewBounds="true"
Can't remember which one but try these scaleType param values

Categories

Resources