Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...