Resize images in bitmap in xml
Asked Answered
O

8

14

I am new to Android programming. Recently, I had started on a project and would like to make a splash screen for the Android app. I had read and successfully implemented the splash screen after following this tutorial. However, I realise that my app logo stretches out of my screen, like this:

Stretched out app logo

The following is my background_splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/splash_background" />

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/tap" />
    </item>
</layer-list>

I have tried

android:width="75sp"
android:height="75sp"

in <bitmap> but it does not work. Thus, I would like to know if there is a way to resize the image (without Java code, preferably) except for using an ImageView. Any help would be appreciated!

Opinionated answered 15/3, 2016 at 13:14 Comment(6)
android:scaleType="fitXY" try it.... and set your Imageview android:layout_width="match_parent" android:layout_height="match_parent" i think it worksRemedial
please set height and width of your layer-list...Remedial
@nandkishormewara I'll try it and see if it works ☺Opinionated
Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length) profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false)); try itRemedial
have you solve the problem...Remedial
Possible duplicate of #10707353Bluecoat
M
10

I faced the ditto same problem while designing splash screen for my app (followed the same link that you mentioned above and a few others) , and a bit of googling got me my answer.

The solution is simple and does not involve coding. ie. you do not need to specify height and width in your xml. As for why this is happening, your image resolution is higher than as specified below.

SOLUTION :

You need to provide image of various resolutions that map to various screen sizes. Following is a list of minimum screen size for various displays as provided by Google (all in pixels) :-


For Android Mobile Devices

LDPI- 426 x 320

MDPI- 470 x 320

HDPI- 640 x 480

XHDPI- 960 x 720


For Android Tablet Devices

LDPI- 200 x 320

MDPI- 320 x 480

HDPI- 480 x 800

XHDPI- 720 x 1280


So all you need to do is resize your image for all sizes, load the images in Drawable according to their sizes (Ex: drawable-hdpi contains the hdpi image) and pass it to the background_splash.xml (you can keep the same name ofcourse). Rebuild the project and run it. Should be as per your specification.

Note: Code does not need to be changed at all. For image resizing, you can use Adobe Photoshop (I find it easier to work with).

According to preference, you may also use 9 Patch image so that the image's border can stretch to fit the size of the screen without affecting the static area of the image.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

Maybellemayberry answered 11/2, 2017 at 15:48 Comment(0)
E
7

Just change

android:gravity="center"

To

android:gravity="fill"
Elope answered 26/2, 2017 at 14:20 Comment(2)
Which is the final result? Is this fitting the width/height by keeping blank spaces, is this actually cropping or it stretches the image?Bub
fill stretches the imageInstitutionalism
C
3

Bitmap does not allow resizing of image dimensions using gravity = "center" and take a separate icon with specific size for splash screen.

Corm answered 19/5, 2017 at 5:26 Comment(0)
P
3

Try to use item instead of bitmap.

Bitmap has no option to change its size.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/white"/> 

    <item
        android:width="200dp"
        android:height="200dp"
        android:drawable="@drawable/ic_launcher"
        android:gravity="center" />

</layer-list>
Proctology answered 9/8, 2020 at 6:35 Comment(1)
This requires API 23 and higher.Dome
L
1

There's more simple way to do it. In your launch_background.xml file, remove that bitmap tag and just add it like this

 <item
    android:width="200dp"
    android:height="200dp"
    android:drawable="@drawable/yourImagehere"
    android:gravity="center" />
Lilithe answered 22/7, 2020 at 11:23 Comment(0)
R
0

R you using ImageView try it I think it helps you...

ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
int width=200;
int height=200;
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);

Or check it

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="100dp"
 android:layout_height="100dp">
<item
    android:drawable="@drawable/splash_background" />

<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/tap" />
</item>
</layer-list>
Remedial answered 19/3, 2016 at 5:23 Comment(1)
No I am not using an ImageView. I'm using bitmap to display it directly. No imageView used.Opinionated
B
0

Try this dont use drawable

if you want change background color that give it to Relative layout, if you want full image set to your scree (best for splash screen) that use match_parent to image view

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:layout_centerInParent="true"
        android:src="@drawable/splash" />

</RelativeLayout>
Belovo answered 19/5, 2017 at 6:16 Comment(0)
B
-3

If you want to resize the bitmap in an item, you should not use bitmap To change the dimensions use directly item .. example:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/splash_background"/>
    <item
        android:src="@drawable/tap"
        android:gravity="center"
        android:width="150dp"
        android:height="150dp"/>
    </item>
</layer-list>

Bitmap does not allow resizing of image dimensions using gravity = "center"

Butterandeggs answered 18/3, 2017 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.