android 9 原生图标,Android-.9/9Patch图标制作(AS)

.9这个东东个人之前很少用到。很少去做聊天的这种界面,所以用的并不多。用9patch文件的主要目的,个人认为主要是图片本身会做动态拉伸,当然不能是那种“花里胡哨”的图片,那种你做.9文件也没什么卵用呀-拉伸肯定会变形的。。。。

就比如这种聊天界面:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

我们重点关注下,红色部分:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

不管我们的文字有多少行,由于我们是设置的Text的background,所以文字行数将影响图片的宽度/高度。 所以正常情况下做拉伸会变形,我们看看变形情况:

看布局:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginBottom="10dp"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"

android:background="@mipmap/chat_bg_01"

android:paddingBottom="10dp"

android:paddingLeft="10dp"

android:paddingRight="10dp"

android:paddingTop="10dp"

android:text="你好你皮虾你皮皮虾虾你虾你好,皮皮虾你好,皮皮虾你好,皮皮虾你好,皮皮虾你好,皮皮虾你好,皮皮虾你好,皮皮虾你好,皮皮虾你好,皮皮虾你好,皮皮虾"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent" />

看图片:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

看文字不太多的效果:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

再看文字比较多的效果:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

尖尖的地方已经变形了哇。有木有......这就是为什么我们需要引入.9文件了....

而且你要设置padding啥的才能算是显示正常。。。

当然不用.9图片也是可以的。怎么做? 采用奇淫技巧“把尖尖单独抠图,然后布局在聊天背景的左上角部分,刚刚覆盖一点点 - 貌似也可以保证不变形。 But, 如果你的背景不是像这种纯色,而是多姿多彩的,调试起来就麻烦了。还是别闹了....”

那步入正题吧。。简单了解下.9后我们就以个人理解最简单的方式制作....

随便粘贴点概念吧....哼。。

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

总得理解个人认为是两点:

1。 可以设置拉伸区域,进而某些区域不参与拉伸,进而不出现拉伸情况

2。 可以设置内容显示区域 - 假使图片底部有只“小狗”, 你可以设置内容显示在“小狗”下面。 以上设置都可以替代TextView的padding效果,甚至比padding还能更好的控制效果...

Now,let's start it....

我们就拿ic_luncher.png来说吧。。

1.首先选中这张图片,然后点击create p-patch file。我们把它保存到drawable下面吧,然后改个名哈:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

我的目的就是,黑色区域不变形, 红色区域显示文字,而且这个文字要保持在机器人的下面:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

2.网上有很多关于.9制作的文章,有的是利用工具做的,其实AS还是蛮好的。有的会讲很多原理啥的。。如果你比较着急,想要快速通过尝试来了解,可能看起那些文章相对有点费劲。我们其实只需要了解制作的一点基础概念就可以上手了,然后出问题了再调试调试,这样应该还是可以能比较快的搞定!

当你把鼠标点击该.9文件时,可以发现几条细细的黑线, 我们就利用这个线来拖动产生黑边?后面慢慢道来...

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

2.1 然后了解一个概念就可以实现显示区域控制了:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

2.2 了解上面目前足矣。我们这就开干可好?

2.2.1 首先我们把显示区域设置下看看是否显示区域已经被我们安排了?

先代码 - 为了让你们相信,我特意加了一个不带.9图片背景的文本控件,方便对比哈...

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="10dp"

android:background="@drawable/ic_launcher_sb"

android:text="你好你"

android:gravity="left|top"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:text="你好你"

android:gravity="left|bottom"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent" />

我们实际做一下:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

看效果 - 没毛病吧?

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

2.2.2 But, 这是我们图片本身不做缩放,或者说文本本身没有太多(太多自然也就会导致图片缩放,一个说法)。。。

现在文字多点看看妮(顺便测试的控件什么的都给去掉吧)

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="10dp"

android:background="@drawable/ic_launcher_sb"

android:text="你好你你好你你好你你好你你好你你好你"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent" />

你已经可以发现这个时候文字已经不可能在我们圈定的范围内了。。因为文字太多了, 图片拉伸了已经导致我们的范围失效了,唯有左右拉伸方可显示....能撑到图片撑不动为止....

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

3. 这个时候我们该考虑做拉伸区域处理了...

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

我们实际做一下:

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

3.1 此时我们再来看效果 - 此时不管怎么拉伸都能保证文字区域,同时机器人也不会变形。而且保持在“右侧”;

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

3.2 当然如果你拉伸区域设置的是右侧,自然机器人就在左侧

来,我们不厌其烦的操作一下

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

左右拉伸区域设置到右侧

b3d1829df39e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

左右拉伸时只有右侧区域拉伸,左侧不参与拉伸

4. 其实到这里,我想大家或者说我应该明白一些事情了。内容区域、拉伸区域:内容区域靠右侧黑边线,底部黑边线共同决定, 左侧黑边线决定上下拉伸区域,顶部黑边线决定左右拉伸区域。 简单操作操作应该能基本使用了。。 至于说像qq那样的聊天的背景,我想应该问题不大了....

肯定建议说做简单纯色背景那种, 不要太花里胡哨....当然有那个需求也行。 尽量满足......9文件以前确实很陌生....慢慢来补吧,不慌....

对了。后面一有空,我会补一下做项目的知识,同时纠结一些之前的文章的问题(之前确实没有问题,不过随着项目版本更新和新项目,发现了一些问题。想特别再新增一篇文章来说明下: 一个是推送进入详情的问题, 一个是关于键盘隐藏显示的问题...)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值