AndroidSensorMessageView.java
Go to the documentation of this file.
00001 package org.ros.android.androidSensorMessage;
00002 
00003 import java.util.ArrayList;
00004 
00005 import android.content.Context;
00006 import android.graphics.Canvas;
00007 import android.graphics.Color;
00008 import android.graphics.Paint;
00009 import android.graphics.Typeface;
00010 import android.graphics.Paint.Style;
00011 import android.view.SurfaceHolder;
00012 import android.view.SurfaceView;
00013 
00014 
00015 public class AndroidSensorMessageView extends SurfaceView implements
00016                 SurfaceHolder.Callback, Runnable {
00017 
00018         private SurfaceHolder holder;
00019         private Thread thread;
00020         private Paint paint;
00021         private Canvas canvas;
00022         private int width;
00023         private int viewFlag[];
00024         private float originalY;
00025         private float scale;
00026 
00027         private ArrayList<Float> Xarray;
00028         private ArrayList<Float> Yarray;
00029         private ArrayList<Float> Zarray;
00030 
00031         public AndroidSensorMessageView(Context context, SurfaceView sv) {
00032                 super(context);
00033                 // initialize
00034                 Xarray = new ArrayList<Float>();
00035                 Yarray = new ArrayList<Float>();
00036                 Zarray = new ArrayList<Float>();
00037                 width = getWidth();
00038                 viewFlag = new int[4];
00039                 originalY = 150;
00040                 scale = 1;
00041                 for (int i = 0; i < 4; i++) {
00042                         viewFlag[i] = 1;
00043                 }
00044 
00045                 paint = new Paint();
00046                 paint.setStyle(Style.FILL);
00047                 paint.setTextSize(32);
00048                 paint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.BOLD_ITALIC));
00049 
00050                 holder = sv.getHolder();
00051                 holder.addCallback(this);
00052                 holder.setFixedSize(480, 262);
00053         }
00054 
00055         public void surfaceCreated(SurfaceHolder holder) {
00056                 thread = new Thread(this);
00057                 thread.start();
00058         }
00059 
00060         public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
00061                 width = w;
00062 
00063         }
00064 
00065         public void surfaceDestroyed(SurfaceHolder holder) {
00066                 thread = null;
00067         }
00068 
00069         public void run() {
00070                 while (true) {
00071                         try {
00072                                 int size = Xarray.size();
00073                                 canvas = holder.lockCanvas();
00074                                 canvas.drawColor(Color.WHITE);
00075                                 paint.setColor(Color.BLACK);
00076                                 if (viewFlag[0] == 1) {
00077                                         canvas.drawLine(0, originalY, width, originalY, paint);
00078                                 }
00079                                 paint.setColor(Color.RED);
00080                                 if (viewFlag[1] == 1) {
00081                                         for (int i = 0; i < size - 2; i++) {
00082                                                 canvas.drawLine(i, Xarray.get(i)*scale + originalY, i + 1,
00083                                                                 Xarray.get(i + 1)*scale + originalY, paint);
00084                                         }
00085                                 }
00086                                 if (viewFlag[2] == 1) {
00087                                         paint.setColor(Color.BLUE);
00088                                         for (int i = 0; i < size - 2; i++) {
00089                                                 canvas.drawLine(i, Yarray.get(i)*scale + originalY, i + 1,
00090                                                                 Yarray.get(i + 1)*scale + originalY, paint);
00091                                         }
00092                                 }
00093                                 if (viewFlag[3] == 1) {
00094                                         paint.setColor(Color.GREEN);
00095                                         for (int i = 0; i < size - 2; i++) {
00096                                                 canvas.drawLine(i, Zarray.get(i)*scale + originalY, i + 1,
00097                                                                 Zarray.get(i + 1)*scale + originalY, paint);
00098                                         }
00099                                 }
00100                                 holder.unlockCanvasAndPost(canvas);
00101 
00102                         } catch (Exception e) {
00103 
00104                         }
00105                 }
00106         }
00107 
00108         public void addValues(float[] values) {
00109                 Xarray.add(values[0]);
00110                 Yarray.add(values[1]);
00111                 Zarray.add(values[2]);
00112                 if (Xarray.size() > width+1) {
00113                         Xarray.remove(0);
00114                         Yarray.remove(0);
00115                         Zarray.remove(0);
00116                 }
00117         }
00118 
00119         public void viewChange(int flag) {
00120                 viewFlag[flag] *= -1;
00121         }
00122         
00123         public void updateOriginalY(float update){
00124                 originalY += update;
00125         }
00126         
00127         public void updateScale(float scale){
00128                 if(scale > 0){
00129                 this.scale += 0.05f;
00130                 }
00131                 else if(scale < 0){
00132                         this.scale -= 0.05f;
00133                 }
00134                 if(this.scale <= (float)0.01){
00135                         this.scale = (float)0.01;
00136                 }
00137         }
00138 }


jsk_android_apps
Author(s): Kazuto Murase
autogenerated on Thu Jun 6 2019 18:03:46