Android应用中res/drawable文件夹下定义的各种xml文件

一:Selector 用法

Android中的Selector主要是用来改变ListView和Button控件的默认背景。

属性介绍:

    <!--选中-->
    android:state_selected

    <!--获得焦点-->
    android:state_focused

    <!--点击-->
    android:state_pressed

    <!--设置是否响应事件,指所有事件-->
    android:state_enabled

根据项目需求,在其内部定义为自己想要的样式了,主要属性如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--默认时的背景图片-->
    <item android:drawable="@drawable/bg_up"/>
    <!--没有焦点时的背景图片-->
    <item android:state_window_focused="false" android:drawable="@drawable/bg_up"/>
    <!--非触摸模式下获得焦点并单击时的背景图片-->
    <item android:state_focused="true" android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/bg_down"/>
    <!--触摸模式下单击时的背景图片-->
    <item android:state_focused="false" android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/bg_down"/>
    <!--选中时的背景图片-->
    <item android:state_selected="true" android:state_enabled="true" android:drawable="@drawable/bg_down"/>
    <!--获得焦点时的背景图片-->
    <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/bg_down"/>
</selector>

二:Shape用法

通常用于自定义控件背景,圆角、渐变、填充等静态背景效果。可以用这种方法定义的图形,尽量选用这种方式,可以减小apk包大小。

属性介绍:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--背景填充色-->
    <solid android:color="#8AD341"/>
    <!--圆角-->
    <corners android:radius="20dp"/>
    <!--描边-->
    <stroke
        android:color="#382F2F"   //描边的颜色
        android:width="1dp"/>     //描边的宽度
    <!--内边距-->
    <padding
        android:right="10dp"
        android:left="10dp"
        android:top="20dp"
        android:bottom="20dp"/>
    <!--渐变-->
    <gradient
        android:type=["linear" | "radial" | "sweep"]   //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变;
        android:angle="90"  //渐变角度,必须为45的倍数,0为从左到右,90为从上到下;
        android:centerX="0.5"  //渐变中心X的相当位置,范围为0~1;
        android:centerY="0.5"  //渐变中心Y的相当位置,范围为0~1;
        android:startColor="#24e9f2"  //渐变开始点的颜色;
        android:centerColor="#2564ef" //渐变中间点的颜色,在开始与结束点之间;
        android:endColor="#25f1ef"  //渐变结束点的颜色;
        android:gradientRadius="5dp"  //渐变的半径,只有当渐变类型为radial时才能使用;
        android:useLevel="false" />   //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果。
</shape>

// 只想要一边描边的效果
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:left="-2dp"
    android:right="-2dp"
    android:bottom="-2dp">
    <shape>
        <stroke
            android:width="1dp"
            android:color="#80646F9A"/>
    </shape>
</item>
</layer-list>

三:animation-list

通常用于实现帧动画

属性介绍:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">   //oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
    <item android:drawable="@drawable/a" android:duration="150"/>  // android:duration 表示展示所用的该图片的时间长度
    <item android:drawable="@drawable/b" android:duration="150"/>
    <item android:drawable="@drawable/c" android:duration="150"/>
    <item android:drawable="@drawable/d" android:duration="150"/>
</animation-list>

四:bitmap

BitmapDrawable 是对bitmap的一种包装,实际开发中,可以直接引用原始的图片即可,但是也可以通过XML方式来描述,通过XML来描述的BitmapDrawable可以设置更多的效果。

属性介绍:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android">
    android:src="@drawable/drawable_resource"
    
    // 是否开启抗锯齿。开启后图片会变得更平滑些,因此一般建议开启,设置为true即可。
    android:antialias=["true" | "false"]
    
    // 是否允许抖动,如果位图与屏幕的像素配置不同时,开启这个选项可以让高质量的图片在低质量的屏幕上保持较好的显示效果
    android:dither=["true" | "false"]
    
    // 是否允许对位图进行滤波。当图片被压缩或者拉伸时,使用滤波可以获得平滑的外观效果。一般建议开启,为true即可
    android:filter=["true" | "false"]
    
    // 当图片小于容器尺寸时,设置此选项可以对图片经典定位,不同选项可以使用‘|’来组合使用。
    android:gravity
    可选项:
        top   // 将图片放在容器顶部,不改变图片大小
        bottom    // 将图片放在容器底部,不改变图片大小
        left  // 将图片放在容器左侧,不改变图片大小
        right // 将图片放在容器右侧,不改变图片大小
        center_vertical   // 图片竖直居中,不改变图片大小
        fill_vertical // 图片竖直方向填充容器
        center_horizontal // 图片水平居中,不改变图片大小
        fill_horizontal   // 图片水平方向填充容器
        center    // 使图片在水平方向和竖直方向同时居中,不改变图片大小
        fill  // 图片填充容器,默认值
        clip_vertical   // 竖直方向剪切,很少使用
        clip_horizontal   // 水平方向剪切,很少使用
    
    //平铺模式
    android:tileMode
    可选项:
        disabled  // 默认值,表示不使用平铺
        clamp  // 复制边缘色彩
        repeat  // X、Y 轴进行重复图片显示,也就是我们说要说的平铺
        mirror  // 在水平和垂直方向上使用交替镜像的方式重复图片的绘制
</bitmap>

五:rotate

用于实现图片旋转

属性介绍:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/a"   // 需要进行旋转动画的图片
    android:fromDegrees="0"    // 旋转的起始点(旋转开始的角度)
    android:pivotX="50%"         // 旋转点的X值(距离左侧的偏移量)
    android:pivotY="50%"         // 旋转点的Y值(距离顶部的偏移量)
    android:toDegrees="180"      // 旋转的结束点(旋转最终角度)
    android:visible="true"       // 图片初始的显示状态
    android:interpolator="LinearInterpolator"   // 设置转动速率,LinearInterpolator为匀速效果,Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果
    android:repeatCount="1"   // 重复的次数,默认为0,必须是int,可以为-1表示不停止
    android:duration="100"    // 表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度
    android:startOffset="10"  // 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行
    android:repeatMode="restart"    // 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行
    android:detachWallpaper="true"  // 是否在壁纸上运行
    android:zAdjustment="normal">    // 被animated的内容在运行时在z轴上的位置
</rotate>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qxnedy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值