在android.graphics中提供了有关Gradient字样的类,例如LinearGradient线性渐变、RadialGradient径向渐变和SweepGradient角度渐变三种,他们的基类为android.graphics.Shader。为了演示图像渐变效果,下面给出一个简单的实例。一、LinearGradient线性渐变在android平台中提供了两种重载方式来实例化该类分别为,他们的不同之处为参数中第一种方法可以用颜色数组,和位置来实现更细腻的过渡效果,比如颜色采样int[]colors数组中存放20种颜色,则渐变将会逐一处理。而第二种方法参数仅为起初颜色color0和最终颜色color1。LinearGradient(floatx0,floaty0,floatx1,floaty1,int[]colors,float[]positions,Shader.TileModetile)LinearGradient(floatx0,floaty0,floatx1,floaty1,intcolor0,intcolor1,Shader.TileModetile)使用实例如下:Paintp=newPaint();LinearGradientlg=newLinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);//参数一为渐变起初点坐标x位置,参数二为y轴位置,参数三和四分辨对应渐变终点,最后参数为平铺方式,这里设置为镜像刚才已经讲到Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变,代码如下:p.setShader(lg);canvas.drawCicle(0,0,200,p);//参数3为画圆的半径,类型为float型。二、RadialGradient镜像渐变有了上面的基础,我们一起来了解下径向渐变。和上面参数唯一不同的是,径向渐变第三个参数是半径,其他的和线性渐变相同。RadialGradient(floatx,floaty,floatradius,int[]colors,float[]positions,Shader.TileModetile)RadialGradient(floatx,floaty,floatradius,intcolor0,intcolor1,Shader.TileModetile)三、SweepGradient角度渐变对于一些3D立体效果的渐变可以尝试用角度渐变来完成一个圆锥形,相对来说比上面更简单,前两个参数为中心点,然后通过载入的颜色来平均的渐变渲染。SweepGradient(floatcx,floatcy,int[]colors,float[]positions)//对于最后一个参数SDK上的描述为MaybeNULL.Therelativepositionofeachcorrespondingcolorinthecolorsarray,beginningwith0andendingwith1.0.Ifthevaluesarenotmonotonic,thedrawingmayproduceunexpectedresults.IfpositionsisNULL,thenthecolorsareautomaticallyspacedevenly.,所以建议使用下面的重载方法,本方法一般为NULL即可。SweepGradient(floatcx,floatcy,intcolor0,intcolor1)到此,希望大家对图像特效处理有了一定的认识,了解这些对打好Android游戏开发的基础很有好处。转载