参考博客:https://blog.csdn.net/iispring/article/details/50500106/

android颜色渐变的分类有:

LinearGradient线性渐变

线性渐变的参数为:

A点X坐标,A点Y坐标,B点X坐标,B点Y坐标,int[] 颜色数组,float[] 渐变点数组 ,渲染器模式;

RadialGradient镜像渐变

镜像渐变的参数为:

圆心点X坐标,圆心点Y坐标,半径增量值,int[] 颜色数组,float[] 渐变点数组 ,渲染器模式;

 SweepGradient角度渐变

角度渐变的参数为:

圆心点X坐标,圆心点Y坐标,int[] 颜色数组,float[] 渐变点数组 ;

渲染器模式说明:

Shader.TileMode.CLAMP 边缘拉伸

Shader.TileMode.MIRROR 镜像 (在水平方向和垂直方向交替镜像, 两个相邻图像间没有缝隙)

Shader.TileMode.REPEAT 重复模式 (在水平方向和垂直方向重复摆放,两个相邻图像间有缝隙缝隙)

1.线性渐变 LinearGradient

效果图:

代码:

package com.example.lenovo.mydemo.myViewDemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View; import com.example.lenovo.mydemo.R; /**
* Created by lenovo on 2018/7/2.
*/ public class MyView_1 extends View {
private final String TAG = "MyView_1";
private Paint mPaint;
private int i = 0; public MyView_1(Context context) {
super(context);
} public MyView_1(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} public MyView_1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); } @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint = new Paint();
mPaint .setStrokeWidth(20);
//LinearGradient 参数是 开始坐标和结束坐标 和 颜色
LinearGradient linearGradient = new LinearGradient(0,100,1000,100,
getResources().getColor(R.color.colorGreen),
getResources().getColor(R.color.colorIndigo),
Shader.TileMode.CLAMP);
mPaint.setShader(linearGradient);
canvas.drawLine(0,100,i,100,mPaint);
if (i==1000){
i=1000;
}else {
i=i+10;
}
postInvalidateDelayed(50); }
}
 

2.角度渐变SweepGradient

效果图:

代码:

package com.example.lenovo.mydemo.myViewDemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View; import com.example.lenovo.mydemo.R; /**
* Created by lenovo on 2018/7/2.
*/ public class MyView_1 extends View {
private final String TAG = "MyView_1";
private Paint mPaint;
private int i = 0;
private Bitmap mBitmap; public MyView_1(Context context) {
super(context);
} public MyView_1(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mBitmap = BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_bg);
mPaint = new Paint();
} public MyView_1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); } @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/*
导入图片
*/
mPaint.reset();
Rect bPRect = new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight());
Rect canvasRect = new Rect(0,0,getWidth(),getHeight());
canvas.drawBitmap(mBitmap,bPRect,canvasRect,mPaint);
mPaint.reset();
/*
画底图圆环
*/
mPaint.setColor(getResources().getColor(R.color.colorGray));
mPaint.setStrokeWidth(50);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(getWidth()/2,getHeight()/2,300,mPaint);
mPaint.reset(); /*
画渐变色彩圆弧
*/
mPaint.setStrokeWidth(40);
/* 这个方式的渐变也可以直接使用
SweepGradient sweepGradient = new SweepGradient(getWidth()/2,getHeight()/2,
getResources().getColor(R.color.colorIndigo),
getResources().getColor(R.color.colorGreen));
*/
//添加渐变颜色
int [] colors= new int[]{getResources().getColor(R.color.colorIndigo),
getResources().getColor(R.color.colorIndigo2),
getResources().getColor(R.color.colorIndigo3),
getResources().getColor(R.color.colorGreen)
};
//添加渐变颜色改变点
float[] f = new float[]{0,0.3f,0.6f,1};
//SweepGradient 的参数是: X中心点,Y中心点,颜色数组,渐变点数组
SweepGradient sweepGradient1 = new SweepGradient(getWidth()/2,getHeight()/2,colors,f);
mPaint.setShader(sweepGradient1);
//设置风格为空心圆
mPaint.setStyle(Paint.Style.STROKE);
//设置线条风格
mPaint.setStrokeCap(Paint.Cap.BUTT); //设置圆弧坐标
RectF rectF = new RectF();
rectF.left = getLeft()+(getWidth()/2-300);
rectF.right = getWidth()/2+300;
rectF.top = getTop()+(getHeight()/2-300);
rectF.bottom = getHeight()/2+300;
//以中心点逆时针旋转画布90度
canvas.rotate(-90,getWidth()/2,getHeight()/2);
//圆弧值
if(i==360) {
i = 0;
}else {
i = i+10;
}
//画圆弧 第一个参数为圆形四角坐标,第二个参数为起始角度,第三个参数为增量角度,第四参数是否闭合图形
canvas.drawArc(rectF,0,i,false,mPaint);
//设置每50毫秒刷新
postInvalidateDelayed(50);
mPaint.reset(); }
}
 

设置SweepGradient 渐变的开始角度:

因为大多数圆形渐变开始位置都不是一致的,所以我们需要设置渐变的开始角度

SweepGradient sg = new SweepGradient(getWidth()/2,getHeight()/2,colors,floats);
Matrix matrix = new Matrix();
//matrix.reset(); //如果是全局变量需要重置在插入值
matrix.preRotate(135,getWidth(),getHeight());
sg.setLocalMatrix(matrix);
mPaint.setShader(sg);
 

android 开发 View _9_ 实现渐变功能(直线与圆形)的更多相关文章

  1. android 开发 View _1_ View的子类们 和 视图坐标系图

    目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...

  2. Android开发——View动画、帧动画和属性动画详解

    0. 前言   Android动画是面试的时候经常被问到的话题.我们都知道Android动画分为三类:View动画.帧动画和属性动画. 先对这三种动画做一个概述: View动画是一种渐进式动画,通过图 ...

  3. Android开发——View滑动的三种实现方式

    0. 前言   Android开发中,我们常常需要View滑动实现一些绚丽的效果来优化用户体验.一般View的滑动可以用三种方式实现. 转载请注明出处:http://blog.csdn.net/seu ...

  4. Android开发——View滑动冲突解决方案

    0. 前言   我们在Android开发--事件分发机制详解中深入学习了事件分发机制,为我们解决Android开发中的滑动冲突问题做了初步准备.针对滑动冲突这里给出两种解决方案:外部拦截法和内部拦截法 ...

  5. android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览

    支持:https://www.cnblogs.com/whoislcj/p/5738478.html translationX的效果: protected void onCreate(Bundle s ...

  6. Android开发中的OpenCV霍夫直线检测(Imgproc.HoughLines()&Imgproc.HoughLinesP())

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃   //2017-04-21更新: 很多网友希望能得到源码,由于在公司做的,所以不太方便传出来 ...

  7. android 自定义view之选座功能

    效果图: 界面比较粗糙,主要看原理. 这个界面主要包括以下几部分 1.座位 2.左边的排数 3.左上方的缩略图 4.缩略图中的红色区域 5.手指移动时跟随移动 6.两个手指缩放时跟随缩放 主要技术点 ...

  8. android 开发 View _14 MotionEvent和事件处理详解,与实践自定义滑动条View

    转载https://blog.csdn.net/huaxun66/article/details/52352469 MotionEvent MotionEvent对象是与用户触摸相关的时间序列,该序列 ...

  9. android 开发 View _11_ xml动画

    请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/39996643 谢谢! 一.概述 Android的anima ...

随机推荐

  1. 剑指Offer 5. 用两个栈实现队列 (栈)

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题目地址 https://www.nowcoder.com/practice/54275ddae22f4 ...

  2. java基础(5)内部类

    1 成员内部类的定义和使用 public class Outer { private String name; public class Inner { public void innerMethod ...

  3. #学习笔记#JSP数据交互

    #学习笔记#JSP数据交互 数据库的使用方式:   当用户在第一个页面的查询框输入查询语句点提交的时候我们是用什么样的方式完成这个查询的? 答:我们通过在第一个页面提交表单的形式,真正的数据库查询时在 ...

  4. 使用 jest 测试 react component 的配置,踩坑。

    首先安装依赖 npm i jest -g npm i jest babel-jest identity-obj-proxy enzyme enzyme-adapter-react-15.4 react ...

  5. robot framework下载文件操作

    不同的浏览器点击[下载]按钮之后,需要点击保存,还是确定,或者直接默认直接下载都是不一样的 1.chrome:点击[下载]之后,会自动执行下载操作,直到下载结束 A)点击下载,等待下载结束(sleep ...

  6. 二维数组的查找(JAVA)

    二维数组查找 解题思路:找到该二维数组的特殊点,易知该二维数组左下角的那个点很特殊.从这个点往右看,数值都在变大:而往上看,数值都在变小.所以 我们可以将这个点的索引设为起点(i,j),当比目标数大时 ...

  7. IIS 集成模式 导致 AjaxPro 无法正常运行

    web.config 配置如下: system.web/httphandlers <httpHandlers> <add verb="POST,GET" path ...

  8. 冒号课堂 编程范式与OOP思想

    上篇:编程范式与编程语言 第1课 开班导言 第2课 重要范式 第3课 常用范式 第4课 重温范式 第5课 语言小谈 第6课 语言简评 下篇:抽象机制与对象范式 第7课 抽象封装 第8课 抽象接口 第9 ...

  9. redis 双写一致性 看一篇成高手系列1

    首先,缓存由于其高并发和高性能的特性,已经在项目中被广泛使用.在读取缓存方面,大家没啥疑问,都是按照下图的流程来进行业务操作. 但是在更新缓存方面,对于更新完数据库,是更新缓存呢,还是删除缓存.又或者 ...

  10. 关于各种BUF源语的研究

    关于各种BUF源语的研究 资料来源: 单端信号需要用到的BUF 关于这些源语的约束: 增大驱动电流 关于管脚的上拉与下拉约束: ODDR的两种操作模式 关于ODDR输出时钟的应用 为什么ODDR需要这 ...