android 渐变

简介: 引用:http://www.189works.com/article-2464-1.html 在Android游戏开发中我们不免要涉及到一些图形特效处理,今天主要看下Android平台下实现渐变效果。

引用:http://www.189works.com/article-2464-1.html

Android游戏开发中我们不免要涉及到一些图形特效处理,今天主要看下Android平台下实现渐变效果。在android.graphics中我们可以找到有关Gradient字样的类,比如LinearGradient 线性渐变、RadialGradient径向渐变和 角度渐变SweepGradient 三种,他们的基类为android.graphics.Shader。为了显示出效果,使用一个简单的例子来说明。

一、LinearGradient线性渐变

在android平台中提供了两种重载方式来实例化该类分别为,他们的不同之处为参数中第一种方法可以用颜色数组,和位置来实现更细腻的过渡效果,比如颜色采样int[] colors数组中存放20种颜色,则渐变将会逐一处理。而第二种方法参数仅为起初颜色color0和最终颜色color1。

LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile) 

LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile) 
使用实例如下:
Paint p=new Paint();
LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);  //参数一为渐变起初点坐标x位置,参数二为y轴位置,参数三和四分辨对应渐变终点,最后参数为平铺方式,这里设置为镜像.

刚才Android开发网已经讲到Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变,代码如下:
p.setShader(lg);
canvas.drawCicle(0,0,200,p); //参数3为画圆的半径,类型为float型。

二、 RadialGradient镜像渐变

有了上面的基础,我们一起来了解下径向渐变。和上面参数唯一不同的是,径向渐变第三个参数是半径,其他的和线性渐变相同。

RadialGradient(float x, float y, float radius, int[] colors, float[] positions, Shader.TileMode tile) 
RadialGradient(float x, float y, float radius, int color0, int color1, Shader.TileMode tile)

三、 SweepGradient角度渐变

对于一些3D立体效果的渐变可以尝试用角度渐变来完成一个圆锥形,相对来说比上面更简单,前两个参数为中心点,然后通过载入的颜色来平均的渐变渲染。

SweepGradient(float cx, float cy, int[] colors, float[] positions)  //对于最后一个参数SDK上的描述为May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.,所以Android123建议使用下面的重载方法,本方法一般为NULL即可。
SweepGradient(float cx, float cy, int color0, int color1)

相关文章
|
NoSQL Shell Linux
Linux上MongoDB无法连接解决之道
解决重启机器后mongodb不能连接的问题
19046 0
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
277 1
|
存储 JavaScript Linux
百度搜索:蓝易云【在Linux上卸载和重新安装NVM。】
现在,你已经成功在Linux上卸载并重新安装了NVM。你可以使用NVM来管理和切换不同的Node.js版本。请注意,具体的命令和步骤可能因NVM的版本而有所不同,上述步骤适用于当前版本的NVM。在重新安装之前,确保备份任何重要的数据。
324 1
|
Android开发 架构师
Item点击水波纹效果
先上效果图   ripple的使用(需要V21以上) 其中item的颜色是控件正常状态的背景色,ripple中的颜色是点击时出现的颜色(会以半透明的形式展示出来)。
1513 0
|
Android开发
Android Button、TabLayout的英文字是大写的?
参考 我的Android进阶之旅------>android Button上面的英文字符串自动大写的问题解决android在使用过程中,解决 Button 和 TabLayout 英文自动大写的问题 如图 1、未解决前的,button内英文文字是大写的,而textview正常 0.png 2、解决后,button内英文文字正常 1.png 解决 1、第一个联想到的就是button控件的大小写属性,可是在button里我没设置啊?奇怪。
3544 0
|
Go 调度 C语言
进程&线程间通信:信号量
参考文档:http://blog.csdn.net/evsqiezi/article/details/8061176头文件:#include int sem_init __P ((sem_t *__sem, int __pshared, unsigned int __value));sem为指向信号量结构的一个指针;pshared不为0时此信号量在进程间共享,否则只能为当前进程的所有线程共享;value给出了信号量的初始值。
1250 0
|
算法 Java Android开发
使用 Android Studio 检测内存泄漏与解决内存泄漏问题
     本文在腾讯技术推文上 修改 发布。     http://wetest.qq.com/lab/view/63.html?from=ads_test2_qqtips&sessionUserType=BFT.
1447 0