Snippets

Daniel Einars android-selection-help

Created by Daniel Einars
  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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package com.polaroidkidd.sleepme;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.concurrent.atomic.AtomicBoolean;

public class Picker extends View {
  AtomicBoolean bedMoving = new AtomicBoolean(false);
  AtomicBoolean alarmMoving = new AtomicBoolean(false);
  //<editor-fold desc="Outer Circle">
  //Colors
  private int ocCircleColor = 0;
  //Dimensions
  private float ocMin = 0;
  private float ocRadius = 0;
  private float ocPadding = 0;
  private float ocThickness = 0;
  private float ocDialX = 0;
  private float ocDialY = 0;
  private double ocAngle = 0;
  private float center;
  //</editor-fold>
  //<editor-fold desc="View Dimensions">
  private float width = 0;
  private float height = 0;
  //</editor-fold>
  //UI Required
  //<editor-fold desc="UI Required">
  private Paint paint;
  private Canvas canvas;
  private RectF rectF;
  //</editor-fold>
  //<editor-fold desc="iconAlarm">
  private Bitmap iconAlarm_bm;
  private float iconAlarmX, iconAlarmY;
  private float iconAlarmAngle;
  private int iconAlarmBackgroundColor = 0;
  private float iconAlarmBackgroundRadius = 0;
  private float offsetAlarmIcon;
  private float angleAlarmIcon;
  private float angleAlarmtime;
  private boolean alarmTimeIsMoving;
  //</editor-fold>
  //<editor-fold desc="bedTime">
  private Bitmap iconBedtime_bm;
  private float iconBedtimeX, iconBedtimeY;
  private int iconBedtimeBackgroundColor = 0;
  private float iconBedtimeBackgroundRadius = 0;
  private float offsetBedtimeIcon;
  private float angleBedtimeIcon;
  private float angleBedtime;
  private boolean bedtimeIsMoving;
  //</editor-fold>
  //<editor-fold desc="text">
  private float textWidth;
  private int bedtimeFontColor;
  private String bedtime;
  private String alarm;
  private int alarmFontColor;
  //</editor-fold>
  //<editor-fold desc="sleepTime">
  private Bitmap iconSleeptime_bm;
  private float iconSleeptimeBackgroundRadius;
  //</editor-fold>
  private boolean isTouching;
  private float posX = 0;
  private float posY = 0;
  boolean isMoving;
  
  /**
   * Simple constructor to use when creating a view from code.
   * @param context The Context the view is running in, through which it can access the current theme, resources, etc.
   */
  public Picker(Context context) {
    
    super(context);
    
  }
  
  public Picker(Context context, AttributeSet attrs) {
    super(context, attrs);
    angleAlarmIcon = (float) ((float) Math.PI * (0.75));
    angleBedtimeIcon = (float) ((float) Math.PI * 1);
    
    init(context, attrs);
  }
  
  public Picker(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    angleAlarmIcon = (float) ((float) Math.PI * (0.75));
    angleBedtimeIcon = (float) ((float) Math.PI * 1);
    
    init(context, attrs);
  }
  
  public Picker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    angleAlarmIcon = (float) ((float) Math.PI * (0.75));
    angleBedtimeIcon = (float) ((float) Math.PI * 1);
    
    init(context, attrs);
  }
  
  private void init(Context context, AttributeSet attrs) {
    initOuterCircle();
    angleAlarmIcon = (float) ((float) Math.PI * (1));
    angleBedtimeIcon = (float) ((float) Math.PI * 1.1);
    
    initIconAlarm();
    initIconBedtime();
    initIconSleeptime();
    loadAttributes(context, attrs);
    
  }
  
  private void initIconSleeptime() {
    iconSleeptime_bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_sleeptime);
    iconSleeptimeBackgroundRadius = 60;
  }
  
  private void initIconBedtime() {
    iconBedtime_bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_bedtime);
    iconBedtimeBackgroundRadius = 60;
  }
  
  private void initIconAlarm() {
    iconAlarm_bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_alarm);
    iconAlarmBackgroundRadius = 60;
  }
  
  private void initOuterCircle() {
    paint = new Paint();
    paint.setAntiAlias(true);
    canvas = new Canvas();
    rectF = new RectF();
    ocAngle = (-Math.PI / 2) + .001;
    calculatePointerPosition(ocAngle);
  }
  
  private void calculatePointerPosition(double angle) {
    ocDialX = (float) (ocRadius * Math.cos(angle));
    ocDialY = (float) (ocRadius * Math.sin(angle));
  }
  
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width = MeasureSpec.getSize(widthMeasureSpec);
    height = MeasureSpec.getSize(heightMeasureSpec);
    
    ocMin = Math.min(width, height);
    setMeasuredDimension((int) ocMin, (int) ocMin);
    offsetAlarmIcon = (float) (iconAlarm_bm.getWidth() / 2 - 1);
    offsetBedtimeIcon = (float) (iconBedtime_bm.getWidth() / 2 - 1);
    ocPadding = ocMin / 15;
    ocRadius = (ocMin / 2) - ocPadding;
    rectF.set(-ocRadius, -ocRadius, ocRadius, ocRadius);
    
  }
  
  private void loadAttributes(Context context, AttributeSet attrs) {
    if (attrs != null) {
      TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Picker, 0, 0);
      if (typedArray != null) {
        //Outer Circle attrs.xml
        ocCircleColor = typedArray.getColor(R.styleable.Picker_ocCircleColor, ocCircleColor);
        ocThickness = typedArray.getDimension(R.styleable.Picker_ocThickness, ocThickness);
        iconAlarmBackgroundColor =
            typedArray.getColor(R.styleable.Picker_iconAlarmBackgroundColor, iconAlarmBackgroundColor);
        iconBedtimeBackgroundColor =
            typedArray.getColor(R.styleable.Picker_iconBedtimeBackgroundColor, iconAlarmBackgroundColor);
        bedtimeFontColor = typedArray.getColor(R.styleable.Picker_bedTimeFontColor, bedtimeFontColor);
        alarmFontColor = typedArray.getColor(R.styleable.Picker_alarmTimeFontColor, alarmFontColor);
        
      }
      typedArray.recycle();
    }
    
  }
  
  /**
   * Implement this to do your drawing.
   * @param canvas the canvas on which the background will be drawn
   */
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    
    //<editor-fold desc="Outer Circle">
    drawOuterCircle(canvas);
    //</editor-fold>
    
    //<editor-fold desc="draw icons">
    paint.setColor(iconAlarmBackgroundColor);
    paint.setStyle(Paint.Style.FILL);
    drawAlarmIcon(canvas);
    drawBedtimeIcon(canvas);
    //</editor-fold>
    DrawAlarmLogic drawAlarmLogic = new DrawAlarmLogic(canvas).invoke();
    float bedtimeOffsetX = drawAlarmLogic.getBedtimeOffsetX();
    float bedtimeOffsetY = drawAlarmLogic.getBedtimeOffsetY();
    int hourAlarm = drawAlarmLogic.getHourAlarm();
    int minuteAlarm = drawAlarmLogic.getMinuteAlarm();
    angleAlarmtime = (float) drawAlarmLogic.getAngleAlarmTime();
    
    DrawBedtimeLogic drawBedtimeLogic = new DrawBedtimeLogic(canvas, bedtimeOffsetX, bedtimeOffsetY).invoke();
    int hourBedtime = drawBedtimeLogic.getHourBedtime();
    int minuteBedtime = drawBedtimeLogic.getMinuteBedtime();
    angleBedtime = (float) drawBedtimeLogic.getAngleBedTime();
    
    drawSleeptimeLogic(canvas, bedtimeOffsetX, hourAlarm, minuteAlarm, hourBedtime, minuteBedtime);
    
    //drawGrid(canvas, angle);
    
    invalidate();
    
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    
    posX = (int) event.getX();
    posY = (int) event.getY();
    double angleTouch = Math.toDegrees(Math.atan2(posY - center, posX - center)) + 90;
    if (angleTouch < -0.1) {
      angleTouch = 360 + angleTouch;
    }/*
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        angleAlarmIcon = (float) ((float) Math.PI * (1));
        angleBedtimeIcon = (float) ((float) Math.PI * 1.1);
        return true;
    }*/
    
    if ((Math.abs(angleTouch - angleBedtime) < 15 || Math.abs(angleTouch - angleBedtime) > 340)) {
      
      angleBedtimeIcon = (float) Math.atan2(posX - center, posY - center);
      
    } else if ((Math.abs(angleTouch - angleAlarmtime) < 15 || Math.abs(angleTouch - angleAlarmtime) > 340)) {
      
      angleAlarmIcon = (float) Math.atan2(posX - center, posY - center);
      
    }
    //invalidate();
    
    return true;
  }
  
  private void drawSleeptimeLogic(Canvas canvas, float bedtimeOffsetX, int hourAlarm, int minuteAlarm, int hourBedtime,
      int minuteBedtime) {
    int hourDifference = hourAlarm - hourBedtime;
    int minuteDifference = minuteBedtime - minuteAlarm;
    if (angleAlarmtime < 0) {
      angleAlarmtime = 360;
    }
    int totalMinutesDiff = (int) Math.abs(((angleBedtime * 2) - (angleAlarmtime * 2)));
    if (hourDifference < 0) {
      hourDifference = 24 + hourDifference;
    }
    if (hourDifference >= 12) {
      hourDifference = Math.abs(12 - hourDifference);
    }
    
    if (minuteDifference < 0) {
      minuteDifference = 60 + minuteDifference;
    }
    
    String sleepTime;
    
    String hourDifferenceStr = String.valueOf(hourDifference);
    String minuteDifferenceStr = String.valueOf(minuteDifference);
    if (hourDifference < 10) {
      hourDifferenceStr = "0" + hourDifferenceStr;
    }
    if (minuteDifference < 10) {
      minuteDifferenceStr = "0" + minuteDifferenceStr;
    }
    sleepTime = hourDifferenceStr + ":" + minuteDifferenceStr;
    
    canvas.drawText(String.valueOf(totalMinutesDiff), bedtimeOffsetX + 20, 650, paint);
    
    canvas.drawCircle(bedtimeOffsetX - 70, 620, iconSleeptimeBackgroundRadius, paint);
    canvas.drawBitmap(iconSleeptime_bm, bedtimeOffsetX - 110, 580, paint);
  }
  
  private void drawGrid(Canvas canvas, double angle) {
    paint.setTextSize(50);
    canvas.drawText("ANGLE: " + String.valueOf((int) angle), 200, 700, paint);
    paint.setStrokeWidth(5);
    paint.setColor(Color.BLUE);
    
    canvas.drawLine(getWidth() / 2, 0, getHeight() / 2, height, paint);
    canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, paint);
    paint.setColor(Color.RED);
    canvas.drawLine(center, center, iconBedtimeX + offsetBedtimeIcon, iconBedtimeY + offsetBedtimeIcon, paint);
  }
  
  private void drawBedtimeIcon(Canvas canvas) {
    
    //angleBedtimeIcon = (float) (angleBedtimeIcon - 0.01);
    iconBedtimeX = (float) ((center - offsetBedtimeIcon) + (ocRadius * Math.sin(angleBedtimeIcon)));
    iconBedtimeY = (float) ((center - offsetBedtimeIcon) + (ocRadius * Math.cos(angleBedtimeIcon)));
    canvas.drawCircle(iconBedtime_bm.getHeight() / 2 + iconBedtimeX, iconBedtime_bm.getWidth() / 2 + iconBedtimeY,
        iconBedtimeBackgroundRadius, paint);
    canvas.drawBitmap(iconBedtime_bm, iconBedtimeX, iconBedtimeY, null);
  }
  
  private void drawAlarmIcon(Canvas canvas) {
    
    //angleAlarmIcon = (float) (angleAlarmIcon - 0.01);
    iconAlarmX = (float) ((center - offsetAlarmIcon) + (ocRadius) * (Math.sin(angleAlarmIcon)));
    iconAlarmY = (float) ((center - offsetAlarmIcon) + (ocRadius) * (Math.cos(angleAlarmIcon)));
    
    canvas.drawCircle(iconAlarm_bm.getHeight() / 2 + iconAlarmX, iconAlarm_bm.getWidth() / 2 + iconAlarmY,
        iconAlarmBackgroundRadius, paint);
    canvas.drawBitmap(iconAlarm_bm, iconAlarmX, iconAlarmY, null);
    
  }
  
  private void drawOuterCircle(Canvas canvas) {
    
    paint.setColor(ocCircleColor);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(ocThickness);
    center = Math.min(width, height) / 2;
    canvas.drawCircle(center, center, ocRadius, paint);
  }
  
  private class DrawAlarmLogic {
    private Canvas canvas;
    private float bedtimeOffsetX;
    private float bedtimeOffsetY;
    private int hourAlarm;
    private int minuteAlarm;
    private double angleAlarmTime;
    
    public DrawAlarmLogic(Canvas canvas) {
      this.canvas = canvas;
    }
    
    public double getAngleAlarmTime() {
      return angleAlarmTime;
    }
    
    public float getBedtimeOffsetX() {
      return bedtimeOffsetX;
    }
    
    public float getBedtimeOffsetY() {
      return bedtimeOffsetY;
    }
    
    public int getHourAlarm() {
      return hourAlarm;
    }
    
    public int getMinuteAlarm() {
      return minuteAlarm;
    }
    
    public DrawAlarmLogic invoke() {
      //<editor-fold desc="Alarm Text">
      alarm = "08:45";
      paint.setTextSize(100);
      paint.setColor(alarmFontColor);
      bedtimeOffsetX = (getWidth() / 2) - (textWidth / 2);
      bedtimeOffsetY = (getHeight() / 3);
      canvas.drawCircle(bedtimeOffsetX - 70, bedtimeOffsetY + 120, iconAlarmBackgroundRadius, paint);
      canvas.drawBitmap(iconAlarm_bm, bedtimeOffsetX - 70 - offsetAlarmIcon, bedtimeOffsetY + 75, paint);
      textWidth = paint.measureText(alarm);
      angleAlarmTime = Math.toDegrees(Math.atan2((iconAlarmY - getWidth() / 2) + offsetAlarmIcon,
          (iconAlarmX - getHeight() / 2) + offsetAlarmIcon)) + 90;
      if (angleAlarmTime < -0.1) {
        angleAlarmTime = 360 + angleAlarmTime;
      }
      hourAlarm = (int) angleAlarmTime / 30;
      String alarmHourStr = String.valueOf(hourAlarm);
      if (hourAlarm < 10) {
        alarmHourStr = "0" + alarmHourStr;
      }
      minuteAlarm = (int) ((angleAlarmTime * 2) % 60);
      minuteAlarm = 5 * (Math.round(minuteAlarm / 5));
      String minuteAlarmStr = String.valueOf(minuteAlarm);
      if (minuteAlarm < 10) {
        minuteAlarmStr = "0" + minuteAlarmStr;
      }
      alarm = alarmHourStr + ":" + minuteAlarmStr;
      canvas.drawText(alarm, bedtimeOffsetX + 20, bedtimeOffsetY + 160, paint);
      //</editor-fold>
      return this;
    }
  }
  
  private class DrawBedtimeLogic {
    private Canvas canvas;
    private float bedtimeOffsetX;
    private float bedtimeOffsetY;
    private int hourBedtime;
    private int minuteBedtime;
    private double angleBedTime;
    
    public DrawBedtimeLogic(Canvas canvas, float bedtimeOffsetX, float bedtimeOffsetY) {
      this.canvas = canvas;
      this.bedtimeOffsetX = bedtimeOffsetX;
      this.bedtimeOffsetY = bedtimeOffsetY;
    }
    
    public double getAngleBedTime() {
      return angleBedTime;
    }
    
    public int getHourBedtime() {
      return hourBedtime;
    }
    
    public int getMinuteBedtime() {
      return minuteBedtime;
    }
    
    public DrawBedtimeLogic invoke() {
      //<editor-fold desc="bedtime Text">
      bedtime = "23:45";
      paint.setTextSize(100);
      paint.setColor(bedtimeFontColor);
      textWidth = paint.measureText(bedtime);
      canvas.drawCircle(bedtimeOffsetX - 70, bedtimeOffsetY - 30, iconBedtimeBackgroundRadius, paint);
      canvas.drawBitmap(iconBedtime_bm, bedtimeOffsetX - 70 - offsetBedtimeIcon, bedtimeOffsetY - 75, paint);
      
      angleBedTime = Math.toDegrees(Math.atan2((iconBedtimeY - getWidth() / 2) + offsetBedtimeIcon,
          (iconBedtimeX - getHeight() / 2) + offsetBedtimeIcon)) + 90;
      if (angleBedTime < -0.1) {
        angleBedTime = 360 + angleBedTime;
      }
      hourBedtime = (int) (angleBedTime / 30);
      if (hourBedtime > 12) {
        hourBedtime = 12 - hourBedtime;
      }
      /*if(hourBedtime<=12){
        hourBedtime = 12 + hourBedtime;
      }*/
      String bedtimeHourStr = String.valueOf(hourBedtime);
      if (hourBedtime < 10) {
        bedtimeHourStr = "0" + bedtimeHourStr;
      }
      minuteBedtime = (int) ((angleBedTime * 2) % 60);
      minuteBedtime = 5 * (Math.round(minuteBedtime / 5));
      String bedtimeMinuteStr = String.valueOf(minuteBedtime);
      
      if (minuteBedtime < 10) {
        bedtimeMinuteStr = "0" + bedtimeMinuteStr;
      }
      bedtime = bedtimeHourStr + ":" + bedtimeMinuteStr;
      canvas.drawText(bedtime, bedtimeOffsetX + 20, bedtimeOffsetY, paint);
      //</editor-fold>
      return this;
    }
    
  }
  
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.