Resize images in bitmap in xml

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:

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!

Answers


Just change

android:gravity="center"

To

android:gravity="fill"

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


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


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>

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>

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"


Need Your Help

How to position a fixed div under another fixed div?

html css css-position

I am trying to set a fixed div div(menu) directly under another fixed div (header) and the positioning should also work with different screen sizes.

Issues regarding SVN folders and good repository name

svn repository-design svn-repository

I am a little bit confused how SVN related settings should be configured assuming I want to use SVN on my local machine. They are as below: