温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中如何自定义渐变颜色效果

发布时间:2022-04-15 16:17:05 来源:亿速云 阅读:255 作者:iii 栏目:编程语言

这篇文章主要讲解了“Android中如何自定义渐变颜色效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android中如何自定义渐变颜色效果”吧!

xml定义渐变颜色

首先,你在drawable目录下写一个xml,代码如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <gradient
    android:angle="270"
    android:endColor="#000000"
    android:startColor="#ffffff" />
  <corners
    android:bottomLeftRadius="5dip"
    android:bottomRightRadius="5dip"
    android:topLeftRadius="5dip"
    android:topRightRadius="5dip" />
</shape>

shape 节点配置的是图形的形式,主要包括方形、圆形等,上边代码为方形,
gradient 节点主要配置起点颜色、终点颜色及中间点的颜色、坐标、渐变效果(0,90,180从左到右渐变,270从上到下渐变)默认从左到右
padding 节点主要配置上下左右的间距
corners 节点配置四周园脚的半径

然后,你就可以随意在代码中或者xml布局中使用它了。

如此简单的配置,只要你知道颜色的rgb值,你就可以成为颜色达人。

代码定义渐变颜色

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:

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);//设置渐变颜色
}

感谢各位的阅读,以上就是“Android中如何自定义渐变颜色效果”的内容了,经过本文的学习后,相信大家对Android中如何自定义渐变颜色效果这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI