android之自定义渐变颜色(二)
来源:互联网 发布:php api接口开发文档 编辑:程序博客网 时间:2024/06/14 01:55
在android之自定义渐变颜色(一)中我们已经学到如何在xml定义渐变颜色,今天我们来学学如何用代码定义渐变颜色
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)
或者直接创建一个drawable:
- <span style="font-size: small;"> public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE); //设置没标题
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , //全屏
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.login);//登录界面
- GradientDrawable grad = new GradientDrawable(//渐变色
- Orientation.TOP_BOTTOM,
- new int[]{Color.BLACK, Color.WHITE}
- );
- getWindow().setBackgroundDrawable(grad);//设置渐变颜色
- }</span>
- android之自定义渐变颜色(二)
- android之自定义渐变颜色(二)
- android之自定义渐变颜色(一)
- 自定义View之颜色渐变折线图
- 自定义View之颜色渐变折线图
- Android自定义颜色渐变的拖动条
- Android UI之自定义——最简单的仿QQ音乐歌词颜色渐变
- Android studio 之 简单的颜色渐变
- 自定义View之GradualView文字渐变-颜色渐变-图像渐变
- 自定义Behavior之ToolBar上滑TabLayout颜色渐变
- Android开发--自定义颜色渐变的标题栏简单Demo
- android 设置颜色渐变
- android颜色渐变
- android背景颜色渐变
- android Textview颜色渐变
- android背景颜色渐变
- android rgb颜色渐变
- android实现颜色渐变
- Android中MediaStore使用示例 .
- 查看Centos的CPU温度,硬盘信息
- UINavigationBar self-study
- Android ListView元素间隙线自定义渐变效果
- android 4.0.4的默认壁纸的修改
- android之自定义渐变颜色(二)
- 线程局部存储,Part 3:编译器和链接器对隐式TLS的支持
- 博士这条船
- Shell 学习8(sort命令)
- 使用doxygen从c++代码中生成文档的方法学习 .
- SQL Server 2005性能排错
- opencv 中BUILD_opencv_world
- Linux 中 RPM包 安装 查询 卸载命令的总结
- UIViewController self-study