android 开发 View _9_ 实现渐变功能(直线与圆形)
参考博客: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_ 实现渐变功能(直线与圆形)的更多相关文章
- android 开发 View _1_ View的子类们 和 视图坐标系图
目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...
- Android开发——View动画、帧动画和属性动画详解
0. 前言 Android动画是面试的时候经常被问到的话题.我们都知道Android动画分为三类:View动画.帧动画和属性动画. 先对这三种动画做一个概述: View动画是一种渐进式动画,通过图 ...
- Android开发——View滑动的三种实现方式
0. 前言 Android开发中,我们常常需要View滑动实现一些绚丽的效果来优化用户体验.一般View的滑动可以用三种方式实现. 转载请注明出处:http://blog.csdn.net/seu ...
- Android开发——View滑动冲突解决方案
0. 前言 我们在Android开发--事件分发机制详解中深入学习了事件分发机制,为我们解决Android开发中的滑动冲突问题做了初步准备.针对滑动冲突这里给出两种解决方案:外部拦截法和内部拦截法 ...
- android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览
支持:https://www.cnblogs.com/whoislcj/p/5738478.html translationX的效果: protected void onCreate(Bundle s ...
- Android开发中的OpenCV霍夫直线检测(Imgproc.HoughLines()&Imgproc.HoughLinesP())
本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 //2017-04-21更新: 很多网友希望能得到源码,由于在公司做的,所以不太方便传出来 ...
- android 自定义view之选座功能
效果图: 界面比较粗糙,主要看原理. 这个界面主要包括以下几部分 1.座位 2.左边的排数 3.左上方的缩略图 4.缩略图中的红色区域 5.手指移动时跟随移动 6.两个手指缩放时跟随缩放 主要技术点 ...
- android 开发 View _14 MotionEvent和事件处理详解,与实践自定义滑动条View
转载https://blog.csdn.net/huaxun66/article/details/52352469 MotionEvent MotionEvent对象是与用户触摸相关的时间序列,该序列 ...
- android 开发 View _11_ xml动画
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/39996643 谢谢! 一.概述 Android的anima ...
随机推荐
- 初见《构建之法》orz……
作为一个大二的计科狗,还是对软件设计有些懵,尽管之前学习过软件工程,但并没有掌握,突如其来的软件设计实践,让我着实傻眼. 通过消息得知,要学习<构建之法>,就去搜了一下,它是以实践为基础的 ...
- js计算两个日期相差天数
//两个时间相差天数 兼容firefox chrome var days = function(startDate) { var sdate = new Date(startDate.replace( ...
- linux安装mysql图文教程
---恢复内容开始--- 1.下载mysql [root@localhost ~]# yum install mysql mysql-server 输入y 输入y 输入y 下载完成 接下来我们要使用w ...
- Spring Boot笔记之自定义启动banner
控制banner内容 Spring Boot启动的时候默认的banner是spring的字样,看多了觉得挺单调的,Spring Boot为我们提供了自定义banner的功能. 自定义banner只需要 ...
- C# 网络通信功能 同步数据交互开发
前言 本文将使用一个Nuget公开的组件技术来实现一对多的数据通信功能,提供了一些简单的API,来方便的向服务器进行数据请求. 在visual studio 中的Nuget管理器中可以下载安装,也可以 ...
- 姿势估计实验-Realtime_Multi-Person_Pose_Estimation-CMU
前言: 论文及源代码网址: https://github.com/ZheC/Realtime_Multi-Person_Pose_Estimation 地址2: https://github.com/ ...
- Python学习笔记(day23更新)
第一章 计算机基础 1.1 硬件 计算机基本的硬件由:CPU / 内存 / 主板 / 硬盘 / 网卡 / 显卡 等组成,只有硬件但硬件之间无法进行交流和通信. 1.2 操作系统 作用:操作系统用于协同 ...
- 第四次作业——关于石墨文档(Android)客户端的案例分析
关于石墨文档(Android)客户端的案例分析 作业地址:[https://edu.cnblogs.com/campus/nenu/2016CS/homework/2505] 第一部分调研,评测 1. ...
- RabbitMQ安装记录(windows10)
RabbitMQ安装记录(windows10) 一.安装包准备 otp_win64_R16B03.exe(这里使用该版本,不支持ssl) otp_win64_19.0.exe(如果要开启ssl,请 ...
- [ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]
https://codeforces.com/contest/1143/problem/D D. The Beatles time limit per test 1 second memory lim ...