Android apk压缩技术(一)——巧用shape

android中通常需要显示通知角标、比如显示数量的角标、以及显示未读信息数量、信息等,如下图就是一个典型的例子

但是如果每个数字都叫美工做一遍无疑是不现实的(除非这里需要“自定义字体”)

所以下面来讲述一下如何适应shape来实现上述图片

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false" >
    <solid
        android:color="#f00" />
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp" />
    <solid
        android:color="#f00" />
    <stroke
        android:width="10px"
        android:color="#000"
        android:dashWidth="20dp"
        android:dashGap="10dp"/>
    <size
        android:width="300dp"
        android:height="300dp" />
    <gradient
        android:startColor="#b77"
        android:endColor="#f00"
        android:angle="225" />
</shape>

讲解一下:

(1)android:shape="oval" Defines the type of shape. Valid values are "rectangle" "oval" "line" "ring"
意思就是当前shape的形状,这里设置为椭圆形(圆形)

(2)android:useLevel Boolean,if this is used as a LevelListDrawable
指的是是否使用LevelListDrawable。LeveListDrawable管理着一组交替的drawable资源,用level-list标签的xml来说实现,然后用imageView.getDrawable().setLevel(i)来改变当前drawable资源。

(3)<solid> A solid color to fill the shape.
指的是图形填充的颜色

(4)<stroke> A stroke line for the shape.
指的是图形轮廓的颜色android:dashWidth表示虚线宽度android:dashGap表示虚线之间的空隙(间隔)

(5)<gradient> Specifies a gradient color for the shape. 
指定一个渐变颜色的形状

layout的布局是这样的

<span style="font-size: 18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aaa">
    <TextView
        android:layout_centerInParent="true"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:gravity="center"
        android:textSize="220sp"
        android:textColor="#fff"
        android:background="@drawable/test_shape"
        android:text="7"/>
</RelativeLayout></span></span>

最终得出如下结果图:



总结:

使用shape的优点:

(1)减少安装包的体积。

(2)提高加载速度。android中解析xml并绘制的速度比直接绘制图片的速度要快



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值