android shape 的应用

在android中有一些简单的图形,我们可以用代码书写出来,但是直接用Java代码书写与android开发布局文件和逻辑处理这样的形式有一些背离,并且在XML文件中书写可以说比较简单,以后都可以引用,废话不多说,开始了。
先说根节点,因为有了根节点你才知道这是一个啥样的。

先列一个简单的

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <corners android:radius="5dp"></corners>
    <solid android:color="#22ff00ff"></solid>
</shape>
我们可以看到根节点就是shape,那么既然是画图形,都可以画哪些呢

android:shape这个属性就是表明你要画的是什么:

shape图形都有以下:

rectangle(矩形):默认的时候就是矩形

line(线)

oval(椭圆)

ring(圆环)

那么他的子节点就是用来修饰上面的图形的,那么都有哪些呢我们先都写出来:
修饰的属性全部有以下几个:

  • solid:填充色

    corners:设置边角的圆的半径,即假如是长方形,但是想让四个角成为圆形,设置这个值

    gradient : 设置渐变色

    padding:设置边框值,只有使用的时候才能看出来

    size 设置这个图形的大小

    stroke 描边
    下面就具体的说一说这几个的具体应用:
    以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="#409cf9"></solid>
</shape>

图片如下:这里写图片描述

先写上填充色,不然你怎么看到,那么没有高度和宽度呢,高度和宽度是默认的占满的,一般不用设置,不然屏幕适配很麻烦,就让他占据引用他的控件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#409cf9"></solid>
    <corners android:radius="50dp"></corners>
</shape>

添加了 corners属性:结果就成了这样
这里写图片描述
可以看到四个角变成了圆角,
corners有以下几个属性:

  • android:bottomLeftRadius 左下角
    android:bottomRightRadius 右下角
    android:topLeftRadius 左上角
    android:topRightRadius 右上角
    android:radius 这个就是四个角一样
    以上的就不列举例子了

我们再添加一个属性

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="30dp"></corners>
    <gradient android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"></gradient>
</shape>

图形如下:
这里写图片描述
这个属性 与solid填充,谁在后面谁显示谁
这个描述的比较多,我们具体来看
gradient有以下几个属性:
以上面的为原图:

  • android:startColor=”#ff0000”起始颜色
    android:centerColor=”#00ff00”//中间颜色
    android:endColor=”#0000ff”//末尾颜色
    angle 角度,只能是45的倍数 angle=0原始的显示,angle = 90 ——》 这里写图片描述

    type 模式,我们可以想象,渐变都可以怎么来显示,比如从左往右,从中间到四周,还可以顺时针的模式所以
        type = line线性就是上面我们看到的(默认的),
        type = sweep -》
    

    顺时针

  • type = radial”从内向外
    android:gradientRadius=”150dp” -》这个就是与radial配合使用的,别处没发用这个属性,他是描述从内到外的辐射大小的
    这里写图片描述

     centerX与centerY,就是描述这个渐变的偏向位置的,假如中间颜色是绿色在中间,让他向左边偏移一下,或者是向上偏移一下,centerX= 0.3向左偏,大于0小于1之间
    

基本渐变就是上面的这些

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#409cf9"></solid>

    <corners android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="30dp"></corners>
 <stroke
     android:width="10dp"
     android:color="#ff0000"></stroke>
</shape>

图片如下:
这里写图片描述
现在开始说明stroke属性:也就是描边
-android:width=”10dp”边的宽度
android:color=”#ff0000”边的颜色
android:dashGap=”20dp”和下面的这个一起用,不然没有效果,这两个是把边改成了一个一个的线断,这个是每两个线段之间的距离
android:dashWidth=”20dp”这个是每个线段的长度
看图片:
这里写图片描述

基本上每个属性都讲了,剩下的只是改变图形而已,有的不能一起用或者是用了没有效果,自己可以想一想,比如线段,他有圆角吗,所以设置了,没有用,要让线段显示颜色和虚线就是设置stroke属性。

关于环形的应用,大家可以去看这个人的博客,这人太有毅力了,彻底打败了我,以上我讲的也有了,没有的也讲了

http://blog.csdn.net/north1989/article/details/52939888

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值