9 Patch image where center is 1:1

ぃ、小莉子 提交于 2019-11-29 06:12:55

Here is an example of how you can do this. This is a 9patch, so save it like this: yourname.9.png and don't forget to set android:scaleType="fitXY" on your ImageView

You might want to mark 4 strech position See the following X mark, just put a dot instead of them. And use (if not already done) the 9path tool inside the SDK folder to have a preview of what will be generated by Android SDK

    X       X
  ************
X ************
  ************
  ****LOGO****
  ************
X ************
  ************

You have two solutions:

  1. Set the splash to a RelativeLayout with a background of a blue color (that you are using). Add to this RelativeLayout a centered image.
  2. Use a 9-patch the right way. You need 4 patches on each of the corners:

if it's just a logo in the center of a background color, you can try out this approach:

no need for 9-patch. Just use a normal image (called "splash_logo" here), and wrap it by creating a new drawable xml file (inside res/drawable) :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <color android:color="@color/default_background" />
    </item>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/splash_logo" >
        </bitmap>
    </item>

</layer-list>

use this drawable on the imageView you wish to use.

If it's a full screen, you can go further and use a theme for the first activity, thus removing the need for any view to be created :

in styles.xml (or theme.xml) :

<style name="SplashTheme" parent="@android:style/Theme.NoTitleBar">
    <!-- Background color must match splash image borders ! -->
    <item name="android:windowBackground">@drawable/activity_splash</item>
    <item name="android:background">@null</item>
</style>

and in the manifest:

    <activity
        android:name="..."
        android:theme="@style/SplashTheme" >

You indicate which parts of a 9-patch image are stretched with the pixels in the top and left border. So just fill the pixels near the edges.

The 9-patch is not used to this kind of rendering. (http://developer.android.com/tools/help/draw9patch.html) But you do as the code mentioned below, using a and centralizing using gravity.

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center" >

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:src="@drawable/your_logo" />

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!