Android GradientDrawable的XML实现

Android GradientDrawable与附录文章1类似,这次以XML而非Java代码形式实现。比如写好一个shape文件放在res/drawable目录下,名字比如可以命名为gradient.xml。
然后在上层Java代码里面就可以使用这个shape文件,比如是一个ImageView文件,把这个shape文件加载:

ImageView image= (ImageView) findViewById(R.id.imageView);
image.setImageDrawable(getResources().getDrawable(R.drawable.gradient));

下面是GradientDrawable以不同xml代码实现的不同结果。

(一)线性渐变的GradientDrawable。

shape代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"> <gradient
android:angle="45"
android:centerColor="@android:color/holo_orange_dark"
android:endColor="@android:color/holo_blue_dark"
android:startColor="@android:color/holo_red_dark" /> <stroke
android:width="20dip"
android:color="@android:color/black"
android:dashGap="2dip"
android:dashWidth="10dip" />
</shape>

结果:

没有dash间断分割的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"> <gradient
android:angle="135"
android:centerColor="@android:color/holo_orange_dark"
android:endColor="@android:color/holo_blue_dark"
android:startColor="@android:color/holo_red_dark" /> <stroke
android:width="20dip"
android:color="@android:color/black" />
</shape>

结果:

以上是GradientDrawable的线性渐变。

(二)ring环形的GradientDrawable。

设定Android shape=ring,制作一个圆环。如代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:thickness="20dp"
android:useLevel="false"> <gradient
android:angle="90"
android:centerColor="@android:color/holo_orange_dark"
android:endColor="@android:color/holo_blue_dark"
android:startColor="@android:color/holo_red_dark" /> </shape>

结果:

这句代码:

android:thickness="20dp"

圆环的厚度。

给圆环描边(假设是5dp):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:thickness="30dp"
android:useLevel="false"> <gradient
android:angle="90"
android:centerColor="@android:color/holo_orange_dark"
android:endColor="@android:color/holo_blue_dark"
android:startColor="@android:color/holo_red_dark" /> <stroke android:width="5dp"
android:color="@android:color/black" /> </shape>

结果:

(三)渐变矩形。

代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <gradient
android:angle="90"
android:endColor="@android:color/holo_green_light"
android:startColor="@android:color/transparent" /> </shape>

结果:

(四)渐变线。

用andriod的shape直接设置属性为line即可画一个实现,同时设定size的高度值为1px或者1dp,这很简单就实现了。但是如果要画一个渐变的线,直接使用line就复杂难办了。解决这个问题可以转化一下,我们画一个矩形,但是这个矩形的高度只有1px或1dip,这样就把一个矩形变矮成一条线,矩形的渐变很容易实现。比如实现一个从两边到中间逐渐变黑的高度仅仅为1px的线条,那么可以画一个矩形,设置这个矩形的开始、中间、结束渐变颜色值,同时设定它的高度值是1px,从而就实现了一个渐变线,代码例子:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <gradient
android:angle="0"
android:centerColor="@android:color/black"
android:endColor="@android:color/transparent"
android:startColor="@android:color/transparent" /> <size android:height="1px" /> </shape>

结果:

附录:
1,《Android渲染器Shader:LinearGradient(一)》链接:http://blog.csdn.net/zhangphil/article/details/52004027
2,《Android渲染器Shader:梯度渐变扫描渲染器SweepGradient(二)》链接:http://blog.csdn.net/zhangphil/article/details/52021677
3,《Android渲染器Shader:环状放射渐变渲染器RadialGradient(三)》链接:http://blog.csdn.net/zhangphil/article/details/52023723
4,《Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape》链接:http://blog.csdn.net/zhangphil/article/details/52025152
5,《Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现》链接:http://blog.csdn.net/zhangphil/article/details/52035255

Android GradientDrawable的XML实现的更多相关文章

  1. Android GradientDrawable(shape标签定义) 静态使用和动态使用(圆角,渐变实现)

    Android GradientDrawable使用优势: 1. 快速实现一些基本图形(线,矩形,圆,椭圆,圆环) 2. 快速实现一些圆角,渐变,阴影等效果 3. 代替图片设置为View的背景 4. ...

  2. Android在layout xml中使用include

    Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...

  3. android获取string.xml的值(转)

    为什么需要把应用中出现的文字单独存放在string.xml文件中呢? 一:是为了国际化,当需要国际化时,只需要再提供一个string.xml文件,把里面的汉子信息都修改为对应的语言(如,English ...

  4. [转载]关于android SDK安装Failed to fetch URL http://dl-ssl.google.com/android/repository/addons_list-1.xml出错

    原文地址为:http://blog.csdn.net/springsky_/article/details/7442388 因为入行移动测试,所以很多测试环境的搭建.从中遇到了和这个GG同样的问题.怕 ...

  5. [android警告] AndroidManifest.xml警告 Should explicitly set android:allowBackup to true or false

    http://www.cnblogs.com/javadu/p/3812528.html Android中AndroidManifest.xml警告 Should explicitly set and ...

  6. 解决:Unable to connect to repository https://dl-ssl.google.com/android/eclipse/site.xml

    ailed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml, reason: Connection ...

  7. "Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list.xml,reason: Connection

    "Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list.xml,reason: Conne ...

  8. 修改Android中strings.xml文件, 动态改变数据

    有些朋友可能会动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法.strings.xml中节点是支持占位符的,如下所示: <string name=&qu ...

  9. [置顶] Android学习系列-Android中解析xml(7)

    Android学习系列-Android中解析xml(7) 一,概述 1,一个是DOM,它是生成一个树,有了树以后你搜索.查找都可以做. 2,另一种是基于流的,就是解析器从头到尾解析一遍xml文件.   ...

随机推荐

  1. 143 Reorder List 重排链表

    给定一个单链表L:L0→L1→…→Ln-1→Ln,重新排列后为: L0→Ln→L1→Ln-1→L2→Ln-2→…必须在不改变节点的值的情况下进行原地操作.例如,给定链表 {1,2,3,4},按要求重排 ...

  2. servlet基础概念

    一.servlet是什么? 运行在Web服务器上(如:tomcat),作为浏览器请求与数据库或其他应用程序之间的中间层 二.servlet主要任务: 1.读取浏览器发送的显式数据(如:html表单)隐 ...

  3. 【Laravel】 常用命令

    自动创建项目 laravel new || laravel new xxx || composer create-project --prefer-dist laravel/laravel blog ...

  4. 基于ABP的Easyui admin framework正式开放源代码

    下载&反馈:http://www.webplus.org.cn v1.0 (2016/9/21) EF6+MVC5+API2+Easyui1.4.2开发 后台管理不使用iframe,全ajax ...

  5. 前端之CSS创建的样式

    CSS即层叠样式表,在创建时有以下几种样式: 1.内联样式(行内样式.行间样式): <标记 style=“属性:属性值:”></标记> 2.内部样式(嵌入式样式): <s ...

  6. 【学习笔记】OSG 基本几何图元

    例:geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); 来指定要利用这些数据生成一个怎么样的形状. ...

  7. Android如何用阿里云的API进行身份证识别

    准备工作:在libs下添加 alicloud-Android-apigateway-sdk-1.0.1.jar,commons-codec-1.10-1.jar 在build.gradle添加  co ...

  8. 【HEVC简介】DB-DeBlock Filter

    参考论文:HEVC Deblocking Filter <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com/DwyaneTalk/ ...

  9. 洛谷 P1434 滑雪

    题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最长 ...

  10. Redhat5 安装序列号及版本说明

    为了保证安装的组件和订阅相匹配,红帽企业 Linux 5 需要输入一个安装号.它被用来配置安装程序来提供正确的软件包.安装号码包含在你的订阅里. 如果您没有输入安装号码,只有核心服务器或 Deskto ...