`
helpbs
  • 浏览: 1171739 次
文章分类
社区版块
存档分类
最新评论

Android Nine Patch图片及按钮背景

 
阅读更多

http://www.cnblogs.com/feisky/archive/2010/01/16/1649502.html


NinePatchDrawable 绘画的是一个可以伸缩的位图图像,Android会自动调整大小来容纳显示的内容。一个例子就是NinePatch为背景,使用标准的Android按钮,按钮必须伸缩来容纳长度变化的字符

NinePatchDrawable是一个标准的PNG图像,它包括额外的1个像素的边界,你必须保存它后缀为.9.png,并且保持到工程的res/drawable目录中。如果你是从APK解压后得到的*.9.png文件,注意它是已将周围的空白像素去掉了的,在使用时必须再加上。

这个边界是用来确定图像的可伸缩和静态区域。你可以在左边和上边的线上画一个或多个黑色的1个像素指出可伸缩的部分(你可以需要很多可伸缩部分),它的相对位置在可伸缩部分相同,所以大的部分总是很大的。

你还有可以在图像的右边和下边画一条可选的drawable区域(有效的,内边距线)。如果你的视图对象设置NinePath为背景然后指定特殊的视图字体,它将自行伸缩使所有的文本来适应根据右线与底部线设计好的区域(如果有的话),当然内边距线不包括其中,Android可以使用左边的线与上面的线来定义一个drawable区域。

我们来澄清一下这两条不同的线,左边跟顶部的线来定义哪些图像的像素允许在伸缩时被复制底部与右边的线用来定义一个相对位置内的图像,视图的内容就放入其中。

image

可以使用tools\draw9patch.bat工具来编辑*.9.png图片,可以看出该工具非常直观的展示了图片在上下或左右拉伸时的效果以及作为背景时其内容显示的位置。

image

作Nine Patch为按钮背景使用方法:

<Button id="@+id/big"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"

 android:text="text!"
 android:textSize="30sp"
 android:background="@drawable/my_button_background"/>

如果想要设置按钮在按下或者获得焦点时,有不同的背景图片,则可以将需要的背景图片放在drawable目录下(.9.png)格式,同时在drawable目录中新建一个xml文件(内容如下),android:background="@drawable/(XML文件名)",这样就可以在不同状态显示不同的背景。

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="true"
       android:drawable="@drawable/button_focused" />
    <item android:state_focused="false" android:state_pressed="true"
       android:drawable="@drawable/button_focused" />
    <item android:state_focused="true"
       android:drawable="@drawable/button_focused" />
    <item android:state_focused="false"
       android:drawable="@drawable/button_defocused" />
</selector>

分享到:
评论

相关推荐

    Nine-Patch素材.rar

    NinePatch图(9-Patch图,.9图)是一种可拉伸的图片。 该压缩包含有两种类型的Nine-Patch图片,一种是微信聊天背景,另一种是QQ聊天背景(海浪)。

    Nine-patch图片写聊天界面

    Nine-patch图片是无论怎样指定宽度都保持清晰,写聊天界面很合适

    9点素材制作工具Nine-Patch Editor1_0_0_3

    android 中的图片制作工具 9点素材制作工具Nine-Patch Editor1_0_0_3

    点九图片素材制作工具Nine-Patch

    自动去黑边,自动识别点九过的图片的处理信息,重新进行编辑。处理完的点九信息在查看时看不到边缘黑线,但是可以在手机上被正确识别,做手机界面设计等的可以来取了

    聊天气泡框 Nine-Patch图片

    仿微信,聊天气泡框 便于制作Nine-Patch图片,很实用,在网上找了很久,大家可以下载使用用于练习

    Drow9_安卓适配工具点9图制作

    在android中专门有一种叫nine patch图片(以 9.png结尾)来解决背景大小不一样时,只用一张背景图片。所以这个工具就是做Android适配点9图用的工具(文件为压缩包,附有使用教程,工具名称为:jdk-8u131-windows-x64)

    Android代码-android-actionbarstylegenerator

    It will generate all necessary nine patch assets plus associated XML drawables and styles which you can copy straight into your project. Use it online here: ...

    Android中各种图像资源的使用

    Android中各种图像资源的使用普通图像资源 XML图像资源 Nine-patch图像资源 XML Nine-patch图像资源 图层(Layer)图像资源 图像状态(state)资源 图像级别(Level)资源 淡入淡出(transition)资源 嵌入(Inset)...

    Nine

    Nine

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Working with Nine-Patch Stretchable Graphics 336 Using the “Working Square” Principle 336 Providing Alternative Application Resources 338 Understanding How Resources Are Resolved 338 Organizing ...

    Android移动应用设计与开发PPT教学课件.pptx

    * Draw Nine-patch * DDMS * sqlite3 * TraceView * logcat 综合案例 本节将提出一个大案例,主要包含该大案例的需求分析和概要设计。本案例是一个社交软件,主要包含用户个人资料、话题、私信、好友等功能。需求...

    Test_sub_int_2addr.rar_sub

    Nine Patch Test extends Android Test Case.

    sixty_nine_app_20230623_cj3d.apk

    sixty_nine_app_20230623_cj3d.apk

    sixty_nine_app_20230925_gjcp.apk

    sixty_nine_app_20230925_gjcp.apk

    Android代码-AndroidAssetStudio

    Android Asset Studio Open the Android Asset Studio See the older version if you're having trouble with the new version A web-based set of tools for generating graphics and other assets that would ...

    .9切图教程

    用自带的tools\draw9patch.bat 打开一张png图片,我们可以在png图片最外面的空格画一个像素宽的黑线。左边是编辑区,在左边的图中,左边黑线的高度决定了垂直拉升时的扩展区域,也 即当垂直拉伸时,只有这个区域的...

    Nine Algorithms That Changed the Future

    Nine Algorithms That Changed the Future高清英文版kindle电子书

    5nine.hyper-10.0.18264.rar

    5nine Manager for Hyper-V -10.0.18264.1 ,压缩包内有补丁,本工具仅于测试使用,lic要自己去5nine.com申请,可以安装在hyper-v 系统内部,直接查看虚拟机运行情况,不在需要hyper-v-tools远程连接才能看os运行资源...

    ninelevel.rar_NINE LEVEL INVERTER_nine level_the nine

    nine level inverter for the induction motor controller in three phase

Global site tag (gtag.js) - Google Analytics