Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2009 Bonifaz Kaufmann.
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- package edu.mit.media.hlt.sensorgraph;
- import java.util.ArrayList;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.Bitmap.Config;
- import android.graphics.Paint.FontMetrics;
- import android.util.AttributeSet;
- import android.view.View;
- public class GraphView extends View {
- private ArrayList<Point> points;
- private Bitmap mBitmap;
- private Canvas mCanvas = new Canvas();
- private Paint mPaint = new Paint();
- private int Grey;
- private int Pink;
- public GraphView(Context context) {
- super(context);
- init();
- }
- public GraphView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- private void init(){
- points = new ArrayList<Point>();
- Grey = Color.argb(50, 140, 140, 200);
- Pink = Color.argb(192, 204, 16, 64);
- mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
- }
- public void addDataPoint(int value){
- int currentX = 0;
- int currentY = value;
- if (points.size() > 0)
- currentX = points.get(points.size() - 1).x + 1;
- points.add(new Point(currentX, currentY));
- // Triggers redrawing via onDraw()
- invalidate();
- }
- /**
- public void setMaxValue(int max){
- maxValue = max;
- mScale = (int) - (mYOffset * (2.0f / maxValue));
- }
- public void setSpeed(int speed){
- mSpeed = speed;
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
- mCanvas.setBitmap(mBitmap);
- mCanvas.drawColor(0xFFF4F2F3);
- mYOffset = h;
- mScale = (int) - (mYOffset * (2.0f / maxValue));
- mWidth = w;
- mLastX = mWidth;
- super.onSizeChanged(w, h, oldw, oldh);
- }**/
- @Override
- protected void onDraw(Canvas canvas) {
- //Bitmap mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
- //canvas.drawBitmap(mBitmap, 0, 0, null);
- /**
- // Initialization
- int yLabelHeight = 20;
- int yLabelSpacing = 20;
- int width = canvas.getWidth();
- int height = canvas.getHeight();
- int xMin, xMax, yMin, yMax;
- xMin = xMax = yMin = yMax = 0;
- boolean firstRun = true;
- for(Point p : points) {
- // Set xMax to the current x value
- if (p.x > xMax || firstRun) {
- xMax = (int) p.x;
- }
- // Set xMin to the current x value
- if (p.x < xMin || firstRun) {
- xMin = (int) p.x;
- }
- // Set yMax to the current y value
- if (p.y > yMax || firstRun) {
- yMax = (int) p.x;
- }
- // Set yMin to the current y value
- if (p.y < yMin || firstRun) {
- yMin = (int) p.x;
- }
- }
- int xRange = Math.abs(xMax - xMin);
- int yRange = Math.abs(yMax - yMin);
- int yLabelNumber = width / yLabelSpacing;
- int yLabelInterval = Math.round(yRange / yLabelNumber);
- int yLabelStart = yMin + yMin % yLabelInterval;
- int yLabelOffset = (width - (yLabelNumber - 1) * yLabelSpacing) / 2;
- FontMetrics metrics = mPaint.getFontMetrics();
- for (int i = 0; i < yLabelNumber; i++) {
- String label = Integer.toString(yLabelStart + i * yLabelInterval);
- int center = yLabelOffset + (i * yLabelSpacing);
- mPaint.setColor(Grey);
- canvas.drawText(label, center - (label.length() / 2), yLabelHeight / 2 + metrics.ascent / 2, mPaint);
- }**/
- // Y-axis values
- int startX = 0;
- int startY = 0;
- int stopX = 0;
- int stopY = canvas.getHeight();
- // Initial values for the y-axis labels
- int yMin, yMax;
- yMin = yMax = 0;
- // Compute the minimum and maximum y-axis label values
- for (Point p : points) {
- if (p.y > yMax) {
- yMax = (int) p.y;
- }
- if (p.y < yMin) {
- yMin = (int) p.y;
- }
- }
- // Paint properties for the y-axis labels
- Paint labelPaint = new Paint();
- labelPaint.setColor(Grey);
- labelPaint.setTextSize(10);
- int labelX = startX;
- int labelMinY = startY;
- int labelMaxY = stopY
- // Draw the y-axis labels
- // canvas.drawText(Integer.toString(yMin), labelX, labelMinY, labelPaint);
- // canvas.drawText(Integer.toString(yMax), labelX, labelMaxY, labelPaint);
- // Paint properties for the y-axis
- Paint p2 = new Paint();
- p2.setColor(Pink);
- p2.setStrokeWidth(5.0f);
- // Draw the y-axis
- canvas.drawLine(startX, startY, stopX, stopY, p2);
- // Paint properties for the graph
- Paint p3 = new Paint();
- p3.setColor(Pink);
- p3.setStrokeWidth(2.0f);
- //p3.setShadowLayer(10.0f, 3.0f, 3.0f, Color.argb(192, 140, 140, 200));
- // Loop through the arraylist of points and draw a line from (last x, last y) to (current x, current y) every time
- for (int i = 1; i < points.size(); i++) {
- int x1 = points.get(i - 1).x;
- int y1 = points.get(i - 1).y;
- int x2 = points.get(i).x;
- int y2 = points.get(i).y;
- canvas.drawLine(x1, y1, x2, y2, p3);
- }
- }
- }
Add Comment
Please, Sign In to add comment