Guest User

Untitled

a guest
Jan 25th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.97 KB | None | 0 0
  1. /*
  2.   Copyright (c) 2009 Bonifaz Kaufmann.
  3.  
  4.   This library is free software; you can redistribute it and/or
  5.   modify it under the terms of the GNU Lesser General Public
  6.   License as published by the Free Software Foundation; either
  7.   version 2.1 of the License, or (at your option) any later version.
  8.  
  9.   This library is distributed in the hope that it will be useful,
  10.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.   Lesser General Public License for more details.
  13.  
  14.   You should have received a copy of the GNU Lesser General Public
  15.   License along with this library; if not, write to the Free Software
  16.   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17. */
  18. package edu.mit.media.hlt.sensorgraph;
  19.  
  20. import java.util.ArrayList;
  21.  
  22. import android.content.Context;
  23. import android.graphics.Bitmap;
  24. import android.graphics.Canvas;
  25. import android.graphics.Color;
  26. import android.graphics.Paint;
  27. import android.graphics.Point;
  28. import android.graphics.Bitmap.Config;
  29. import android.graphics.Paint.FontMetrics;
  30. import android.util.AttributeSet;
  31. import android.view.View;
  32.  
  33. public class GraphView extends View {
  34.  
  35.     private ArrayList<Point> points;
  36.    
  37.     private Bitmap mBitmap;
  38.     private Canvas mCanvas = new Canvas();
  39.     private Paint mPaint = new Paint();
  40.    
  41.     private int Grey;
  42.     private int Pink;
  43.    
  44.     public GraphView(Context context) {
  45.         super(context);
  46.         init();
  47.     }
  48.    
  49.     public GraphView(Context context, AttributeSet attrs) {
  50.         super(context, attrs);
  51.         init();
  52.     }
  53.    
  54.     private void init(){
  55.         points = new ArrayList<Point>();
  56.         Grey = Color.argb(50, 140, 140, 200);  
  57.         Pink = Color.argb(192, 204, 16, 64);       
  58.         mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
  59.     }
  60.    
  61.  
  62.    
  63.     public void addDataPoint(int value){       
  64.         int currentX = 0;
  65.         int currentY = value;
  66.        
  67.         if (points.size() > 0)
  68.             currentX = points.get(points.size() - 1).x + 1;
  69.        
  70.         points.add(new Point(currentX, currentY));
  71.        
  72.         // Triggers redrawing via onDraw()
  73.         invalidate();
  74.     }
  75.    
  76.     /**
  77.     public void setMaxValue(int max){
  78.         maxValue = max;
  79.         mScale = (int) - (mYOffset * (2.0f / maxValue));
  80.     }
  81.    
  82.     public void setSpeed(int speed){
  83.         mSpeed = speed;
  84.     }
  85.    
  86.    
  87.     @Override
  88.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  89.         mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
  90.         mCanvas.setBitmap(mBitmap);
  91.         mCanvas.drawColor(0xFFF4F2F3);
  92.         mYOffset = h;
  93.         mScale = (int) - (mYOffset * (2.0f / maxValue));
  94.         mWidth = w;
  95.         mLastX = mWidth;
  96.         super.onSizeChanged(w, h, oldw, oldh);
  97.     }**/
  98.  
  99.     @Override
  100.     protected void onDraw(Canvas canvas) {     
  101.         //Bitmap mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
  102.         //canvas.drawBitmap(mBitmap, 0, 0, null);
  103.         /**
  104.         // Initialization
  105.         int yLabelHeight = 20;
  106.         int yLabelSpacing = 20;
  107.            
  108.         int width = canvas.getWidth();
  109.         int height = canvas.getHeight();
  110.            
  111.         int xMin, xMax, yMin, yMax;
  112.         xMin = xMax = yMin = yMax = 0;
  113.            
  114.         boolean firstRun = true;
  115.        
  116.         for(Point p : points) {
  117.                
  118.             // Set xMax to the current x value
  119.             if (p.x > xMax || firstRun) {      
  120.                 xMax = (int) p.x;  
  121.             }
  122.                
  123.             // Set xMin to the current x value
  124.             if (p.x < xMin || firstRun) {  
  125.                 xMin = (int) p.x;  
  126.             }
  127.                
  128.             // Set yMax to the current y value
  129.             if (p.y > yMax || firstRun) {  
  130.                 yMax = (int) p.x;  
  131.             }
  132.                
  133.             // Set yMin to the current y value
  134.             if (p.y < yMin || firstRun) {  
  135.                 yMin = (int) p.x;  
  136.             }          
  137.                
  138.         }
  139.            
  140.         int xRange = Math.abs(xMax - xMin);
  141.         int yRange = Math.abs(yMax - yMin);
  142.         int yLabelNumber = width / yLabelSpacing;
  143.         int yLabelInterval = Math.round(yRange / yLabelNumber);
  144.         int yLabelStart = yMin + yMin % yLabelInterval;
  145.         int yLabelOffset = (width - (yLabelNumber - 1) * yLabelSpacing) / 2;
  146.            
  147.         FontMetrics metrics = mPaint.getFontMetrics();
  148.            
  149.         for (int i = 0; i < yLabelNumber; i++) {
  150.                
  151.             String label = Integer.toString(yLabelStart + i * yLabelInterval);
  152.             int center = yLabelOffset + (i * yLabelSpacing);
  153.                
  154.             mPaint.setColor(Grey);
  155.             canvas.drawText(label, center - (label.length() / 2), yLabelHeight / 2 + metrics.ascent / 2, mPaint);
  156.                
  157.         }**/
  158.        
  159.  
  160.         // Y-axis values
  161.         int startX = 0;
  162.         int startY = 0;
  163.         int stopX = 0;
  164.         int stopY = canvas.getHeight();
  165.  
  166.         // Initial values for the y-axis labels
  167.         int yMin, yMax;
  168.         yMin = yMax = 0;
  169.        
  170.         // Compute the minimum and maximum y-axis label values
  171.         for (Point p : points) {
  172.            
  173.             if (p.y > yMax) {
  174.                 yMax = (int) p.y;
  175.             }
  176.            
  177.             if (p.y < yMin) {
  178.                 yMin = (int) p.y;
  179.             }
  180.            
  181.         }
  182.        
  183.         // Paint properties for the y-axis labels
  184.         Paint labelPaint = new Paint();
  185.         labelPaint.setColor(Grey);
  186.         labelPaint.setTextSize(10);
  187.  
  188.     int labelX = startX;
  189.     int labelMinY = startY;
  190.     int labelMaxY = stopY      
  191.  
  192.         // Draw the y-axis labels
  193.         // canvas.drawText(Integer.toString(yMin), labelX, labelMinY, labelPaint);
  194.         // canvas.drawText(Integer.toString(yMax), labelX, labelMaxY, labelPaint);
  195.    
  196.        
  197.         // Paint properties for the y-axis
  198.         Paint p2 = new Paint();
  199.         p2.setColor(Pink);
  200.         p2.setStrokeWidth(5.0f);
  201.  
  202.         // Draw the y-axis
  203.         canvas.drawLine(startX, startY, stopX, stopY, p2);
  204.        
  205.         // Paint properties for the graph
  206.         Paint p3 = new Paint();
  207.         p3.setColor(Pink);
  208.         p3.setStrokeWidth(2.0f);
  209.         //p3.setShadowLayer(10.0f, 3.0f, 3.0f, Color.argb(192, 140, 140, 200));
  210.            
  211.         // Loop through the arraylist of points and draw a line from (last x, last y) to (current x, current y) every time
  212.         for (int i = 1; i < points.size(); i++) {
  213.             int x1 = points.get(i - 1).x;
  214.             int y1 = points.get(i - 1).y;
  215.             int x2 = points.get(i).x;
  216.             int y2 = points.get(i).y;
  217.             canvas.drawLine(x1, y1, x2, y2, p3);
  218.         }
  219.     }
  220. }
Add Comment
Please, Sign In to add comment