drawable中xml的shape标签

介绍

1概述

android中的shape属于drawable中的xml绘制的标签,主要用于绘制简单的图形

2作用

  1. 占内存小
  2. 加载快
  3. 不会出现失真情况

3属性

3.1demo

新建drawable下shape_demo.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="false"
    android:innerRadius="10dp"
    android:innerRadiusRatio="10"
    android:shape="ring"
    android:thickness="10dp"
    android:thicknessRatio="10"
    android:tint="#999999"
    android:tintMode="multiply"
    android:useLevel="false"
    android:visible="true">
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:radius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />
    <size
        android:width="100dp"
        android:height="10dp" />
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
    <solid android:color="#CA3434" />
    <stroke
        android:width="1dp"
        android:color="#FFBA0D"
        android:dashWidth="0dp"
        android:dashGap="10dp" />
    <gradient
        android:angle="100"
        android:centerColor="#FF3030"
        android:centerX="0"
        android:centerY="0"
        android:endColor="#0048FF"
        android:gradientRadius="100"
        android:startColor="#41814A"
        android:type="linear"
        android:useLevel="true" />
</shape>

3.2详解

3.2.1shape(形状)

  1. dither:在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true
  2. innerRadius:shape为ring时可用,内环半径
  3. innerRadiusRatio:shape为ring时可用,内环的厚度比,即环的宽度比表示内环半径,默认为3,可被innerRadius值覆盖
  4. shape:分别为rectangle(矩形)、line(线)、oval(椭圆)、ring(环)。默认为rectangle(矩形)
  5. thickness:shape为ring时可用,环的厚度
  6. thicknessRatio:shape为ring时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖
  7. tint:给shape着色
  8. tintMode:shape着色类型:src_in、src_atop、src_over、add、multiply、screen
  9. useLevel:较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用
  10. ** visible**:true、false

3.2.2corners(圆角)

  1. bottomLeftRadius:左下角圆角半径
  2. bottomRightRadius:右下角圆角半径
  3. radius:圆角半径,该值设置时其他四个属性失效
  4. topLeftRadius:左上角圆角半径
  5. topRightRadius:右上角圆角半径

3.2.3size(大小)

  1. width:宽度
  2. height:高度

3.2.4padding(内边距)

  1. bottom:底部边距
  2. left:左边边距
  3. right:右边边距
  4. top:顶部边距

3.2.5solid(填充)

  1. color:shape的填充色

3.2.6stroke(描边)

  1. width:描边的宽度
  2. color:描边的颜色
  3. dashWidth:虚线宽度
  4. dashGap:虚线间隔

3.2.7gradient(渐变)

  1. angle:渐变角度,当type为linear(线性)时有效。角度为45的倍数,0度时从左往右渐变,角度方向逆时针
  2. centerColor:渐变中间位置颜色
  3. centerX:type为radial(放射性)时有效,设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置
  4. centerY:type为radial(放射性)时有效,设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置
  5. endColor:渐变结束位置颜色
  6. gradientRadius:type为radial(放射性)时有效,渐变的半径
  7. startColor:渐变开始位置颜色
  8. type:渐变类型,分别为linear(线性)、radial(放射性)、sweep(扫描性)渐变,默认为linear(线性)
  9. useLevel:与shape中该属性的一致

使用

1layout中的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
    	android:id="@+id/tv_shape"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_demo"
        />
</LinearLayout>

2代码activity中使用

Resources res = getResources();
Drawable shape = res.getDrawable(R.drawable.shape_demo);
TextView tv = findViewById(R.id.tv_shape);
tv.setBackgroundDrawable(shape);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值