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
297 views
in Technique[技术] by (71.8m points)

android - Unwanted stretching of nine-patch image for splash screen

I want to use a nine-patch image to make a splash screen for my application, which should look the same (square) in both portrait and landscape orientation.

My code is simple:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);
  // more code here...
}

My resource file is:

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

Nine-patch image looks fine in Draw 9-patch utility: enter image description here

But as the result the image is stretched: enter image description here

What's wrong? Can nine-patch images be used this way at all?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Can nine-patch image be used this way at all? Well, no. Not properly.

Because 9 patches are meant to be stretchable.
A nice tutorial: https://radleymarx.com/2011/simple-guide-to-9-patch

You'd better use a square image and use it as a source (android:src), not as a background (android:background) for your ImageView.
Then you can ajust it by setting the android:scaleType attribute (FIT_XY ?).

Reference: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html


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

...