Monday 29 June 2015

Android and Java

My main track of learning has been in Java, and as such I have of course found an interest in learning to work with the Android platform. My own smartphone is an Android, we own a tablet which is Android and I see myself continuing to make purchases of this type in the future. I got a couple of Android game programming books for Christmas and one of them is too simple (copy what we wrote and don't ask questions), the other assumes a lot more knowledge and has taken me a while to catch up to.

Before learning Java more comprehensibly through the Head First Java book, I studied via a web series from developer John Purcell. The course is available on Udemy and I highly recommend it for anyone looking to get a grip on Java while seeing someone type and run code into the Eclipse IDE.

There is also an Android course available by John Purcell which I hope to take in once I have a handle of the basics. My experience so far has been that studying from one source and then getting that second look from another really helps to solidify my understanding. Seeing one example of a new concept compared to another is often the deciding factor on whether I "get it" or need to go to bed because I am too tired.

My current understanding of Android is of the Manifest and how to add activities, the use of a few basic View's, some simple touch handling and basic drawing to a Canvas. I am working on an activity that takes in multiple touches (pointers) and assigns them to a new or incomplete pair. For each pointer, if it belongs to a pair but has no other pointer, it draws a circle, and for complete pairs (both pointers still touching the screen) it draws smaller circles and a line to connect them. My code for this has gone through a few iterations as I figure out the pointers for each MotionEvent that is fired and handle it, but the drawing aspect has me stuck right now. My program may just be over complicated (using a class instance for each PointerPair) and I may have to go back to simple arrays to keep track of each pointer and its relationship. for now though my code looks like this.

There are a few Log.d debugging lines in there for when I test on my phone. It took a while for me to realize that my code was not functioning and cloned my initial PointerPair instance to all slots of the array because I didn't exit the for loop when I should have. I understand my code isn't pretty and probably makes some ridiculous optimization choices but its main objective is to help me learn so I'm not too precious yet :P

(I used hilite.me for code formatting)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package ***********;

import java.util.ArrayList;
import java.util.Random;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class DrawLinesTest extends Activity implements OnTouchListener{
 private final int MAX_PAIRS = 5;
 PointerPair[] pairs = new PointerPair[MAX_PAIRS];
 Random rng;
 PointerPair openPair = null;
 
 ArrayList<PointerPair> drawables = new ArrayList<PointerPair>();
 
 
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  RenderView v = new RenderView(this);
  v.setOnTouchListener(this);
  setContentView(v);
  
  rng = new Random();
 }
 
 public void onResume(){
  super.onResume();
  for(int i = 0; i < pairs.length; i++){
   pairs[i] = null;
  }
 }
 
 @TargetApi(Build.VERSION_CODES.FROYO)
 public boolean onTouch(View v, MotionEvent e){
  //Log.d("PointerPair", "Screen Touched");
  int action = e.getActionMasked();
  int pointerIndex = e.getActionIndex();
  int pointerId = e.getPointerId(pointerIndex);
  
  switch(action){
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_POINTER_DOWN:
   if(openPair != null){
    openPair.setPointer2Id(pointerId);
    openPair.setCoords(1, e.getX(), e.getY());
    Log.d("PointerPair", "Pairs: An pointer partner has been found! ");
    openPair = null;
   } else {
    boolean pairCreated = false;
    for(int i = 0; i < pairs.length; i++){
     if(!pairCreated){
      if(pairs[i] == null){
       Log.d("PointerPair", "Pairs: Creating new pair at index " + i);
       pairs[i] = new PointerPair(pointerId, e.getX(), e.getY());
       drawables.add(pairs[i]);
       Log.d("PointerPair", "Pairs: An pointer needs a partner! ");
       openPair = pairs[i];
       pairCreated = true;
      }
     }
    }
   }
   break;
  case MotionEvent.ACTION_MOVE:
   for(int i = 0; i < pairs.length; i++){
    if(pairs[i] != null){
     if(pointerId == pairs[i].pointer1Id){
      pairs[i].setCoords(0, e.getX(), e.getY());
     } else if(pointerId == pairs[i].pointer2Id){
      Log.d("PointerPair", "Painting - updating second pointer coords");
      pairs[i].setCoords(1, e.getX(), e.getY());
     }
    } 
   }
   break;
  case MotionEvent.ACTION_POINTER_UP:
   for(int i = 0; i < pairs.length; i++){
    if(pairs[i] != null){
     if(pointerId == pairs[i].pointer1Id){
      //Log.d("PointerPair", "Pairs: Lifted Pointer matches pointer 1 in pair ID:" + i);
      pairs[i].pointer1Id = -1;
      
     } else if(pointerId == pairs[i].pointer2Id){
      //Log.d("PointerPair", "Pairs: Lifted Pointer matches pointer 2 in pair ID:" + i);
      pairs[i].pointer2Id = -1;
     }
    } 
   }
   break;
  case MotionEvent.ACTION_UP:
   for(int i = 0; i < pairs.length; i++){
    pairs[i] = null;
    drawables.clear();
   }
   openPair = null;
   break;
  }
  
  cleanPairs();
  v.invalidate();
  return true;
 }
 
 private void cleanPairs(){
  for(int j = 0; j < pairs.length; j++){
   if(pairs[j] != null){
    pairs[j].update();
   }
  }
  
  // Clean out dead pairs
  for(int i= 0; i < pairs.length; i++){
   if(pairs[i] != null){
    if(pairs[i].pairDead()){
     drawables.remove(pairs[i]);
     //Log.d("PointerPair", "Pairs: Nulling a pair at position" + i);
     pairs[i] = null;
    }
   }
  }
 }
 
 class PointerPair{
  private int pointer1Id, pointer2Id;
  private boolean needsPair, isPointer1Dead, isPointer2Dead, isPairDead;
  private int p1x, p1y, p2x, p2y;
  Paint paint;
  
  
  public PointerPair(int pointer1Id){
   this.pointer1Id = pointer1Id;
   pointer2Id = -1;
   needsPair = true;
   paint = new Paint();
   paint.setARGB(255, rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
  }
  
  public PointerPair(int pointer1Id, float x, float y){
   this(pointer1Id);
   setCoords(0, x, y);
  }
  
  public void setCoords(int pointerIndex, float x, float y){
   if(pointerIndex == 0){
    p1x = (int) x;
    p1y = (int) y;
   } else if(pointerIndex == 1){
    p2x = (int) x;
    p2y = (int) y;
   }
  }
  
  public void update(){
   if(pointer1Id == -1){
    isPointer1Dead = true;
   }
   if (pointer2Id == -1){
    isPointer2Dead = true;
   }
   
   if(isPointer1Dead && isPointer2Dead){
    isPairDead = true;
   } else if (isPointer1Dead && needsPair){
    isPairDead = true;
   }
  }
  
  public boolean needsPair(){
   return needsPair;
  }
  
  public void setPointer2Id(int pointer2Id){
   this.pointer2Id = pointer2Id;
   needsPair = false;
  }
  
  public boolean pairDead(){
   return isPairDead;
  }
  
  public Paint getPaint(){
   return paint;
  }
 }
 
 class RenderView extends View{
  Paint paint;
  
  public void onDraw(Canvas canvas){
   
   clear(canvas);
   for(int i = 0; i < pairs.length; i++){
    for(PointerPair p: drawables){
     Paint paint1 = p.getPaint();
     
     if(p.pointer1Id != -1 && p.pointer2Id != -1){
      Log.d("PointerPairPaint", "Painting a pair, 2 circles baby!s");
      canvas.drawCircle(p.p1x, p.p1y, getWidth() / 15, paint1);
      canvas.drawCircle(p.p2x, p.p2y, getWidth() / 15, paint1);
      return;
     } 
     
     if(p.needsPair() || p.isPointer2Dead){
      Log.d("PointerPairPaint", "Painting lonely first pointer, " + p.p1x + ", " + p.p1y);
      canvas.drawCircle(p.p1x, p.p1y, getWidth() / 10, paint1);
      return;
     } else if(p.isPointer1Dead){
      Log.d("PointerPairPaint", "Painting lonely second pointer, " + p.p2x + ", " + p.p2y);
      canvas.drawCircle(p.p2x, p.p2y, getWidth() / 10, paint1);
      return;
     }
    }
   }
   //invalidate();
  }
  
  public RenderView(Context context){
   super(context);
   paint = new Paint();
  }
  
  private void clear(Canvas c){
   paint.setColor(Color.GRAY);
   c.drawRect(0, 0, getWidth() - 1, getHeight() - 1, paint);
  }
  
 }
 

}

No comments:

Post a Comment