美文网首页安卓
Android 项目中资源文件 -- shape篇

Android 项目中资源文件 -- shape篇

作者: __Witness | 来源:发表于2020-06-12 17:38 被阅读0次

样式篇:
Android 项目中资源文件 -- shape篇
Android 项目中资源文件 -- selector篇
Android 项目中资源文件 -- layer-list篇

其实,自己也不知道接下来写什么,那就接着“Android 项目中资源文件 ”这个题目往继续吧。这篇就来整理一下 Android shape 的使用,和一些常用属性。

话不多说先上图一张

色彩渐变
如果在开发中,我们要做一个上图效果的色彩渐变的效果,最简单的方法就是使用我们 Android 中的shape来完成。当然有人会说他可以使用一个图片来实现同样的效果,那就要告诉一下 使用 shape 可以减少资源的占用,减少 apk 的大小,同时他可以很好的适配各种不同尺寸的 Android 设备

**先给大家推荐一个工具 : ** Android shape 在线编辑 & 效果展示

shape

shape 文件语法简例

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] > // 定义形状
    <corners //圆角属性
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient //渐变属性
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding //边距属性
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size //大小属性
        android:width="integer"
        android:height="integer" />
    <solid //填充属性
        android:color="color" />
    <stroke //描边属性
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

从上面 shape 文件开始介绍它的属性吧

1)特殊属性

shape 使用的时候,提供了不同的形状供我们选择

形状(shape) 属性值
矩形 rectangle
椭圆 oval
线 line
圆环 ring
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape=["rectangle" | "oval" | "line" | "ring"] //shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)   
  //下面的属性只有在android:shape="ring"时可用:   
  android:innerRadius="10dp" //  内环的半径; 
  android:innerRadiusRatio="2"  // 浮点型,以环的宽度比率来表示内环的半径;   
  android:thickness="3dp"   // 环的厚度;   
  android:thicknessRatio="2" //  浮点型,以环的宽度比率来表示环的厚度;  
  android:useLevel="false"> //  boolean值,如果当做是LevelListDrawable使用时值为true,否则为false。
</shape>
  • rectangle(矩形)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimary"/>
</shape>
  • oval(椭圆)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorPrimary"/>
    <size android:height="100dp"
        android:width="100dp"/>
</shape>
  • line(线)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
         android:color="@color/colorAccent"
         android:dashGap="3dp"//虚线间距
         android:dashWidth="4dp"/>//虚线宽度
    <size android:height="3dp"/>
</shape>
  • rectangle(矩形)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"
    android:innerRadius="20dp" // 内环的半径
    android:thickness="10dp"> // 圆环宽度
    <!--useLevel需要设置为false-->
    <solid android:color="@color/colorAccent"/>
</shape>

2)基本属性

shape 使用时可以定义 view 的一些效果(圆角、渐变、颜色、边框、大小等等)。这些往往都可以通过 shape 的子标签来实现。 shape 的子标签有:

shape的子标签 效果样式
<corners> 圆角
<solid> 填充色
<gradient> 渐变
<stroke> 描边
<padding> 内边距
<size> 大小
  • <corners>(圆角)
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <corners    //定义圆角   
    android:radius="10dp"  //全部的圆角半径;   
    android:topLeftRadius="5dp"  //左上角的圆角半径;   
    android:topRightRadius="5dp" //右上角的圆角半径;   
    android:bottomLeftRadius="5dp"  //左下角的圆角半径;   
    android:bottomRightRadius="5dp" /> //右下角的圆角半径。
</shape>  
  • <solid>(内部填充色)
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <solid android:color="#ffff00"/> //内部填充色
</shape> 
  • <gradient>(渐变)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <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> 
渐变方式(type的值) 渐变方式
linear 线性渐变(默认)
radial 放射渐变
sweep 扫描式渐变
  • <stroke>(描边)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <stroke        
    android:width="1dp"   //描边的宽度   
    android:color="#ff0000"   //描边的颜色   
    // 以下两个属性设置虚线   
    android:dashWidth="1dp" //虚线的宽度,值为0时是实线   
    android:dashGap="1dp" />//虚线的间隔
</shape> 
  • <padding>(内边距)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <padding    
    android:left="10dp"  //左内边距; 
    android:top="10dp"   //上内边距;
    android:right="10dp"   //右内边距;
    android:bottom="10dp" /> //下内边距。
</shape>
  • <size>(大小)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <size   
    android:width="50dp"  //宽度 
    android:height="50dp" />// 高度
</shape>

shape 使用

1)在 res/drawable 目录下新建 shape_test.xml 文件;
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="@color/color_FEDA4D"
        android:endColor="@color/color_FEC84D" />
    <corners android:topRightRadius="@dimen/qb_px_25"
        android:bottomRightRadius="@dimen/qb_px_25"/>

</shape>
2)在布局文件的 view 中引入 shape_test.xml 文件;
    <TextView
        android:id="@+id/tv_confirm"
        android:layout_width="0dp"
        android:layout_height="@dimen/qb_px_50"
        app:layout_constraintStart_toEndOf="@id/tv_reset"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/tv_reset"
        android:layout_marginEnd="@dimen/qb_px_15"
        android:background="@drawable/bg_end_r25_change"
        android:textStyle="bold"
        android:text="确 定"
        android:textSize="@dimen/sp_16"
        android:textColor="@color/textColor_2f2f2f"
        android:gravity="center"/>

下图中的 确认 按钮的效果如下:

效果展示

相关文章

网友评论

    本文标题:Android 项目中资源文件 -- shape篇

    本文链接:https://www.haomeiwen.com/subject/eycntktx.html