android shape 自定义绘制图片思路
  HvTJUzsxOBtS 2023年11月25日 27 0



文章目录

  • 4、常用的shape 属性及介绍



####1、效果展示


android  shape 自定义绘制图片思路_shape

####2、首先绘制drawable资源文件
1)drawable 里面 新建一个 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">                <!--设置形状为矩形-->
    <corners android:radius="15dp"/>          <!--四个角度15dp-->

    <stroke android:color="@color/background_text_color"
             android:width="2dp"/>          <!--设置矩形外框颜色为白色/ 外线宽度2dp-->

    <solid android:color="@color/possible_result_points"/>   <!--填充颜色-->
</shape>

效果如图所示:

android  shape 自定义绘制图片思路_xml_02

####3、在layout组件中添加背景资源

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="30dp"
    android:orientation="vertical">

    <Button
        android:id="@+id/bluetooh_open_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开音箱蓝牙"
        android:textSize="30dp"
        android:background="@drawable/btground"/>
    <Button
        android:id="@+id/bluetooh_close_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="关闭音箱蓝牙"
        android:textSize="30dp"
        android:layout_marginTop="10dp"/>
</LinearLayout>

效果如图:

android  shape 自定义绘制图片思路_xml_03

4、常用的shape 属性及介绍
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="false|true"             //将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true。
    android:shape="rectangle|line|oval|ring"//分别为矩形、线、椭圆、环。默认为矩形rectangle
    android:innerRadius="integer"           // shape为ring时可用,内环半径
    android:innerRadiusRatio="float"        // shape为ring时可用,内环的厚度比,即环的宽度比表示内环半径,默认为3,可被innerRadius值覆盖
    android:thickness="integer"             // shape为ring时可用,环的厚度
    android:thicknessRatio="float"          // shape为ring时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖
    android:tint="color"                    // 给shape着色
    android:tintMode="src_in|src_atop|src_over|add|multiply|screen" // 着色类型
    android:useLevel="false|true"           // 较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用
    android:visible="false|true" 
    >
    <!-- 圆角 -->
    <corners
        android:radius="integer"            // 圆角半径,该值设置时下面四个属性失效
        android:bottomLeftRadius="integer"  // 左下角圆角半径
        android:bottomRightRadius="integer" // 右下角圆角半径
        android:topLeftRadius="integer"     // 左上角圆角半径
        android:topRightRadius="integer"    // 右上角圆角半径
        />
    <!-- 渐变 -->
    <gradient
        android:useLevel="false|true"       // 与上面shape中该属性的一致
        android:type="linear|radial|sweep"  // 渐变类型,分别为线性、放射性、扫描性渐变,默认为线性渐变linear
        android:angle="integer"             // 渐变角度,当上面type为线性渐变linear时有效。角度为45的倍数,0度时从左往右渐变,角度方向逆时针
        android:centerColor="color"         // 渐变中间位置颜色
        android:startColor="color"          // 渐变开始位置颜色
        android:endColor="color"            // 渐变结束位置颜色
        android:centerX="float"             // type为放射性渐变radial时有效,设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置
        android:centerY="float"             // type为放射性渐变radial时有效,设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置
        android:gradientRadius="integer"    // type为放射性渐变radial时有效,渐变的半径
        />
    <!-- 内边距 -->
    <padding
        android:bottom="integer"  // 设置底部边距
        android:left="integer"    // 左边边距
        android:right="integer"   // 右边
        android:top="integer"     // 顶部
    />
    <!-- 大小 -->
    <size
        android:height="integer"  // 宽度
        android:width="integer"   // 高度
        />
    <!-- 填充 -->
    <solid
        android:color="color"     // shape的填充色
        />
    <!-- 描边 -->
    <stroke
        android:color="color"       // 描边的颜色
        android:width="integer"     // 描边的宽度
        android:dashGap="integer"   // 虚线间隔
        android:dashWidth="integer" // 虚线宽度
    />
</shape>

####5、常见几个小例子

1)一个圆

android  shape 自定义绘制图片思路_圆角_04


代码:

<TextView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:text="12"
    android:gravity="center"
    android:textColor="#ffffff"
    android:background="@drawable/text"
    />

xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="oval"
  >
  <corners
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"
    />
  <size
    android:width="30dp"
    android:height="30dp"
    />
  <stroke
    android:width="1dp"
    android:color="#ffffff" />
  <solid
    android:color="#F05F4C"
    />
 
</shape>

2)椭圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
<size
    android:width="50dp"
    android:height="100dp"/>
<gradient
    android:type="radial"
    android:centerX="1"
    android:centerY="0.5"
    android:startColor="@color/colorPrimary"
    android:centerColor="@color/colorAccent"
    android:endColor="@color/colorPrimaryDark"
    android:gradientRadius="50dp"
    />
</shape>

文献参考:

Android中shape属性详解


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月25日 0

暂无评论

推荐阅读
HvTJUzsxOBtS