LineGraph.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * Created by Daniel Nadeau
  3. * daniel.nadeau01@gmail.com
  4. * danielnadeau.blogspot.com
  5. *
  6. * Licensed to the Apache Software Foundation (ASF) under one
  7. or more contributor license agreements. See the NOTICE file
  8. distributed with this work for additional information
  9. regarding copyright ownership. The ASF licenses this file
  10. to you under the Apache License, Version 2.0 (the
  11. "License"); you may not use this file except in compliance
  12. with the License. You may obtain a copy of the License at
  13. http://www.apache.org/licenses/LICENSE-2.0
  14. Unless required by applicable law or agreed to in writing,
  15. software distributed under the License is distributed on an
  16. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. KIND, either express or implied. See the License for the
  18. specific language governing permissions and limitations
  19. under the License.
  20. */
  21. package com.echo.holographlibrary;
  22. import android.content.Context;
  23. import android.graphics.Bitmap;
  24. import android.graphics.Bitmap.Config;
  25. import android.graphics.Canvas;
  26. import android.graphics.Color;
  27. import android.graphics.Paint;
  28. import android.graphics.Path;
  29. import android.graphics.Path.Direction;
  30. import android.graphics.Point;
  31. import android.graphics.PorterDuffXfermode;
  32. import android.graphics.Region;
  33. import android.util.AttributeSet;
  34. import android.util.TypedValue;
  35. import android.view.MotionEvent;
  36. import android.view.View;
  37. import java.util.ArrayList;
  38. public class LineGraph extends View {
  39. /**
  40. * The axis converter will be called, if the line graph loads / refreshed its axis.
  41. */
  42. public interface AxisDataConverter {
  43. /**
  44. * returns the x axis value for a given x coord.
  45. * @param x double, the x coord.
  46. * @return String, formatted value for the x position.
  47. */
  48. public String convertDataForX_Position(double x);
  49. /**
  50. * returns the y axis value for a given y coord.
  51. * @param y double, the y coord.
  52. * @return String, formatted value for the y position.
  53. */
  54. public String convertDataForY_Position(double y);
  55. }
  56. private final static int AXIS_LABEL_FONT_SIZE = 8;
  57. private ArrayList<Line> lines = new ArrayList<Line>();
  58. Paint paint = new Paint();
  59. private double minY = 0, minX = 0;
  60. private double maxY = 0, maxX = 0;
  61. private double rangeYRatio = 0;
  62. private double rangeXRatio = 0;
  63. private boolean isMaxYUserSet = false;
  64. private boolean isMaxXUserSet = false;
  65. private int lineToFill = -1;
  66. private int indexSelected = -1;
  67. private OnPointClickedListener listener;
  68. private Bitmap fullImage;
  69. private boolean shouldUpdate = false;
  70. static final float bottomPadding = 40, topPadding = 16;
  71. static final float leftPadding = 50, rightPadding = 16;
  72. static final float sidePadding = rightPadding + leftPadding;
  73. /**
  74. * Step = Axis Period
  75. */
  76. private float xAxisStep = 4;
  77. private float yAxisStep = 4;
  78. private AxisDataConverter converter;
  79. private Context mContext;
  80. public void setxAxisStep(float step){
  81. this.xAxisStep = step;
  82. }
  83. public void setYAxisStep(float step){
  84. this.yAxisStep = step;
  85. }
  86. public float getyAxisStep(){return this.yAxisStep;}
  87. public float getxAxisStep(){return this.xAxisStep;}
  88. /**
  89. * The axis converter will be called if the line graph refresh / load its axis labels.
  90. * @param conv AxisDataConverter
  91. */
  92. public void setConverter(AxisDataConverter conv){
  93. this.converter = conv;
  94. }
  95. /**
  96. * Constructor
  97. * @param context Context
  98. */
  99. public LineGraph(Context context){
  100. super(context);
  101. this.mContext = context;
  102. this.setWillNotDraw(false);
  103. }
  104. /**
  105. * Constructor
  106. * @param context Context
  107. * @param attrs AttributeSet
  108. */
  109. public LineGraph(Context context, AttributeSet attrs) {
  110. super(context, attrs);
  111. this.mContext = context;
  112. this.setWillNotDraw(false);
  113. }
  114. /**
  115. * Removes all lines.
  116. */
  117. public void removeAllLines(){
  118. while (lines.size() > 0){
  119. lines.remove(0);
  120. }
  121. shouldUpdate = true;
  122. //postInvalidate();
  123. }
  124. /**
  125. * Add a line.
  126. * @param line {@link Line Line}
  127. */
  128. public void addLine(Line line) {
  129. lines.add(line);
  130. shouldUpdate = true;
  131. //postInvalidate();
  132. }
  133. /**
  134. * Adds a new point to a {@link Line lin}
  135. * @param lineIndex the index of the line
  136. * @param x double, the x coord.
  137. * @param y double, the y coord.
  138. */
  139. public void addPointToLine(int lineIndex, double x, double y){
  140. addPointToLine(lineIndex, (float) x, (float) y);
  141. }
  142. public void addPointToLine(int lineIndex, float x, float y){
  143. LinePoint p = new LinePoint(x, y);
  144. addPointToLine(lineIndex, p);
  145. }
  146. public double getRangeYRatio(){
  147. return rangeYRatio;
  148. }
  149. public void setRangeYRatio(double rr){
  150. this.rangeYRatio = rr;
  151. }
  152. public double getRangeXRatio(){
  153. return rangeXRatio;
  154. }
  155. public void setRangeXRatio(double rr){
  156. this.rangeXRatio = rr;
  157. }
  158. public void addPointToLine(int lineIndex, LinePoint point){
  159. Line line = getLine(lineIndex);
  160. line.addPoint(point);
  161. lines.set(lineIndex, line);
  162. resetLimits();
  163. shouldUpdate = true;
  164. //postInvalidate();
  165. }
  166. public void addPointsToLine(int lineIndex, LinePoint[] points){
  167. Line line = getLine(lineIndex);
  168. for(LinePoint point : points){
  169. line.addPoint(point);
  170. }
  171. lines.set(lineIndex, line);
  172. resetLimits();
  173. shouldUpdate = true;
  174. //postInvalidate();
  175. }
  176. public void removeAllPointsAfter(int lineIndex, double x){
  177. removeAllPointsBetween(lineIndex, x, getMaxX());
  178. }
  179. public void removeAllPointsBefore(int lineIndex, double x){
  180. removeAllPointsBetween(lineIndex, getMinX(), x);
  181. }
  182. public void removeAllPointsBetween(int lineIndex, double startX, double finishX){
  183. Line line = getLine(lineIndex);
  184. LinePoint[] pts = new LinePoint[line.getPoints().size()];
  185. pts = line.getPoints().toArray(pts);
  186. for(LinePoint point : pts){
  187. if(point.getX() >= startX && point.getX() <= finishX)
  188. line.removePoint(point);
  189. }
  190. lines.set(lineIndex, line);
  191. resetLimits();
  192. shouldUpdate = true;
  193. //postInvalidate();
  194. }
  195. public void removePointsFromLine(int lineIndex, LinePoint[] points){
  196. Line line = getLine(lineIndex);
  197. for(LinePoint point : points){
  198. line.removePoint(point);
  199. }
  200. lines.set(lineIndex, line);
  201. resetLimits();
  202. shouldUpdate = true;
  203. //postInvalidate();
  204. }
  205. public void removePointFromLine(int lineIndex, float x, float y){
  206. LinePoint p = null;
  207. Line line = getLine(lineIndex);
  208. p = line.getPoint(x, y);
  209. removePointFromLine(lineIndex, p);
  210. }
  211. public void removePointFromLine(int lineIndex, LinePoint point){
  212. Line line = getLine(lineIndex);
  213. line.removePoint(point);
  214. lines.set(lineIndex, line);
  215. resetLimits();
  216. shouldUpdate = true;
  217. //postInvalidate();
  218. }
  219. /**
  220. * Resets the y axis limits
  221. */
  222. public void resetYLimits(){
  223. double range = getMaxY() - getMinY();
  224. setRangeY(getMinY()-range*getRangeYRatio(), getMaxY()+range*getRangeYRatio());
  225. isMaxYUserSet = false;
  226. }
  227. /**
  228. * Resets the x axis limits
  229. */
  230. public void resetXLimits(){
  231. double range = getMaxX() - getMinX();
  232. setRangeX(getMinX()-range*getRangeXRatio(), getMaxX()+range*getRangeXRatio());
  233. isMaxXUserSet = false;
  234. }
  235. public void resetLimits() {
  236. resetYLimits();
  237. resetXLimits();
  238. }
  239. public ArrayList<Line> getLines() {
  240. return lines;
  241. }
  242. public void setLineToFill(int indexOfLine) {
  243. this.lineToFill = indexOfLine;
  244. shouldUpdate = true;
  245. //postInvalidate();
  246. }
  247. public int getLineToFill(){
  248. return lineToFill;
  249. }
  250. public void setLines(ArrayList<Line> lines) {
  251. this.lines = lines;
  252. }
  253. public Line getLine(int index) {
  254. return lines.get(index);
  255. }
  256. public int getSize(){
  257. return lines.size();
  258. }
  259. /**
  260. * Sets the y axis range (minimal and maximal value for the y axis)
  261. * @param min float
  262. * @param max float
  263. */
  264. public void setRangeY(float min, float max) {
  265. minY = min;
  266. maxY = max;
  267. isMaxYUserSet = true;
  268. }
  269. /**
  270. * Sets the y axis range (minimal and maximal value for the y axis)
  271. * @param min double
  272. * @param max double
  273. */
  274. public void setRangeY(double min, double max){
  275. minY = min;
  276. maxY = max;
  277. isMaxYUserSet = true;
  278. }
  279. /**
  280. * Sets the x axis range (minimal and maximal value for the x axis)
  281. * @param min float
  282. * @param max float
  283. */
  284. public void setRangeX(float min, float max) {
  285. minX = min;
  286. maxX = max;
  287. isMaxXUserSet = true;
  288. }
  289. /**
  290. * Sets the x axis range (minimal and maximal value for the x axis)
  291. * @param min double
  292. * @param max double
  293. */
  294. public void setRangeX(double min, double max){
  295. minX = min;
  296. maxX = max;
  297. isMaxXUserSet = true;
  298. }
  299. public double getMaxY(){
  300. if (isMaxYUserSet)return maxY;
  301. double max = lines.get(0).getPoint(0).getY();
  302. for (Line line : lines){
  303. for (LinePoint point : line.getPoints()){
  304. max = point.getY() > max ? point.getY() : max;
  305. }
  306. }
  307. maxY = max;
  308. return maxY;
  309. }
  310. public double getMinY(){
  311. if (isMaxYUserSet)return minY;
  312. double min = lines.get(0).getPoint(0).getY();
  313. for (Line line : lines){
  314. for (LinePoint point : line.getPoints()){
  315. min = point.getY() < min ? point.getY() : min;
  316. }
  317. }
  318. minY = min;
  319. return minY;
  320. }
  321. public double getMinLimY(){
  322. return minY;
  323. }
  324. public double getMaxLimY(){
  325. return maxY;
  326. }
  327. public double getMinLimX(){
  328. if (isMaxXUserSet) {
  329. return minX;
  330. }
  331. else {
  332. return getMinX();
  333. }
  334. }
  335. public double getMaxLimX(){
  336. if (isMaxXUserSet) {
  337. return maxX;
  338. }
  339. else {
  340. return getMaxX();
  341. }
  342. }
  343. public double getMaxX(){
  344. double max = lines.size() > 0 ? lines.get(0).getPoint(0).getX() : 0;
  345. for (Line line : lines){
  346. for (LinePoint point : line.getPoints()){
  347. max =Math.max(point.getX(), max);// point.getX() > max ? point.getX() : max;
  348. }
  349. }
  350. maxX = max;
  351. return maxX;
  352. }
  353. public double getMinX(){
  354. double min = lines.size() > 0 ? lines.get(0).getPoint(0).getX() : 0;
  355. for (Line line : lines){
  356. for (LinePoint point : line.getPoints()){
  357. min =Math.min(point.getX(), min);// point.getX() < min ? point.getX() : min;
  358. }
  359. }
  360. minX = min;
  361. return minX;
  362. }
  363. /**
  364. * Returns the title for the x coordinate by calling the converter.
  365. * @param x double, the x coord.
  366. * @return String, the title
  367. */
  368. private String getX_AxisTitle(double x){
  369. if (this.converter == null)return "" + (long)x;
  370. return this.converter.convertDataForX_Position(x);
  371. }
  372. /**
  373. * Returns the title for the y coordinate by calling the converter.
  374. * @param y double, the y coord.
  375. * @return String, the title
  376. */
  377. private String getY_AxisTitle(double y){
  378. if (this.converter == null)return "" + (long)y;
  379. return this.converter.convertDataForY_Position(y);
  380. }
  381. @Override
  382. public void onDraw(Canvas ca) {
  383. super.onDraw(ca);
  384. if (this.lines == null || this.lines.size() == 0) return;
  385. if (fullImage == null || shouldUpdate) {
  386. fullImage = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
  387. Canvas canvas = new Canvas(fullImage);
  388. paint.reset();
  389. Path path = new Path();
  390. float usableHeight = getHeight() - bottomPadding - topPadding;
  391. float usableWidth = getWidth() - 2*sidePadding;
  392. double maxY = getMaxLimY();
  393. double minY = getMinLimY();
  394. double maxX = getMaxLimX();
  395. double minX = getMinLimX();
  396. // DRAW THE BACKGROUND
  397. //this.drawBackground(canvas);
  398. paint.setAntiAlias(true);
  399. // DRAW THE AXIS
  400. this.drawAxis(canvas);
  401. // DRAW LINES
  402. for (Line line : lines){
  403. int count = 0;
  404. float lastXPixels = 0, newYPixels = 0;
  405. float lastYPixels = 0, newXPixels = 0;
  406. paint.setColor(line.getColor());
  407. paint.setStrokeWidth(getStrokeWidth(line));
  408. for (LinePoint p : line.getPoints()){
  409. float yPercent =(float) ((p.getY()-minY)/(maxY - minY));
  410. float xPercent = (float)((p.getX()-minX)/(maxX - minX));
  411. if (count == 0){
  412. lastXPixels = sidePadding + (xPercent*usableWidth);
  413. lastYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  414. } else {
  415. newXPixels = sidePadding + (xPercent*usableWidth);
  416. newYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  417. canvas.drawLine(lastXPixels, lastYPixels, newXPixels, newYPixels, paint);
  418. lastXPixels = newXPixels;
  419. lastYPixels = newYPixels;
  420. }
  421. count++;
  422. }
  423. }
  424. int pointCount = 0;
  425. // DRAW POINTS
  426. for (Line line : lines){
  427. paint.setColor(line.getColor());
  428. paint.setStrokeWidth(getStrokeWidth(line));
  429. paint.setStrokeCap(Paint.Cap.ROUND);
  430. if (line.isShowingPoints()){
  431. for (LinePoint p : line.getPoints()){
  432. float yPercent =(float) ((p.getY()-minY)/(maxY - minY));
  433. float xPercent =(float) ((p.getX()-minX)/(maxX - minX));
  434. float xPixels = sidePadding + (xPercent*usableWidth);
  435. float yPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  436. int outerRadius;
  437. if (line.isUsingDips()) {
  438. outerRadius = getPixelForDip(line.getStrokeWidth() + 4);
  439. }
  440. else {
  441. outerRadius = line.getStrokeWidth() + 4;
  442. }
  443. int innerRadius = outerRadius / 2;
  444. paint.setColor(p.getColor());
  445. canvas.drawCircle(xPixels,(float) yPixels, outerRadius, paint);
  446. paint.setColor(Color.WHITE);
  447. canvas.drawCircle(xPixels,(float) yPixels, innerRadius, paint);
  448. Path path2 = new Path();
  449. path2.addCircle(xPixels,(float) yPixels, 30, Direction.CW);
  450. p.setPath(path2);
  451. p.setRegion(new Region((int)(xPixels-30), (int)(yPixels-30), (int)(xPixels+30), (int)(yPixels+30)));
  452. if (indexSelected == pointCount && listener != null){
  453. paint.setColor(p.getColor());
  454. paint.setAlpha(100);
  455. canvas.drawPath(p.getPath(), paint);
  456. paint.setAlpha(255);
  457. }
  458. pointCount++;
  459. }
  460. }
  461. }
  462. shouldUpdate = false;
  463. }
  464. ca.drawBitmap(fullImage, 0, 0, null);
  465. }
  466. /**
  467. * Draw the x & y axis
  468. * @param canvas Canvas
  469. */
  470. private void drawAxis(Canvas canvas){
  471. //double maxX = getMaxLimX();
  472. //double minX = getMinLimX();
  473. //float usableWidth = getWidth() - 2*sidePadding;
  474. //float usableHeight = getHeight() - bottomPadding - topPadding;
  475. float yPixels = getHeight() - (bottomPadding - (10));
  476. // DRAW SEPERATOR
  477. paint.setColor(Color.BLACK);
  478. paint.setAlpha(50);
  479. paint.setAntiAlias(true);
  480. // x Axis
  481. canvas.drawLine(leftPadding ,yPixels, getWidth()-sidePadding, yPixels, paint);
  482. // y Axis
  483. canvas.drawLine(leftPadding ,topPadding, leftPadding, yPixels, paint);
  484. paint.setAlpha(255);
  485. this.drawAxisLabel(canvas);
  486. }
  487. /**
  488. * Draw the x & y axis labels.
  489. * @param canvas Canvas
  490. */
  491. private void drawAxisLabel(Canvas canvas){
  492. this.paint.setTextSize(AXIS_LABEL_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  493. double maxX = getMaxLimX();
  494. double minX = getMinLimX();
  495. float usableWidth = getWidth() - 2*sidePadding;
  496. float usableHeight = getHeight() - bottomPadding - topPadding;
  497. float yPixels = getHeight() - (bottomPadding - (10));
  498. // Draw y-axis label text
  499. double step = Math.max(1., (maxY - minY) / (Math.max(1., yAxisStep)));
  500. double v = 0;
  501. for (double y = minY; y <= maxY; y+=step){
  502. double yPercent = (y-minY)/(maxY - minY);
  503. double newYPixels = topPadding + (yPercent*usableHeight);
  504. canvas.drawLine((float)leftPadding,(float)newYPixels,(float)leftPadding-5.f,(float)newYPixels, paint);
  505. String title = this.getY_AxisTitle(maxY - v);
  506. float textwidth = (this.paint.measureText(title));
  507. canvas.drawText(title, 5.f ,(float)newYPixels + (textwidth/2), this.paint);
  508. v+=step;
  509. }
  510. this.paint.setTextSize(AXIS_LABEL_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  511. // Draw x-axis label text
  512. step = Math.max(1, (maxX - minX) / (Math.max(1, xAxisStep)));
  513. for (double x = minX; x <= maxX; x+=step){
  514. double xPercent = (x-minX)/(maxX - minX);
  515. double newXPixels = sidePadding + (xPercent*usableWidth);
  516. canvas.drawLine((float)newXPixels,(float) yPixels + 5,(float) newXPixels,(float) yPixels, paint);
  517. String title = this.getX_AxisTitle(x);
  518. float textwidth = (this.paint.measureText(title));
  519. float x_Coord = Math.max(0.f, (float) (newXPixels - (textwidth / 2)));
  520. x_Coord = Math.min(x_Coord, getWidth() - rightPadding);
  521. float y_Coord = (float) (yPixels + ((bottomPadding - 10) / 1));
  522. canvas.drawText(title, x_Coord , y_Coord, this.paint);
  523. }
  524. }
  525. /**
  526. * Draw the lined background.
  527. * @param canvas Canvas
  528. */
  529. private void drawBackground(Canvas canvas){
  530. paint.reset();
  531. float usableHeight = getHeight() - bottomPadding - topPadding;
  532. float usableWidth = getWidth() - 2*sidePadding;
  533. double maxY = getMaxLimY();
  534. double minY = getMinLimY();
  535. double maxX = getMaxLimX();
  536. double minX = getMinLimX();
  537. Path path = new Path();
  538. // DRAW THE BACKGROUND
  539. int lineCount = 0;
  540. for (Line line : lines){
  541. int count = 0;
  542. float firstXPixels = 0, lastXPixels = 0, newYPixels = 0;
  543. float lastYPixels = 0, newXPixels = 0;
  544. if (lineCount == lineToFill){
  545. paint.setColor(Color.BLACK);
  546. paint.setAlpha(30);
  547. paint.setStrokeWidth(2);
  548. for (int i = 10; i-getWidth() < getHeight(); i = i+20){
  549. canvas.drawLine(i, getHeight()-bottomPadding, 0, getHeight()-bottomPadding-i, paint);
  550. }
  551. paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
  552. for (LinePoint p : line.getPoints()){
  553. float yPercent =(float) ((p.getY()-minY)/(maxY - minY));
  554. float xPercent =(float) ((p.getX()-minX)/(maxX - minX));
  555. if (count == 0){
  556. lastXPixels = sidePadding + (xPercent*usableWidth);
  557. lastYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  558. firstXPixels = lastXPixels;
  559. path.moveTo(lastXPixels, lastYPixels);
  560. } else {
  561. newXPixels = sidePadding + (xPercent*usableWidth);
  562. newYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  563. path.lineTo(newXPixels, newYPixels);
  564. Path pa = new Path();
  565. pa.moveTo(lastXPixels, lastYPixels);
  566. pa.lineTo(newXPixels, newYPixels);
  567. pa.lineTo(newXPixels, 0);
  568. pa.lineTo(lastXPixels, 0);
  569. pa.close();
  570. canvas.drawPath(pa, paint);
  571. lastXPixels = newXPixels;
  572. lastYPixels = newYPixels;
  573. }
  574. count++;
  575. }
  576. path.reset();
  577. path.moveTo(0, getHeight()-bottomPadding);
  578. path.lineTo(sidePadding, getHeight()-bottomPadding);
  579. path.lineTo(sidePadding, 0);
  580. path.lineTo(0, 0);
  581. path.close();
  582. canvas.drawPath(path, paint);
  583. path.reset();
  584. path.moveTo(getWidth(), getHeight()-bottomPadding);
  585. path.lineTo(getWidth()-sidePadding, getHeight()-bottomPadding);
  586. path.lineTo(getWidth()-sidePadding, 0);
  587. path.lineTo(getWidth(), 0);
  588. path.close();
  589. canvas.drawPath(path, paint);
  590. }
  591. lineCount++;
  592. }
  593. this.paint.reset();
  594. }
  595. private int getStrokeWidth(Line line) {
  596. int strokeWidth;
  597. if (line.isUsingDips()) {
  598. strokeWidth = getPixelForDip(line.getStrokeWidth());
  599. }
  600. else {
  601. strokeWidth = line.getStrokeWidth();
  602. }
  603. return strokeWidth;
  604. }
  605. private int getPixelForDip(int dipValue) {
  606. return (int) TypedValue.applyDimension(
  607. TypedValue.COMPLEX_UNIT_DIP,
  608. dipValue,
  609. getResources().getDisplayMetrics());
  610. }
  611. @Override
  612. public boolean onTouchEvent(MotionEvent event) {
  613. Point point = new Point();
  614. point.x = (int) event.getX();
  615. point.y = (int) event.getY();
  616. int count = 0;
  617. int lineCount = 0;
  618. int pointCount = 0;
  619. Region r = new Region();
  620. for (Line line : lines){
  621. pointCount = 0;
  622. for (LinePoint p : line.getPoints()){
  623. if (p.getPath() != null && p.getRegion() != null){
  624. r.setPath(p.getPath(), p.getRegion());
  625. if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
  626. indexSelected = count;
  627. } else if (event.getAction() == MotionEvent.ACTION_UP){
  628. if (r.contains((int)point.x,(int) point.y) && listener != null){
  629. listener.onClick(lineCount, pointCount);
  630. }
  631. indexSelected = -1;
  632. }
  633. }
  634. pointCount++;
  635. count++;
  636. }
  637. lineCount++;
  638. }
  639. if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP){
  640. shouldUpdate = true;
  641. postInvalidate();
  642. }
  643. return true;
  644. }
  645. @Override
  646. protected void onDetachedFromWindow()
  647. {
  648. if(fullImage != null)
  649. fullImage.recycle();
  650. super.onDetachedFromWindow();
  651. //postInvalidate();
  652. }
  653. @Override
  654. protected void onSizeChanged (int w, int h, int oldw, int oldh){
  655. super.onSizeChanged(w,h,oldw,oldh);
  656. //postInvalidate();
  657. }
  658. @Override
  659. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  660. super.onLayout(changed, l, t, r, b);
  661. if(fullImage != null)
  662. fullImage = null;
  663. postInvalidate();
  664. }
  665. /**
  666. * Set the point click listener, which will be called on clicking a point.
  667. * @param listener OnPointClickedListener
  668. */
  669. public void setOnPointClickedListener(OnPointClickedListener listener) {
  670. this.listener = listener;
  671. }
  672. /**
  673. * OnPointClickedListener will be called, if the user clicks a point.
  674. */
  675. public interface OnPointClickedListener {
  676. abstract void onClick(int lineIndex, int pointIndex);
  677. }
  678. }