Android XML绘图-Shape

Shape简介

XML在Andorid系统中可不是仅仅是一个布局的文件,配置列表。在Android开发中,它可以画一副图。Android的开发者给XML提供了几个强大的技能来帮我们实现。

我们在Android开发中都知道XML文件必定是比图片小的,所以针对APP的优化,我们能够用shape实现的效果就尽量用shape实现。

 <?xml version="1.0" encoding="utf-8"?>
    <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    //默认为rectangle
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners //当shape="rectangle"时使用  //定义圆角  
    android:radius="integer"           //全部的圆角半径   
    android:topLeftRadius="integer"0   //左上角的圆角半径   
    android:topRightRadius="integer"   //右上角的圆角半径
    android:bottomLeftRadius="integer" //左下角的圆角半径
    android:bottomRightRadius="integer" />  //右下角的圆角半径 
    <gradient //渐变
    android:angle="integer"          //渐变角度,必须为45的倍数,0为从左到右,90为从上到下
    android:centerX="integer"        //渐变中心X的相当位置,范围为0~1  
    android:centerY="integer"         //渐变中心Y的相当位置,范围为0~1
    android:centerColor="integer"     //渐变中间点的颜色,在开始与结束点之间
    android:endColor="color"          //渐变结束点的颜色   
    android:gradientRadius="integer"  //渐变的半径,只有当渐变类型为radial时才能使用
    android:startColor="color"
    android:type=["linear" | "radial" | "sweep"]
    //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变。
    android:useLevel=["true" | "false"] />
    //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果

    <padding
    android:left="integer"
    android:top="integer"
    android:right="integer"
    android:bottom="integer" />

    <size    //指定大小 一般用在imageview中的scaleType中使用
    android:width="integer"

    <solid   //填充颜色
    android:color="color" />

    <stroke  //指定边框
    android:width="integer"
    android:color="color"
    android:dashWidth="integer"

    </shape>

从上诉代码可以看出来
android:shape=[“rectangle” | “oval” | “line” | “ring”] >
shape可以画四种图形,分别是:矩形(rectangle)、椭圆(oval)、线(line)、圆环(ring)。

Shape可以说是XML绘图的精华所在,它的功能十分强大,无论是扁平化,还是你无话,还是渐变,它都能绘制,整个shape的可用参数,在上面的表中已经列举的很清楚了。

怎么用shape?

1、在项目 res/drawable 文件夹下创建文件 my_shape.xml :
这里写图片描述
这里写图片描述

2、在项目 res/layout 布局文件中引用:
这里写图片描述

3、run效果
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值