Android 中使用xml定义各种Drawable

1,bitmap定义可以任意改变颜色的图片
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:autoMirrored="true"
    android:src="@drawable/ic_settings_bluetooth_alpha"
    android:tint="#ff0000">
</bitmap>

蓝牙图片
上面将图片改为红色,改为其他颜色只需设置tint属性,如果引用一个颜色值就可以一键改变所有图片颜色

2,shape 定义各种图形的图片
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring">
    <!--rectangle 矩形 ,oval 椭圆,line 横线,ring 圆环-->
    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"
        android:radius="20dp" />
    <!--angle 为45的倍数 0表示从左到右,90表示从下到上-->
    <!--gradientRadius 渐变半径 仅当tyoe =radial时有效-->
    <!--type 渐变类别 :线性渐变linear ;扩散渐变(以一个点向外扩散) radial ;扫描渐变(以一个直线一端为原点旋转扩散) sweep-->
    <gradient
        android:angle="0"
        android:centerColor="#00ff00"
        android:centerX="0dp"
        android:centerY="0dp"
        android:endColor="#0000ff"
        android:gradientRadius="50dp"
        android:startColor="#ff0000"
        android:type="radial" />
    <!--width 边界的宽 ;color 边界颜色;dashGap 边界中间长度;dashWidth 边界实线宽 -->
    <stroke
        android:width="15dp"
        android:color="#00ff00"
        android:dashGap="20dp"
        android:dashWidth="10dp" />
    <solid android:color="#ff0000" />
    <size
        android:width="20dp"
        android:height="30dp" />
    <padding
        android:bottom="20dp"
        android:left="20dp"
        android:right="20dp"
        android:top="20dp" />
</shape>

shape可以定义: rectangle 矩形 ,oval 椭圆,line 横线,ring 圆环

3,layer-list叠加的图形

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="1dp"
        android:drawable="@drawable/shape_bitmap"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">

    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ff0000" />
        </shape>
    </item>
</layer-list>

每一个item就是一个Drawable ,后面的叠加前面的,如定义Edittext的背景

4,selector 状态图形

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_bitmap" android:state_pressed="true">

    </item>
    <item android:drawable="@drawable/shape_bitmap" android:state_pressed="false">

    </item>
</selector>

每一个item对应一个状态,如 按下,选中,可用 常用于 按钮的点击效果等

还有其他一堆drawable

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值