Project

General

Profile

« Previous | Next » 

Revision 1f19a4d0

Added by Leszek Koltunski almost 5 years ago

Improve the Dynamic app.

View differences:

src/main/java/org/distorted/examples/dynamic/DynamicSpeedSurfaceView.java
34 34
class DynamicSpeedSurfaceView extends SurfaceView implements SurfaceHolder.Callback
35 35
{
36 36
    private static final int FRAME_INTERVAL = 70;
37
    private static final int NUM_POINTS     = DynamicSurfaceView.NUM_POINTS;
37
    private static final int NUM_POINTS     = 400;
38 38

  
39 39
    private static boolean refreshScreen = true;
40 40
    private GraphicsThread mThread;
......
42 42
    private int mMode, mNumRedPoints, mPointSize;
43 43
    private float mFontHeight;
44 44
    private Paint mPaint;
45
    private float[] mPoints = new float[2*NUM_POINTS];
46
    private int mPosition;
45
    private float[] mPoints = new float[NUM_POINTS];
47 46
    private float mMultiplier;
48 47

  
49 48
/////////////////////////////////////////////////////////////////////////////////////////
......
115 114
      super(context,attrs);
116 115

  
117 116
      mNumRedPoints = 0;
118
      mPosition     = 0;
119 117

  
120 118
      mPaint = new Paint();
121 119
      mPaint.setAntiAlias(true);
......
158 156
        {
159 157
        mPaint.setColor(0xff007d00);
160 158
        c.drawRect(0, 0, mWidth, mHeight, mPaint);
161
        mPaint.setColor(0xff000000);
162
        c.drawText( "Speed", mWidth/2, mMargin, mPaint);
163 159
        drawAxis(c);
164 160
        drawSpeedGraph(c);
165 161
        drawRedPoints(c);
162
        mPaint.setColor(0xff000000);
163
        c.drawText( "Speed", mWidth*0.5f, mMargin, mPaint);
166 164
        }
167 165
      }
168 166

  
......
214 212
    private void drawSpeedGraph(Canvas c)
215 213
      {
216 214
      mPaint.setColor(0xffffffff);
217
      float y, highiest=0.0f;
215
      float x,y, highiest=0.0f;
218 216

  
219 217
      for(int i=0; i<NUM_POINTS; i++)
220 218
        {
221
        y = mPoints[2*i+1];
222
        c.drawCircle( mMargin+mPoints[2*i]*(mWidth-2*mMargin), mHeight-mMargin-mMultiplier*y , mPointSize*0.3f, mPaint );
219
        x = ((float)(i*(mWidth-2*mMargin)))/NUM_POINTS;
220
        y = mPoints[i];
221
        c.drawCircle( mMargin+x, mHeight-mMargin-mMultiplier*y , mPointSize*0.3f, mPaint );
223 222

  
224 223
        if( y>highiest ) highiest = y;
225 224
        }
226 225

  
227
      mMultiplier = (mHeight-2*mMargin)/highiest;
226
      mMultiplier = (mHeight-3*mMargin)/highiest;
228 227
      }
229 228

  
230 229
///////////////////////////////////////////////////////////////////////////////////////////////////
......
239 238

  
240 239
    void clearPoints()
241 240
      {
242
      for(int i=0; i<2*NUM_POINTS; i++)
241
      for(int i=0; i<NUM_POINTS; i++)
243 242
         {
244
         mPoints[i] = -10.0f;
243
         mPoints[i] = -100.0f;
245 244
         }
246 245
      refreshScreen = true;
247 246
      }
......
250 249

  
251 250
    void addPoint(float x, float y)
252 251
      {
253
      mPoints[2*mPosition  ] =x;
254
      mPoints[2*mPosition+1] =y;
255

  
256
      if ( ++mPosition >= NUM_POINTS ) mPosition=0;
257

  
258
      refreshScreen = true;
252
      if( x>=0.0f && x<=1.0f )
253
        {
254
        int pos = (int)((NUM_POINTS-1)*x);
255
        mPoints[pos] =y;
256
        refreshScreen = true;
257
        }
258
      else
259
        {
260
        android.util.Log.e("speed", "ERROR adding new Speed Point, x="+x);
261
        }
259 262
      }
260 263

  
261 264
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff