参考博客: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. Alpha 冲刺 (10/10)

    Alpha 冲刺 (10/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.和愈明.韫月一起对接 2 ...

  2. intelij idea模板

    1.idea设置模板 Postfix Completion是无法改变的模板 live Template是可以修改的 自定义模板 如下图: 创建测试方法: $VAR1$代表光标占位符

  3. mysql group_concat(column) 函数替换成 oracle wm_concat(colum)

    11gr2和12C上已经摒弃了wm_concat函数,所以只能手动创建该函数 解决办法: 一.解锁sys用户 alter user sys account unlock; 二.创建包.包体和函数 以s ...

  4. JavaScript之循环

    我是昨天的小尾巴...https://blog.csdn.net/weixin_42217154/article/details/81182817 3.2 循环结构 循环结构是指在程序中需要反复执行某 ...

  5. 1) 嵌套的 div ,或者 ul ol .li 阻止冒泡 ,特别是 对应onclick="test(event)" 通过传递event 阻止 冒泡. cancelBubble , stopPropagation

    1 .html 结构: <ul class="ul_2 hide" data-first="5"> <li class="li_2& ...

  6. 使用jsoup轻松爬数据

    刚刚学习爬虫,感觉使用jsoup爬虫挺容易的.记录一下自己爬取数据的过程. Jsoup介绍: Jsoup 是一个 Java 的开源HTML解析器,可直接解析某个URL地址.HTML文本内容.使用Jso ...

  7. 弹性布局(Flex布局)整理

    一.  弹性布局 一个好的网站都有让用户看上去很舒服的布局,一个网站的布局也会或多或少影响到它的浏览量,看完阮大神的博客,就想把弹性布局整理一下. 在平时的我们常用的布局类型有以下几种: 1.浮动+定 ...

  8. 2017-2018 ACM-ICPC, NEERC A题Automatic Door 挺棘手的模拟

    题目链接:http://codeforces.com/contest/883/problem/A 题意大致就是有一个门,有n个人有规律的来,时刻分别是a,2a,3a.....na.有m个人无规律的来, ...

  9. Visual Studio AI 离线模型训练(Windows10)

    一.序 环境搭建:[查看] samples-for-ai项目下载:[下载],两个版本,一个2018年6月15日前,一个2018年6月15日-16日版本(当前最新版本). 在环境搭建过程中,通过git ...

  10. BEAM188简单应用

    目录 BEAM188简介 APDL应用实例 显示梁三维图 BEAM188简介 BEAM188-3D线性有限应变梁 Beam188 单元适合于分析从细长到中等粗短的梁结构,该单元基于铁木辛哥梁结构理论, ...