android 涨潮动画加载_Android附带涨潮动画效果的曲线报表绘制

写在前面

本文属于部分原创,实现安卓平台正弦曲线类报表绘制功能介绍,基于网络已有的曲线报表绘制类(LineGraphicView)自己添加了涨潮的渐变动画算法

最终效果图

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

废话少说,直接上源码

一、自定义View LineGraphicView,本类注释不算多,能搜到这篇文章的朋友看懂代码应该不会有障碍

import java.util.ArrayList;

import android.content.Context;

import android.content.res.Resources;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Paint.Style;

import android.graphics.Path;

import android.graphics.Point;

import android.util.AttributeSet;

import android.util.DisplayMetrics;

import android.view.View;

import android.view.WindowManager;

import android.view.animation.Animation;

import android.view.animation.Transformation;

class LineGraphicView extends View {

/**

* 公共部分

*/

private static final int CIRCLE_SIZE = 10;

private static int yPos = 0;

private float aniProgress;// 实现动画的值

private HistogramAnimation ani;

private static enum Linestyle {

Line, Curve

}

private Context mContext;

private Paint mPaint;

private Resources res;

private DisplayMetrics dm;

/**

* data

*/

private Linestyle mStyle = Linestyle.Curve;

private int canvasHeight;

private int canvasWidth;

private int bheight = 0;

private int blwidh;

private boolean isMeasure = true;

/**

* Y轴最大值

*/

private int maxValue;

/**

* Y轴间距值

*/

private int averageValue;

private int marginTop = 10;

private int marginBottom = 40;

/**

* 曲线上总点数

*/

private Point[] mPoints;

/**

* 纵坐标值

*/

private ArrayList yRawData;

/**

* 横坐标值

*/

private ArrayList xRawDatas;

private ArrayList xList = new ArrayList();// 记录每个x的值

private int spacingHeight;

public LineGraphicView(Context context) {

this(context, null);

}

public LineGraphicView(Context context, AttributeSet attrs) {

super(context, attrs);

this.mContext = context;

initView();

}

private void initView() {

this.res = mContext.getResources();

this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

dm = new DisplayMetrics();

WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

wm.getDefaultDisplay().getMetrics(dm);

ani = new HistogramAnimation();

ani.setDuration(1000);

startAnimation(ani);

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

if (isMeasure) {

this.canvasHeight = getHeight();

this.canvasWidth = getWidth();

if (bheight == 0)

bheight = (int) (canvasHeight - marginBottom);

blwidh = dip2px(30);

isMeasure = false;

}

}

@Override

protected void onDraw(Canvas canvas) {

mPaint.setColor(Color.GRAY);

drawAllXLine(canvas);

// 画直线(纵向)

drawAllYLine(canvas);

// 点的操作设置

mPoints = getPoints();

mPaint.setColor(Color.RED);

mPaint.setStrokeWidth(dip2px(0.2f));

mPaint.setStyle(Style.STROKE);

if (mStyle == Linestyle.Curve) {

drawScrollLine(canvas);

} else {

drawLine(canvas);

}

mPaint.setStyle(Style.FILL);

for (int i = 0; i < mPoints.length; i++) {

canvas.drawCircle(mPoints[i].x, mPoints[i].y, CIRCLE_SIZE / 2, mPaint);

}

}

/**

* 画所有横向表格,包括X轴

*/

private void drawAllXLine(Canvas canvas) {

for (int i = 0; i < spacingHeight + 1; i++) {

if (i == 0) {

yPos = bheight - (bheight / spacingHeight) * i + marginTop;

}

canvas.drawLine(blwidh, bheight - (bheight / spacingHeight) * i + marginTop, (canvasWidth - blwidh), bheight - (bheight / spacingHeight) * i + marginTop, mPaint);// Y坐标

drawText(String.valueOf(averageValue * i), blwidh / 2, bheight - (bheight / spacingHeight) * i + marginTop, canvas);

}

}

/**

* 画所有纵向表格,包括Y轴

*/

private void drawAllYLine(Canvas canvas) {

for (int i = 0; i < yRawData.size(); i++) {

xList.add(blwidh + (canvasWidth - blwidh) / yRawData.size() * i);

canvas.drawLine(blwidh + (canvasWidth - blwidh) / yRawData.size() * i, marginTop, blwidh + (canvasWidth - blwidh) / yRawData.size() * i, bheight + marginTop, mPaint);

drawText(xRawDatas.get(i), blwidh + (canvasWidth - blwidh) / yRawData.size() * i, bheight + dip2px(18), canvas);// X坐标

}

}

private void drawScrollLine(Canvas canvas) {

// 画曲线

int sh = 0;

int eh = 0;

Point startp = new Point();

Point endp = new Point();

for (int i = 0; i < mPoints.length - 1; i++) {

startp = mPoints[i];

endp = mPoints[i + 1];

if (aniProgress <= 0) {

sh = yPos;

eh = yPos;

} else if (aniProgress >= 1) {

sh = startp.y;

eh = endp.y;

} else {

sh = (int) ((int) startp.y + (yPos - startp.y) * (1 - aniProgress));

eh = (int) ((int) endp.y + (yPos - endp.y) * (1 - aniProgress));

}

startp.y = sh;

endp.y = eh;

int wt = (startp.x + endp.x) / 2;

Point p3 = new Point();

Point p4 = new Point();

p3.x = wt;

p3.y = sh;

p4.x = wt;

p4.y = eh;

Path path = new Path();

path.moveTo(startp.x, startp.y);// (51, 991),(172, 670)

path.cubicTo(p3.x, p3.y, p4.x, p4.y, endp.x, endp.y);

// canvas.drawPath(path, mPaint);

path.lineTo(endp.x, yPos);

path.lineTo(startp.x, yPos);

path.lineTo(startp.x, endp.y);

Paint paint = new Paint();

paint.setColor(Color.CYAN);

paint.setStyle(Paint.Style.FILL);

// 设置抗锯齿。

paint.setAntiAlias(true);

canvas.drawPath(path, paint);

}

}

private void drawLine(Canvas canvas) {

Point startp = new Point();

Point endp = new Point();

for (int i = 0; i < mPoints.length - 1; i++) {

startp = mPoints[i];

endp = mPoints[i + 1];

canvas.drawLine(startp.x, startp.y, endp.x, endp.y, mPaint);

}

}

private void drawText(String text, int x, int y, Canvas canvas) {

Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

p.setTextSize(dip2px(12));

p.setColor(Color.BLACK);

p.setTextAlign(Paint.Align.LEFT);

canvas.drawText(text, x, y, p);

}

private Point[] getPoints() {

Point[] points = new Point[yRawData.size()];

for (int i = 0; i < yRawData.size(); i++) {

int ph = bheight - (int) (bheight * (yRawData.get(i) / maxValue));

points[i] = new Point(xList.get(i), ph + marginTop);

}

return points;

}

public void setData(ArrayList yRawData, ArrayList xRawData, int maxValue, int averageValue) {

this.maxValue = maxValue;

this.averageValue = averageValue;

this.mPoints = new Point[yRawData.size()];

this.xRawDatas = xRawData;

this.yRawData = yRawData;

this.spacingHeight = maxValue / averageValue;

}

public void setTotalvalue(int maxValue) {

this.maxValue = maxValue;

}

public void setPjvalue(int averageValue) {

this.averageValue = averageValue;

}

public void setMargint(int marginTop) {

this.marginTop = marginTop;

}

public void setMarginb(int marginBottom) {

this.marginBottom = marginBottom;

}

public void setMstyle(Linestyle mStyle) {

this.mStyle = mStyle;

}

public void setBheight(int bheight) {

this.bheight = bheight;

}

/**

* 根据手机的分辨率从 dp 的单位 转成为 px(像素)

*/

private int dip2px(float dpValue) {

return (int) (dpValue * dm.density + 0.5f);

}

private class HistogramAnimation extends Animation {

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

super.applyTransformation(interpolatedTime, t);

aniProgress = interpolatedTime;

postInvalidate();

}

}二、入口页面调用这个自定义控件

import java.util.ArrayList;

import java.util.Random;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

@SuppressLint("ClickableViewAccessibility")

public class MainActivity extends Activity {

LineGraphicView tu;

ArrayList yList;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initViews();

}

private void initViews() {

tu = (LineGraphicView) findViewById(R.id.histogram);

yList = new ArrayList();

Random random = new Random();

for (int i = 0; i < 16; i++) {

yList.add(random.nextDouble() * 10);

}

ArrayList xRawDatas = new ArrayList();

for (int i = 0; i < 16; i++) {

xRawDatas.add(String.valueOf(i));

}

tu.setData(yList, xRawDatas, 10, 1);

}

}三、Mainactivity引用的activity_main.xml如下

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/histogram"

android:layout_width="wrap_content"

android:layout_height="match_parent" >

结束语

上述代码亲测有效,但也只是实现了基本功能,封装有待完善,欢迎宝宝们来学习交流~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392569.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Oracle Study之--Oracle等待事件(5)

Db file single write这个等待事件通常只发生在一种情况下&#xff0c;就是Oracle 更新数据文件头信息时&#xff08;比如发生Checkpoint&#xff09;。当这个等待事件很明显时&#xff0c;需要考虑是不是数据库中的数据文件数量太大&#xff0c;导致Oracle 需要花较长的时间来…

Java多线程-工具篇-BlockingQueue

Java多线程-工具篇-BlockingQueue 转载 http://www.cnblogs.com/jackyuj/archive/2010/11/24/1886553.html 这也是我们在多线程环境下&#xff0c;为什么需要BlockingQueue的原因。作为BlockingQueue的使用者&#xff0c;我们再也不需要关心什么时候需要阻塞线程&#xff0c;什…

怎么连接 mysql_怎样连接连接数据库

这个博客是为了说明怎么连接数据库第一步&#xff1a;肯定是要下载数据库&#xff0c;本人用的SqlServer2008&#xff0c;是从别人的U盘中拷来的。第二步&#xff1a;数据库的登录方式设置为混合登录&#xff0c;步骤如下&#xff1a;1.打开数据库这是数据库界面&#xff0c;要…

webstorm环境安装配置(less+autoprefixer)

node安装&#xff1a; 参考地址&#xff1a;http://www.runoob.com/nodejs/nodejs-install-setup.html 1.下载node安装包并完成安装 2.在开始菜单打开node 3.查看是否安装完成&#xff08;npm是node自带安装的&#xff09; 命令&#xff1a;node -v npm -v less安装&#xff1a…

如何解决ajax跨域问题(转)

由 于此前很少写前端的代码(哈哈&#xff0c;不合格的程序员啊)&#xff0c;最近项目中用到json作为系统间交互的手段&#xff0c;自然就伴随着众多ajax请求&#xff0c;随之而来的就是要解决 ajax的跨域问题。本篇将讲述一个小白从遇到跨域不知道是跨域问题&#xff0c;到知道…

判断输入的字符串是否为回文_刷题之路(九)--判断数字是否回文

Palindrome Number问题简介&#xff1a;判断输入数字是否是回文,不是返回0,负数返回0举例:1:输入: 121输出: true2:输入: -121输出: false解释: 回文为121-&#xff0c;所以负数都不符合3:输入: 10输出: false解释: 倒序为01&#xff0c;不符合要求解法一&#xff1a;这道题比较…

VirtualBox 虚拟机复制

本文简单讲两种情况下的复制方式 1 跨电脑复制 2 同一virtrul box下 虚拟机复制 ---------------------------------------------- 1 跨电脑复制 a虚拟机 是老的虚拟机 b虚拟机 是新的虚拟机 新虚拟机b 新建&#xff0c; 点击下一步会生成 相应的文件夹 找到老虚拟机a的 vdi 文…

mysql case快捷方法_MySQL case when使用方法实例解析

首先我们创建数据库表&#xff1a; CREATE TABLE t_demo (id int(32) NOT NULL,name varchar(255) DEFAULT NULL,age int(2) DEFAULT NULL,num int(3) DEFAULT NULL,PRIMARY KEY (id)) ENGINEInnoDB DEFAULT CHARSETutf8;插入数据&#xff1a;INSERT INTO t_demo VALUES (1, 张…

Java快速扫盲指南

文章转自&#xff1a;https://segmentfault.com/a/1190000004817465#articleHeader22 JDK&#xff0c;JRE和 JVM 的区别 JVM&#xff1a;java 虚拟机&#xff0c;负责将编译产生的字节码转换为特定机器代码&#xff0c;实现一次编译多处执行&#xff1b; JRE&#xff1a;java运…

mongo基本使用方法

mongo与关系型数据库的概念对比&#xff0c;区分大小写&#xff0c;_id为主键。 1.数据库操作 >show dbs #查看所有数据库 >use dbname #创建和切换数据库&#xff08;如果dbname存在则切换到该数据库&#xff0c;不存在则创建并切换到该数据库&#xff1b;新创建的…

python爬虫消费者与生产者_Condition版生产者与消费者模式

概述&#xff1a;在人工智能来临的今天&#xff0c;数据显得格外重要。在互联网的浩瀚大海洋中&#xff0c;隐藏着无穷的数据和信息。因此学习网络爬虫是在今天立足的一项必备技能。本路线专门针对想要从事Python网络爬虫的同学而准备的&#xff0c;并且是严格按照企业的标准定…

【Python包】安装teradatasql提示找不到pycryptodome模块错误(pycrypto,pycryptodome和crypto加密库)...

1.问题描述 安装teradatasql时&#xff0c;出现错误Could not find a version that satisfies the requirement pycryptodome&#xff0c;具体如下&#xff1a; 2.解决方法 查看Python第三方库目录$PYTHON_HOME/lib/python3.6/site-packages目录下没有pycryptodome目录&#xf…

简述yolo1-yolo3_使用YOLO框架进行对象检测的综合指南-第二部分

简述yolo1-yolo3In the last part, we understood what YOLO is and how it works. In this section, let us understand how to apply it using pre-trained weights and obtaining the results. This article is greatly inspired by Andrew Ng’s Deep Learning Specializat…

java cxf 调用wcf接口_JAVA 调用 WCF 服务流程

1. 将 WCF 服务发布到 Windows 服务(或者 IIS)此步骤的目的是为 WCF 服务搭建服务器&#xff0c;从而使服务相关的 Web Services 可以被 JAVA 客户端程序调用&#xff0c;具体步骤参考如下&#xff1a;(1) 发布到 Windows 服务(2) 发布到 IIS注&#xff1a;如果是将 WCF 服务…

gcp devops_将GCP AI平台笔记本用作可重现的数据科学环境

gcp devopsBy: Edward Krueger and Douglas Franklin.作者&#xff1a; 爱德华克鲁格 ( Edward Krueger)和道格拉斯富兰克林 ( Douglas Franklin) 。 In this article, we will cover how to set up a cloud computing instance to run Python with or without Jupyter Notebo…

迅为工业级iMX6Q开发板全新升级兼容PLUS版本|四核商业级|工业级|双核商业级...

软硬件全面升级 1. 新增Yocto项目的支持 增加opencv等软件功能 2. 新近推出i.MX6增强版本核心板&#xff08;PLUS&#xff09; -性能更强 四种核心板全兼容 四核商业级2G/16G&#xff1b;双核商业级1G/8G &#xff1b;四核工业级1G/8G &#xff1b;四核增强版(PLUS) 3. 豪华配…

电力现货市场现货需求_现货与情绪:现货铜市场中的自然语言处理与情绪评分

电力现货市场现货需求Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works with…

java做主成分分析_主成分分析PCA

PCA(Principal Component Analysis)&#xff0c;即主成分分析&#xff0c;一种常用于数据降维分析的方法。要理解PCA的原理&#xff0c;首先需要理解矩阵变换的意义。矩阵变换&#xff0c;有两种意义&#xff1a;1&#xff0c;在当前坐标系下的向量&#xff0c;经过矩阵M变换后…

个人学习进度(第十六周)

转载于:https://www.cnblogs.com/lhj1017/p/7011993.html

用python绘制箱线图_用卫星图像绘制世界海岸线图-第一部分

用python绘制箱线图At the UKHO, we use data science to gain valuable insight into the data sets we hold and further our understanding of the marine environment around us.在UKHO&#xff0c;我们使用数据科学获得对所拥有数据集的宝贵见解&#xff0c;并进一步了解周…