Setting下的自定义控件LinearColorBar
版本:1.0
/**
*
*/
package com.example.settingscustomviewdemo.widget;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;public class LinearColorBar extends LinearLayout {static final int LEFT_COLOR = 0xff0099cc;static final int MIDDLE_COLOR = 0xff0099cc;static final int RIGHT_COLOR = 0xff888888;private float mRedRatio;private float mYellowRatio;private float mGreenRatio;private boolean mShowingGreen;final Rect mRect = new Rect();final Paint mPaint = new Paint();int mLastInterestingLeft, mLastInterestingRight;int mLineWidth;final Path mColorPath = new Path();final Path mEdgePath = new Path();final Paint mColorGradientPaint = new Paint();final Paint mEdgeGradientPaint = new Paint();public LinearColorBar(Context context, AttributeSet attrs) {super(context, attrs);setWillNotDraw(false);mPaint.setStyle(Paint.Style.FILL);mColorGradientPaint.setStyle(Paint.Style.FILL);mColorGradientPaint.setAntiAlias(true);mEdgeGradientPaint.setStyle(Paint.Style.STROKE);mLineWidth = getResources().getDisplayMetrics().densityDpi >= DisplayMetrics.DENSITY_HIGH? 2 : 1;mEdgeGradientPaint.setStrokeWidth(mLineWidth);mEdgeGradientPaint.setAntiAlias(true);}public void setRatios(float red, float yellow, float green) {mRedRatio = red;mYellowRatio = yellow;mGreenRatio = green;invalidate();}public void setShowingGreen(boolean showingGreen) {if (mShowingGreen != showingGreen) {mShowingGreen = showingGreen;updateIndicator();invalidate();}}private void updateIndicator() {int off = getPaddingTop() - getPaddingBottom();if (off < 0) off = 0;mRect.top = off;mRect.bottom = getHeight();if (mShowingGreen) {mColorGradientPaint.setShader(new LinearGradient(0, 0, 0, off-2, RIGHT_COLOR&0xffffff, RIGHT_COLOR, Shader.TileMode.CLAMP));} else {mColorGradientPaint.setShader(new LinearGradient(0, 0, 0, off-2, MIDDLE_COLOR&0xffffff, MIDDLE_COLOR, Shader.TileMode.CLAMP));}mEdgeGradientPaint.setShader(new LinearGradient(0, 0, 0, off/2, 0x00a0a0a0, 0xffa0a0a0, Shader.TileMode.CLAMP));}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);updateIndicator();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int width = getWidth();int left = 0;int right = left + (int)(width*mRedRatio);int right2 = right + (int)(width*mYellowRatio);int right3 = right2 + (int)(width*mGreenRatio);int indicatorLeft, indicatorRight;if (mShowingGreen) {indicatorLeft = right2;indicatorRight = right3;} else {indicatorLeft = right;indicatorRight = right2;}if (mLastInterestingLeft != indicatorLeft || mLastInterestingRight != indicatorRight) {mColorPath.reset();mEdgePath.reset();if (indicatorLeft < indicatorRight) {final int midTopY = mRect.top;final int midBottomY = 0;final int xoff = 2;mColorPath.moveTo(indicatorLeft, mRect.top);mColorPath.cubicTo(indicatorLeft, midBottomY,-xoff, midTopY,-xoff, 0);mColorPath.lineTo(width+xoff-1, 0);mColorPath.cubicTo(width+xoff-1, midTopY,indicatorRight, midBottomY,indicatorRight, mRect.top);mColorPath.close();final float lineOffset = mLineWidth+.5f;mEdgePath.moveTo(-xoff+lineOffset, 0);mEdgePath.cubicTo(-xoff+lineOffset, midTopY,indicatorLeft+lineOffset, midBottomY,indicatorLeft+lineOffset, mRect.top);mEdgePath.moveTo(width+xoff-1-lineOffset, 0);mEdgePath.cubicTo(width+xoff-1-lineOffset, midTopY,indicatorRight-lineOffset, midBottomY,indicatorRight-lineOffset, mRect.top);}mLastInterestingLeft = indicatorLeft;mLastInterestingRight = indicatorRight;}if (!mEdgePath.isEmpty()) {canvas.drawPath(mEdgePath, mEdgeGradientPaint);canvas.drawPath(mColorPath, mColorGradientPaint);}if (left < right) {mRect.left = left;mRect.right = right;mPaint.setColor(LEFT_COLOR);canvas.drawRect(mRect, mPaint);width -= (right-left);left = right;}right = right2;if (left < right) {mRect.left = left;mRect.right = right;mPaint.setColor(MIDDLE_COLOR);canvas.drawRect(mRect, mPaint);width -= (right-left);left = right;}right = left + width;if (left < right) {mRect.left = left;mRect.right = right;mPaint.setColor(RIGHT_COLOR);canvas.drawRect(mRect, mPaint);}}
}
int width = getWidth();//获取组件宽度int left = 0;//int right = left + (int) (width * mRedRatio);//int right2 = right + (int) (width * mYellowRatio);//int right3 = right2 + (int) (width * mGreenRatio);//int indicatorLeft, indicatorRight;if (mShowingGreen) {indicatorLeft = right2;indicatorRight = right3;} else {indicatorLeft = right;indicatorRight = right2;}
mColorPath.moveTo(indicatorLeft, mRect.top);mColorPath.cubicTo(indicatorLeft, midBottomY, -xoff, midTopY,-xoff, 0);mColorPath.lineTo(width + xoff - 1, 0);mColorPath.cubicTo(width + xoff - 1, midTopY, indicatorRight,midBottomY, indicatorRight, mRect.top);mColorPath.close();
final float lineOffset = mLineWidth + .5f;mEdgePath.moveTo(-xoff + lineOffset, 0);mEdgePath.cubicTo(-xoff + lineOffset, midTopY, indicatorLeft+ lineOffset, midBottomY, indicatorLeft + lineOffset,mRect.top);mEdgePath.moveTo(width + xoff - 1 - lineOffset, 0);mEdgePath.cubicTo(width + xoff - 1 - lineOffset, midTopY,indicatorRight - lineOffset, midBottomY, indicatorRight- lineOffset, mRect.top);
if (left < right) {mRect.left = left;mRect.right = right;mPaint.setColor(LEFT_COLOR);canvas.drawRect(mRect, mPaint);width -= (right - left);left = right;}right = right2;if (left < right) {mRect.left = left;mRect.right = right;mPaint.setColor(MIDDLE_COLOR);canvas.drawRect(mRect, mPaint);width -= (right - left);left = right;}right = left + width;if (left < right) {mRect.left = left;mRect.right = right;mPaint.setColor(RIGHT_COLOR);canvas.drawRect(mRect, mPaint);}
Setting下的自定义控件LinearColorBar相关推荐
- 3.1-3.31推荐文章汇总
3.1-3.31推荐文章汇总 [Eclipse AST]AST的创建 刘伟 Android WebKit HTML主资源加载过程 谭海燕 HTML5物理游戏开发 - 越野山地自行车(一)建立各 ...
- java settings文件夹_windows下打开.m2文件夹,没有找到setting.xml
Frank作家 2018-03-15 10:07 已采纳 那就自己创建一个 手动创建本地仓库的存放路径,例如:D:\Java\m2\repository: 在windows的环境变量中增加了新的用户变 ...
- java m2文件放哪里_windows下打开.m2文件夹,没有找到setting.xml
那就自己创建一个 手动创建本地仓库的存放路径,例如:D:\Java\m2\repository: 在windows的环境变量中增加了新的用户变量M2_REPO,其对应的值为D:\Java\m2\rep ...
- 如何在fluid中添加自定义控件
fluid中已经有它自己自带的控件,那如何让我们自定义的控件放到fluid中呢,这里有两种方法,一种是修改fluid代码,一种是以另外的控件来代替自定义的控件,只是在代替控件属性的class选项下填写 ...
- UA MATH567 高维统计专题1 稀疏信号及其恢复6 随机设计矩阵下LASSO的估计误差
UA MATH567 高维统计专题1 稀疏信号及其恢复6 随机设计矩阵下LASSO的估计误差 上一讲我们推导了noisy setting下LASSO估计误差的阶O(slogd/n)O(\sqrt{s ...
- 自定义控件只允许输入Decimal和int类型字符串
为什么80%的码农都做不了架构师?>>> 自定义控件中只放了一个TextBox控件,并在TextBox下利用自定义控件的Paint画了一条线,然后给TextBox做了3个自定义 ...
- WinCE下直接启动自己应用程序的方法
WinCE下直接启动自己应用程序的方法 WinCE下直接启动自己应用程序的方法 其实让一个程序在wince里启动和windows里差不多,直接设置其为启动项,这个有几个方法.一个就是制作一个快捷方式, ...
- openwrt源码分析_Luci流程分析(openwrt下)
1.页面请求: 1.1.代码结构 在openwrt文件系统中,lua语言的代码不要编译,类似一种脚本语言被执行,还有一些uhttpd服务器的主目录,它们是: /www/index.html cgi-b ...
- 一种崭新的长尾分布下分类问题的通用算法|NeurIPS 2020
↑ 点击蓝字 关注视学算法 作者丨汤凯华@知乎 来源丨https://zhuanlan.zhihu.com/p/259569655 编辑丨极市平台 本文主要介绍我们组今年被NeurIPS 2020接收 ...
最新文章
- VRRP在企业网中的应用(H3C设备)
- ssh mysql环境搭建 myeclipse_MyEclipse整合ssh三大框架环境搭载用户注册源码下载
- 基于loudmouth的XMPP客户端DEMO
- Python+selenium 自动化-mac下安装配置chrome驱动方法
- Python数据结构与算法(第一天)
- [Asp.net]站点地图SiteMap
- 继续Bargaining
- 清明出游,在高速上堵了16个小时。
- [转载] 使用selenium_一日一技:使用Selenium的浏览器自动化
- Mysql缺少可执行的命令
- linux下目录的基本命令
- 我应该通过软件或硬件调整音量以获得最佳声音吗?
- Interior-point methods(内点法)学习笔记
- 专访邱锡鹏:人工智能开源社区的「先行者」
- Java对pdf文件进行压缩打包并执行下载
- 京东云安装docker
- 反算坐标方位角小程序(c#)
- Excel信息批量替换Word模板生成新文件
- 植物识别扫一扫的软件有哪些?植物识别软件推荐。
- ubuntu删除旧的linux内核