17人参与 • 2025-04-24 • Android
文件位置:res/drawable/
或 res/drawable-anydpi/
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportwidth="24.0" android:viewportheight="24.0"> <path android:fillcolor="#ff000000" android:pathdata="m12,2c6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10s17.52,2 12,2z"/> </vector>
关键属性:
width/height
:矢量图的固有尺寸viewportwidth/viewportheight
:画布的逻辑尺寸pathdata
:定义形状的路径数据(svg 格式)fillcolor
:填充颜色strokecolor
:描边颜色strokewidth
:描边宽度优点:
文件位置:res/drawable/
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="8dp" /> <gradient android:angle="45" android:startcolor="#ff6200ee" android:endcolor="#ff03dac5" android:type="linear" /> <stroke android:width="2dp" android:color="#ffffff" android:dashwidth="4dp" android:dashgap="2dp" /> <size android:width="200dp" android:height="100dp" /> </shape>
形状类型:
rectangle
(默认):矩形oval
:椭圆line
:水平线ring
:环形子元素详解:
<corners>
:圆角
radius
:统一圆角半径topleftradius
等:各角单独设置<gradient>
:渐变type
:linear(线性)/radial(径向)/sweep(扫描)centerx/y
:渐变中心点(0-1)gradientradius
:径向渐变半径<stroke>
:描边dashwidth
:虚线段的长度dashgap
:虚线间隔<solid>
:纯色填充<padding>
:内边距<size>
:固有尺寸<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 底层阴影 --> <item android:top="4dp" android:left="4dp"> <shape android:shape="rectangle"> <solid android:color="#33000000" /> <corners android:radius="8dp" /> </shape> </item> <!-- 上层内容 --> <item> <shape android:shape="rectangle"> <solid android:color="#ff6200ee" /> <corners android:radius="8dp" /> </shape> </item> </layer-list>
特点:
<item>
可以设置偏移量(left/right/top/bottom)<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="#ff3700b3" /> <corners android:radius="8dp" /> </shape> </item> <!-- 默认状态 --> <item> <shape android:shape="rectangle"> <solid android:color="#ff6200ee" /> <corners android:radius="8dp" /> </shape> </item> </selector>
常用状态属性:
state_pressed
:按下状态state_focused
:获得焦点state_selected
:选中状态state_enabled
:启用状态(false 表示禁用)state_checked
:勾选状态(用于 checkbox 等)state_activated
:激活状态重要规则:
组成:
示例:
<!-- res/drawable/avd_heart.xml --> <animated-vector xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/ic_heart"> <target android:name="heart" android:animation="@animator/heart_beat" /> </animated-vector>
<!-- res/drawable/ic_heart.xml --> <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportwidth="24.0" android:viewportheight="24.0"> <path android:name="heart" android:pathdata="m12,21.35l-1.45,-1.32c5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09c13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54l12,21.35z" android:fillcolor="#ff0000" /> </vector>
<!-- res/animator/heart_beat.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectanimator android:propertyname="scalex" android:duration="300" android:valuefrom="1" android:valueto="1.2" android:valuetype="floattype" android:repeatcount="1" android:repeatmode="reverse" /> <objectanimator android:propertyname="scaley" android:duration="300" android:valuefrom="1" android:valueto="1.2" android:valuetype="floattype" android:repeatcount="1" android:repeatmode="reverse" /> </set>
使用方法:
imagebutton heartbutton = findviewbyid(r.id.heart_button); animatedvectordrawablecompat avd = animatedvectordrawablecompat.create( this, r.drawable.avd_heart); heartbutton.setimagedrawable(avd); avd.start();
// 修改矢量图颜色 vectordrawablecompat vector = vectordrawablecompat.create( getresources(), r.drawable.ic_vector, gettheme()); vector.settint(contextcompat.getcolor(this, r.color.new_color)); // 修改形状圆角 gradientdrawable shape = (gradientdrawable) view.getbackground(); shape.setcornerradii(new float[]{ 0, 0, // 左上 20, 20, // 右上 40, 40, // 右下 0, 0 // 左下 });
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="?attr/colorprimary" /> <stroke android:width="1dp" android:color="?attr/colorprimarydark" /> </shape>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportwidth="24.0" android:viewportheight="24.0"> <group android:name="rotationgroup" android:pivotx="12" android:pivoty="12" android:rotation="0"> <path android:name="v" android:fillcolor="#000000" android:pathdata="m12,2c6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10s17.52,2 12,2z" /> </group> </vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportwidth="24.0" android:viewportheight="24.0"> <path android:name="circle" android:pathdata="m12,2c6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10s17.52,2 12,2z" android:fillcolor="#4caf50" /> <path android:name="check" android:pathdata="m9,16.17l4.83,12l-1.42,1.41l9,19 21,7l-1.41,-1.41z" android:fillcolor="#ffffff" /> </vector>
命名规范:
btn_[状态]_[颜色]
(如 btn_pressed_primary
)ic_[名称]
(如 ic_search
)bg_[描述]
(如 bg_rounded_corner
)性能优化:
适配技巧:
drawable-night/
)drawable-v21/
)工具推荐:
通过合理使用这些 xml drawable 资源,你可以创建出既美观又高效的界面元素,同时保持应用的轻量化和可维护性。
九宫格图片(ninepatch drawable,文件扩展名为 .9.png
)是 android 平台上一种特殊的 png 图片格式,它能够智能地拉伸图片而不会使边角变形。
九宫格图片将一张图片划分为 9 个部分:
.9.png
步骤:
res/drawable
目录new
→ image asset
asset type
中选择 nine-patch
.9.png
格式+---------------------+ | 上边:水平拉伸区域 | | 左边:垂直拉伸区域 | | 右边:垂直内容区域 | | 下边:水平内容区域 | +---------------------+
边缘 | 作用 | 标记规则 |
---|---|---|
上边 | 定义水平拉伸区域 | 黑色像素表示可拉伸部分,透明表示不拉伸 |
左边 | 定义垂直拉伸区域 | 黑色像素表示可拉伸部分,透明表示不拉伸 |
右边 | 定义垂直内容区域(内边距) | 黑色像素表示内容边界 |
下边 | 定义水平内容区域(内边距) | 黑色像素表示内容边界 |
+-------------------------+ | 1px 黑边标记区域 | | | | +---------------+ | | | 角区域(不拉伸) | | | | 边区域(拉伸) | | | | 中心区域(拉伸) | | | +---------------+ | | | +-------------------------+
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_background" />
其中 button_background.9.png
是九宫格图片。
// 获取九宫格图片 ninepatchdrawable npd = (ninepatchdrawable) getresources() .getdrawable(r.drawable.button_background); // 修改颜色滤镜 npd.setcolorfilter(new porterduffcolorfilter( color.red, porterduff.mode.src_in)); // 应用到视图 button.setbackground(npd);
// 加载位图 bitmap bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.button_background); // 获取 ninepatch 块数据 byte[] chunk = bitmap.getninepatchchunk(); // 创建 ninepatchdrawable ninepatchdrawable npd = new ninepatchdrawable( getresources(), bitmap, chunk, new rect(), null);
_9
后缀,如 btn_normal_9.png
特性 | 九宫格图片 | 普通 png |
---|---|---|
拉伸方式 | 智能拉伸 | 整体拉伸 |
文件大小 | 稍大 | 较小 |
边角保护 | 保持原样 | 会变形 |
适用场景 | 按钮/背景 | 图标/图片 |
制作复杂度 | 较高 | 低 |
九宫格图片是 android 开发中处理可拉伸背景的强大工具,合理使用可以显著提升应用的界面适配性和视觉效果。
以上就是android drawable目录下的xml图形文件详解的详细内容,更多关于android drawable xml图形文件的资料请关注代码网其它相关文章!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论