SeatTable.java
36.3 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
package com.cnlive.goldenline.ui.widget;
import android.animation.Animator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
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.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.model.PortraitBean;
import com.cnlive.goldenline.util.ToastUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by baoyunlong on 16/6/16.
*/
public class SeatTable extends View {
private final boolean DBG = false;
private static final String TAG = "SeatTable";
Paint paint = new Paint();
Paint lineNumberPaint;
float lineNumberTxtHeight;
private int translateY = 5;
/**
* 设置行号 默认显示 1,2,3....数字
*
* @param lineNumbers
*/
public void setLineNumbers(ArrayList<String> lineNumbers) {
this.lineNumbers = lineNumbers;
invalidate();
}
/**
* 用来保存所有行号
*/
ArrayList<String> lineNumbers = new ArrayList<>();
Paint.FontMetrics lineNumberPaintFontMetrics;
Matrix matrix = new Matrix();
/**
* 座位水平间距
*/
int spacing;
/**
* 座位垂直间距
*/
int verSpacing;
/**
* 行号宽度
*/
int numberWidth;
/**
* 行数
*/
int row;
/**
* 列数
*/
int column;
/**
* 可选时座位的图片
*/
Bitmap seatBitmap;
/**
* 选中时座位的图片
*/
Bitmap checkedSeatBitmap;
/**
* 座位已经售出时的图片
*/
Bitmap seatSoldBitmap;
int lastX;
int lastY;
/**
* 整个座位图的宽度
*/
int seatBitmapWidth;
/**
* 整个座位图的高度
*/
int seatBitmapHeight;
/**
* 标识是否需要绘制座位图
*/
boolean isNeedDrawSeatBitmap = true;
/**
* 荧幕高度
*/
float screenHeight;
/**
* 荧幕默认宽度与座位图的比例
*/
float screenWidthScale = 0.5f;
/**
* 荧幕最小宽度
*/
int defaultScreenWidth;
/**
* 标识是否正在缩放
*/
boolean isScaling;
float scaleX, scaleY;
/**
* 是否是第一次缩放
*/
boolean firstScale = true;
/**
* 最多可以选择的座位数量
*/
int maxSelected = Integer.MAX_VALUE;
private SeatChecker seatChecker;
public boolean isDrawScreenHeader = false;
public boolean isDrawScreenName = false;
private boolean isSoldSeatClickAble = false;
/**
* 荧幕名称
*/
private String screenName = "";
Paint headPaint;
Bitmap headBitmap;
/**
* 是否第一次执行onDraw
*/
boolean isFirstDraw = true;
int txt_color;
int seatCheckedResID;
int seatSoldResID;
int seatAvailableResID;
boolean isOnClick;
/**
* 座位已售
*/
private static final int SEAT_TYPE_SOLD = 1;
/**
* 座位已经选中
*/
private static final int SEAT_TYPE_SELECTED = 2;
/**
* 座位可选
*/
private static final int SEAT_TYPE_AVAILABLE = 3;
/**
* 座位不可用
*/
private static final int SEAT_TYPE_NOT_AVAILABLE = 4;
private int downX, downY;
private boolean pointer;
/**
* 顶部高度,可选,已选,已售区域的高度
*/
float headHeight;
Paint pathPaint;
RectF rectF;
/**
* 头部下面横线的高度
*/
int borderHeight = 1;
// Paint redBorderPaint;
/**
* 默认的座位图宽度,如果使用的自己的座位图片比这个尺寸大或者小,会缩放到这个大小
*/
private float defaultImgW = 66;
/**
* 默认的座位图高度
*/
private float defaultImgH = 63;
/**
* 座位图片的宽度
*/
private int seatWidth;
/**
* 座位图片的高度
*/
private int seatHeight;
public SeatTable(Context context) {
super(context);
}
public SeatTable(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SeatTableView);
txt_color = typedArray.getColor(R.styleable.SeatTableView_txt_color, Color.WHITE);
seatCheckedResID = typedArray.getResourceId(R.styleable.SeatTableView_seat_checked, R.mipmap.seat_selected);
seatSoldResID = typedArray.getResourceId(R.styleable.SeatTableView_overview_sold, R.mipmap.seat_selected);
seatAvailableResID = typedArray.getResourceId(R.styleable.SeatTableView_seat_available, R.mipmap.seat_nor);
typedArray.recycle();
}
public SeatTable(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
float xScale1 = 1;
float yScale1 = 1;
private void init() {
spacing = (int) dip2Px(8);
verSpacing = (int) dip2Px(10);
defaultScreenWidth = (int) dip2Px(80);
seatBitmap = BitmapFactory.decodeResource(getResources(), seatAvailableResID);
checkedSeatBitmap = BitmapFactory.decodeResource(getResources(), seatCheckedResID);
seatSoldBitmap = BitmapFactory.decodeResource(getResources(), seatSoldResID);
float scaleX = defaultImgW / seatBitmap.getWidth();
float scaleY = defaultImgH / seatBitmap.getHeight();
xScale1 = scaleX;
yScale1 = scaleY;
seatHeight = (int) (seatBitmap.getHeight() * yScale1);
seatWidth = (int) (seatBitmap.getWidth() * xScale1);
seatBitmapWidth = (int) (column * seatBitmap.getWidth() * xScale1 + (column - 1) * spacing);
seatBitmapHeight = (int) (row * seatBitmap.getHeight() * yScale1 + (row - 1) * verSpacing);
Log.e(TAG, "init: seatHeight = " + seatHeight + " seatWidth = " + seatWidth + " seatBitmapHeight = " + seatBitmapHeight + " seatBitmapWidth = " + seatBitmapWidth);
paint.setColor(Color.RED);
numberWidth = (int) dip2Px(20);
screenHeight = dip2Px(20);
headHeight = dip2Px(30);
headPaint = new Paint();
headPaint.setStyle(Paint.Style.FILL);
headPaint.setTextSize(24);
headPaint.setColor(Color.WHITE);
headPaint.setAntiAlias(true);
pathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
pathPaint.setStyle(Paint.Style.FILL);
pathPaint.setColor(Color.parseColor("#e2e2e2"));
rectF = new RectF();
lineNumberPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
lineNumberPaint.setColor(bacColor);
lineNumberPaint.setTextSize(getResources().getDisplayMetrics().density * 13);
lineNumberTxtHeight = lineNumberPaint.measureText("4");
lineNumberPaintFontMetrics = lineNumberPaint.getFontMetrics();
lineNumberPaint.setTextAlign(Paint.Align.CENTER);
translateY = (int) (lineNumberTxtHeight / 2);
if (lineNumbers == null) {
lineNumbers = new ArrayList<>();
} else if (lineNumbers.size() <= 0) {
for (int i = 0; i < row; i++) {
lineNumbers.add((i + 1) + "");
}
}
matrix.postTranslate(numberWidth + spacing, headHeight + screenHeight + borderHeight + verSpacing);
}
/**
* 获取分辨率:宽
*
* @param context
* @return
*/
public static int getScreenWidth(@Nullable Context context) {
int screenWidth = 0;
try {
if (context == null) return 0;
DisplayMetrics dm = new DisplayMetrics();
dm = context.getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
} catch (Exception ex) {
}
return screenWidth;
}
/**
* 获取分辨率:高
*
* @param context
* @return
*/
public static int getScreenHeight(@Nullable Context context) {
int screenWidth = 0;
try {
if (context == null) return 0;
DisplayMetrics dm = new DisplayMetrics();
dm = context.getResources().getDisplayMetrics();
screenWidth = dm.heightPixels;
} catch (Exception ex) {
}
return screenWidth;
}
@Override
protected void onDraw(Canvas canvas) {
long startTime = System.currentTimeMillis();
if (row <= 0 || column == 0) {
return;
}
// int screenWidth = getScreenWidth(getContext());
// int screenHeight = getScreenHeight(getContext());
// int height = getHeight();
// int width = getWidth();
//
// Log.e(TAG, "onDraw: getHeight() = " + height + " getWidth() = " + width + " screenHeight = " + screenHeight + " screenWidth = " + screenWidth + " defaultImgH = " + defaultImgH + " defaultImgW = " + defaultImgW);
//
// defaultImgW = defaultImgW * width / screenWidth;
// defaultImgH = defaultImgH * height / screenHeight;
//
// Log.e(TAG, "onDraw: defaultImgH = " + defaultImgH + " defaultImgW = " + defaultImgW);
//
// float scaleX = defaultImgW / seatBitmap.getWidth();
// float scaleY = defaultImgH / seatBitmap.getHeight();
// xScale1 = scaleX;
// yScale1 = scaleY;
//
// seatHeight = (int) (seatBitmap.getHeight() * yScale1);
// seatWidth = (int) (seatBitmap.getWidth() * xScale1);
//
// seatBitmapWidth = (int) (column * seatBitmap.getWidth() * xScale1 + (column - 1) * spacing);
// seatBitmapHeight = (int) (row * seatBitmap.getHeight() * yScale1 + (row - 1) * verSpacing);
// Log.e(TAG, "onDraw: xScale1 = " + xScale1 + " yScale1 = " + yScale1 + " seatHeight = " + seatHeight + " seatWidth = " + seatWidth + " seatBitmapWidth = " + seatBitmapWidth + " seatBitmapHeight = " + seatBitmapHeight);
drawSeat(canvas);
drawNumber(canvas);
if (isDrawScreenHeader) {
if (headBitmap == null) {
headBitmap = drawHeadInfo();
}
canvas.drawBitmap(headBitmap, 0, 0, null);
}
if (isDrawScreenName)
drawScreen(canvas);
if (DBG) {
long drawTime = System.currentTimeMillis() - startTime;
Log.d("drawTime", "totalDrawTime:" + drawTime);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int y = (int) event.getY();
int x = (int) event.getX();
super.onTouchEvent(event);
scaleGestureDetector.onTouchEvent(event);
gestureDetector.onTouchEvent(event);
int pointerCount = event.getPointerCount();
if (pointerCount > 1) {
pointer = true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
pointer = false;
downX = x;
downY = y;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
if (!isScaling && !isOnClick) {
int downDX = Math.abs(x - downX);
int downDY = Math.abs(y - downY);
if ((downDX > 10 || downDY > 10) && !pointer) {
int dx = x - lastX;
int dy = y - lastY;
matrix.postTranslate(dx, dy);
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
autoScale();
int downDX = Math.abs(x - downX);
int downDY = Math.abs(y - downY);
if ((downDX > 10 || downDY > 10) && !pointer) {
autoScroll();
}
break;
}
isOnClick = false;
lastY = y;
lastX = x;
return true;
}
Bitmap drawHeadInfo() {
String txt = "已售";
float txtY = getBaseLine(headPaint, 0, headHeight);
int txtWidth = (int) headPaint.measureText(txt);
float spacing = dip2Px(10);
float spacing1 = dip2Px(5);
float y = (headHeight - seatBitmap.getHeight()) / 2;
float width = seatBitmap.getWidth() + spacing1 + txtWidth + spacing + seatSoldBitmap.getWidth() + txtWidth + spacing1 + spacing + checkedSeatBitmap.getHeight() + spacing1 + txtWidth;
Bitmap bitmap = Bitmap.createBitmap(getWidth(), (int) headHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//绘制背景
canvas.drawRect(0, 0, getWidth(), headHeight, headPaint);
headPaint.setColor(Color.BLACK);
float startX = (getWidth() - width) / 2;
tempMatrix.setScale(xScale1, yScale1);
tempMatrix.postTranslate(startX, (headHeight - seatHeight) / 2);
canvas.drawBitmap(seatBitmap, tempMatrix, headPaint);
canvas.drawText("可选", startX + seatWidth + spacing1, txtY, headPaint);
float soldSeatBitmapY = startX + seatBitmap.getWidth() + spacing1 + txtWidth + spacing;
tempMatrix.setScale(xScale1, yScale1);
tempMatrix.postTranslate(soldSeatBitmapY, (headHeight - seatHeight) / 2);
canvas.drawBitmap(seatSoldBitmap, tempMatrix, headPaint);
canvas.drawText("已售", soldSeatBitmapY + seatWidth + spacing1, txtY, headPaint);
float checkedSeatBitmapX = soldSeatBitmapY + seatSoldBitmap.getWidth() + spacing1 + txtWidth + spacing;
tempMatrix.setScale(xScale1, yScale1);
tempMatrix.postTranslate(checkedSeatBitmapX, y);
canvas.drawBitmap(checkedSeatBitmap, tempMatrix, headPaint);
canvas.drawText("已选", checkedSeatBitmapX + spacing1 + seatWidth, txtY, headPaint);
//绘制分割线
headPaint.setStrokeWidth(1);
headPaint.setColor(Color.GRAY);
canvas.drawLine(0, headHeight, getWidth(), headHeight, headPaint);
return bitmap;
}
/**
* 绘制中间屏幕
*/
void drawScreen(Canvas canvas) {
pathPaint.setStyle(Paint.Style.FILL);
pathPaint.setColor(Color.parseColor("#e2e2e2"));
float startY = headHeight + borderHeight;
float centerX = seatBitmapWidth * getMatrixScaleX() / 2 + getTranslateX();
float screenWidth = seatBitmapWidth * screenWidthScale * getMatrixScaleX();
if (screenWidth < defaultScreenWidth) {
screenWidth = defaultScreenWidth;
}
Path path = new Path();
path.moveTo(centerX, startY);
path.lineTo(centerX - screenWidth / 2, startY);
path.lineTo(centerX - screenWidth / 2 + 20, screenHeight * getMatrixScaleY() + startY);
path.lineTo(centerX + screenWidth / 2 - 20, screenHeight * getMatrixScaleY() + startY);
path.lineTo(centerX + screenWidth / 2, startY);
canvas.drawPath(path, pathPaint);
pathPaint.setColor(Color.BLACK);
pathPaint.setTextSize(20 * getMatrixScaleX());
canvas.drawText(screenName, centerX - pathPaint.measureText(screenName) / 2, getBaseLine(pathPaint, startY, startY + screenHeight * getMatrixScaleY()), pathPaint);
}
Matrix tempMatrix = new Matrix();
void drawSeat(Canvas canvas) {
zoom = getMatrixScaleX();
long startTime = System.currentTimeMillis();
float translateX = getTranslateX() + 5;
// float translateY = getTranslateY();
// float translateY = 0;
float scaleX = zoom;
float scaleY = zoom;
for (int i = 0; i < row; i++) {
float top = i * seatBitmap.getHeight() * yScale1 * scaleY + i * verSpacing * scaleY + translateY;
float bottom = top + seatBitmap.getHeight() * yScale1 * scaleY;
// Log.e(TAG, "drawSeat: i = " + i + " bottom = " + bottom + " top = " + top);
if (bottom < 0 || top > getHeight()) {
// Log.e(TAG, "drawSeat: i = " + i + " row for circle continue");
continue;
}
for (int j = 0; j < column; j++) {
float left = j * seatBitmap.getWidth() * xScale1 * scaleX + j * spacing * scaleX + translateX;
float right = (left + seatBitmap.getWidth() * xScale1 * scaleY);
// Log.e(TAG, "drawSeat: j = " + j + " right = " + right + " left = " + left + " getWidth() = " + getWidth());
if (right < 0 || left > getWidth()) {
// Log.e(TAG, "drawSeat: i = " + i + " j = " + j + "column for circle continue");
continue;
}
int seatType = getSeatType(i, j);
// Log.e(TAG, "drawSeat: i = " + i + " j = " + j + " seatType = " + seatType);
tempMatrix.setTranslate(left, top);
tempMatrix.postScale(xScale1, yScale1, left, top);
tempMatrix.postScale(scaleX, scaleY, left, top);
switch (seatType) {
case SEAT_TYPE_AVAILABLE:
canvas.drawBitmap(seatBitmap, tempMatrix, paint);
break;
case SEAT_TYPE_NOT_AVAILABLE:
break;
case SEAT_TYPE_SELECTED:
canvas.drawBitmap(checkedSeatBitmap, tempMatrix, paint);
break;
case SEAT_TYPE_SOLD:
// final Canvas c = canvas;
// try {
// Bitmap myBitmap = Glide.with(getContext()).load(PORTRAIT1).asBitmap().centerCrop().into((int) defaultImgW, (int) defaultImgH).get();
// Log.e(TAG, "drawSeat: myBitmap = " + myBitmap);
// savebitmap(myBitmap);
// canvas.drawBitmap(myBitmap, tempMatrix, paint);
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }
// try {
// Glide.with(getContext()).load(PORTRAIT1).asBitmap().into(new SimpleTarget<Bitmap>() {
// @Override
// public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
// Log.e(TAG, "onResourceReady: resource = " + resource + " " + resource.getWidth() + " " + resource.getHeight());
// savebitmap(resource);
// c.drawBitmap(resource, tempMatrix, paint);
// }
// });
// } catch (Exception e) {
// e.printStackTrace();
// }
if (headList != null && headList.size() > 0)
canvas.drawBitmap(headList.get(0).getBitmap(), tempMatrix, paint);
break;
}
}
}
if (DBG) {
long drawTime = System.currentTimeMillis() - startTime;
Log.d("drawTime", "seatDrawTime:" + drawTime);
}
}
private List<PortraitBean> headList = new ArrayList<>();
public static void savebitmap(Bitmap bitmap) {
File appDir = new File(Environment.getExternalStorageDirectory(), "com.ksy.recordlib.demo.demo");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static final String PORTRAIT1 = "http://i1.mopimg.cn/img/tt/2016-12/564/20161223141738534.jpeg790x600.jpeg";
private int getSeatType(int row, int column) {
if (isHave(getID(row, column)) >= 0) {
return SEAT_TYPE_SELECTED;
}
if (seatChecker != null) {
if (!seatChecker.isValidSeat(row, column)) {
return SEAT_TYPE_NOT_AVAILABLE;
} else if (seatChecker.isSold(row, column)) {
return SEAT_TYPE_SOLD;
}
}
return SEAT_TYPE_AVAILABLE;
}
private int getID(int row, int column) {
return row * this.column + (column + 1);
}
int bacColor = Color.parseColor("#7e000000");
/**
* 绘制行号
*/
void drawNumber(Canvas canvas) {
long startTime = System.currentTimeMillis();
lineNumberPaint.setColor(bacColor);
// int translateY = (int) getTranslateY();
// int translateY = 0;
float scaleY = getMatrixScaleY();
rectF.top = translateY - lineNumberTxtHeight / 2;
if (rectF.top < 0) rectF.top = 0;
rectF.bottom = translateY + (seatBitmapHeight * scaleY) + lineNumberTxtHeight / 2;
rectF.left = 0;
rectF.right = numberWidth;
canvas.drawRoundRect(rectF, numberWidth / 2, numberWidth / 2, lineNumberPaint);
lineNumberPaint.setColor(Color.WHITE);
for (int i = 0; i < row; i++) {
float top = (i * seatHeight + i * verSpacing) * scaleY + translateY;
float bottom = (i * seatHeight + i * verSpacing + seatHeight) * scaleY + translateY;
float baseline = (bottom + top - lineNumberPaintFontMetrics.bottom - lineNumberPaintFontMetrics.top) / 2;
if (getSeatType(i, 0) != SEAT_TYPE_NOT_AVAILABLE)
canvas.drawText(lineNumbers.get(i), numberWidth / 2, baseline, lineNumberPaint);
}
if (DBG) {
long drawTime = System.currentTimeMillis() - startTime;
Log.d("drawTime", "drawNumberTime:" + drawTime);
}
}
/**
* 自动回弹
* 整个大小不超过控件大小的时候:
* 往左边滑动,自动回弹到行号右边
* 往右边滑动,自动回弹到右边
* 往上,下滑动,自动回弹到顶部
* <p>
* 整个大小超过控件大小的时候:
* 往左侧滑动,回弹到最右边,往右侧滑回弹到最左边
* 往上滑动,回弹到底部,往下滑动回弹到顶部
*/
private void autoScroll() {
float currentSeatBitmapWidth = seatBitmapWidth * getMatrixScaleX();
float currentSeatBitmapHeight = seatBitmapHeight * getMatrixScaleY();
float moveYLength = 0;
float moveXLength = 0;
//处理左右滑动的情况
if (currentSeatBitmapWidth < getWidth()) {
if (getTranslateX() < 0 || getMatrixScaleX() < numberWidth + spacing) {
//计算要移动的距离
if (getTranslateX() < 0) {
moveXLength = (-getTranslateX()) + numberWidth + spacing;
} else {
moveXLength = numberWidth + spacing - getTranslateX();
}
}
} else {
if (getTranslateX() < 0 && getTranslateX() + currentSeatBitmapWidth > getWidth()) {
} else {
//往左侧滑动
if (getTranslateX() + currentSeatBitmapWidth < getWidth()) {
moveXLength = getWidth() - (getTranslateX() + currentSeatBitmapWidth);
} else {
//右侧滑动
moveXLength = -getTranslateX() + numberWidth + spacing;
}
}
}
float startYPosition = screenHeight * getMatrixScaleY() + verSpacing * getMatrixScaleY() + headHeight + borderHeight;
//处理上下滑动
if (currentSeatBitmapHeight + headHeight < getHeight()) {
if (getTranslateY() < startYPosition) {
moveYLength = startYPosition - getTranslateY();
} else {
moveYLength = -(getTranslateY() - (startYPosition));
}
} else {
if (getTranslateY() < 0 && getTranslateY() + currentSeatBitmapHeight > getHeight()) {
} else {
//往上滑动
if (getTranslateY() + currentSeatBitmapHeight < getHeight()) {
moveYLength = getHeight() - (getTranslateY() + currentSeatBitmapHeight);
} else {
moveYLength = -(getTranslateY() - (startYPosition));
}
}
}
Point start = new Point();
start.x = (int) getTranslateX();
start.y = (int) getTranslateY();
Point end = new Point();
end.x = (int) (start.x + moveXLength);
end.y = (int) (start.y + moveYLength);
moveAnimate(start, end);
}
private void autoScale() {
if (getMatrixScaleX() > 2.2) {
zoomAnimate(getMatrixScaleX(), 2.0f);
} else if (getMatrixScaleX() < 0.98) {
zoomAnimate(getMatrixScaleX(), 1.0f);
}
}
Handler handler = new Handler();
ArrayList<Integer> selects = new ArrayList<>();
public ArrayList<String> getSelectedSeat() {
ArrayList<String> results = new ArrayList<>();
for (int i = 0; i < this.row; i++) {
for (int j = 0; j < this.column; j++) {
if (isHave(getID(i, j)) >= 0) {
results.add(i + "," + j);
}
}
}
return results;
}
private int isHave(Integer seat) {
return Collections.binarySearch(selects, seat);
}
private void remove(int index) {
selects.remove(index);
}
float[] m = new float[9];
private float getTranslateX() {
matrix.getValues(m);
return m[2];
}
private float getTranslateY() {
matrix.getValues(m);
return m[5];
}
private float getMatrixScaleY() {
matrix.getValues(m);
return m[4];
}
private float getMatrixScaleX() {
matrix.getValues(m);
return m[Matrix.MSCALE_X];
}
private float dip2Px(float value) {
return getResources().getDisplayMetrics().density * value;
}
private float getBaseLine(Paint p, float top, float bottom) {
Paint.FontMetrics fontMetrics = p.getFontMetrics();
int baseline = (int) ((bottom + top - fontMetrics.bottom - fontMetrics.top) / 2);
return baseline;
}
private void moveAnimate(Point start, Point end) {
ValueAnimator valueAnimator = ValueAnimator.ofObject(new MoveEvaluator(), start, end);
valueAnimator.setInterpolator(new DecelerateInterpolator());
MoveAnimation moveAnimation = new MoveAnimation();
valueAnimator.addUpdateListener(moveAnimation);
valueAnimator.setDuration(400);
valueAnimator.start();
}
private void zoomAnimate(float cur, float tar) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(cur, tar);
valueAnimator.setInterpolator(new DecelerateInterpolator());
ZoomAnimation zoomAnim = new ZoomAnimation();
valueAnimator.addUpdateListener(zoomAnim);
valueAnimator.addListener(zoomAnim);
valueAnimator.setDuration(400);
valueAnimator.start();
}
private float zoom;
private void zoom(float zoom) {
float z = zoom / getMatrixScaleX();
matrix.postScale(z, z, scaleX, scaleY);
invalidate();
}
private void move(Point p) {
float x = p.x - getTranslateX();
float y = p.y - getTranslateY();
matrix.postTranslate(x, y);
invalidate();
}
public void setHeadList(List<PortraitBean> headList) {
this.headList = headList;
invalidate();
}
class MoveAnimation implements ValueAnimator.AnimatorUpdateListener {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Point p = (Point) animation.getAnimatedValue();
move(p);
}
}
class MoveEvaluator implements TypeEvaluator {
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
Point startPoint = (Point) startValue;
Point endPoint = (Point) endValue;
int x = (int) (startPoint.x + fraction * (endPoint.x - startPoint.x));
int y = (int) (startPoint.y + fraction * (endPoint.y - startPoint.y));
return new Point(x, y);
}
}
class ZoomAnimation implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
zoom = (Float) animation.getAnimatedValue();
zoom(zoom);
if (DBG) {
Log.d("zoomTest", "zoom:" + zoom);
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationStart(Animator animation) {
}
}
public void setData(int row, int column) {
this.row = row;
this.column = column;
init();
invalidate();
}
ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
isScaling = true;
float scaleFactor = detector.getScaleFactor();
if (getMatrixScaleY() * scaleFactor > 3) {
scaleFactor = 3 / getMatrixScaleY();
}
if (firstScale) {
scaleX = detector.getCurrentSpanX();
scaleY = detector.getCurrentSpanY();
firstScale = false;
}
if (getMatrixScaleY() * scaleFactor < 0.5) {
scaleFactor = 0.5f / getMatrixScaleY();
}
matrix.postScale(scaleFactor, scaleFactor, scaleX, scaleY);
invalidate();
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
isScaling = false;
firstScale = true;
}
});
GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
isOnClick = true;
int x = (int) e.getX();
int y = (int) e.getY();
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
int tempX = (int) ((j * seatWidth + j * spacing) * getMatrixScaleX() + getTranslateX());
int maxTemX = (int) (tempX + seatWidth * getMatrixScaleX());
int tempY = (int) ((i * seatHeight + i * verSpacing) * getMatrixScaleY() + getTranslateY());
int maxTempY = (int) (tempY + seatHeight * getMatrixScaleY());
if (seatChecker != null && seatChecker.isValidSeat(i, j)) {
if (isSoldSeatClickAble && seatChecker.isSold(i, j)) {
Log.e(TAG, "onSingleTapConfirmed: ");
} else {
if (x >= tempX && x <= maxTemX && y >= tempY && y <= maxTempY) {
int id = getID(i, j);
int index = isHave(id);
if (index >= 0) {
remove(index);
if (seatChecker != null) {
seatChecker.unCheck(i, j);
}
} else {
if (selects.size() >= maxSelected) {
ToastUtil.showToast("最多只能选择" + maxSelected + "个");
return super.onSingleTapConfirmed(e);
} else {
addChooseSeat(i, j);
if (seatChecker != null) {
seatChecker.checked(i, j);
}
}
}
isNeedDrawSeatBitmap = true;
float currentScaleY = getMatrixScaleY();
if (currentScaleY < 1.7) {
scaleX = x;
scaleY = y;
zoomAnimate(currentScaleY, 1.9f);
}
invalidate();
break;
}
}
}
}
}
return super.onSingleTapConfirmed(e);
}
});
private void addChooseSeat(int row, int column) {
int id = getID(row, column);
for (int i = 0; i < selects.size(); i++) {
int item = selects.get(i);
if (id < item) {
selects.add(i, id);
return;
}
}
selects.add(id);
}
public interface SeatChecker {
/**
* 是否可用座位
*
* @param row
* @param column
* @return
*/
boolean isValidSeat(int row, int column);
/**
* 是否已售
*
* @param row
* @param column
* @return
*/
boolean isSold(int row, int column);
void checked(int row, int column);
void unCheck(int row, int column);
void onSoldSeatChecked(int row, int column);
}
public void setScreenName(String screenName) {
this.screenName = screenName;
}
public void setMaxSelected(int maxSelected) {
this.maxSelected = maxSelected;
}
public void setDrawScreenHeader(boolean drawScreenHeader) {
isDrawScreenHeader = drawScreenHeader;
invalidate();
}
public void setDrawScreenName(boolean drawScreenName) {
isDrawScreenName = drawScreenName;
invalidate();
}
public void setSeatChecker(SeatChecker seatChecker) {
this.seatChecker = seatChecker;
invalidate();
}
private int getRowNumber(int row) {
int result = row;
if (seatChecker == null) {
return -1;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (seatChecker.isValidSeat(i, j)) {
break;
}
if (j == column - 1) {
if (i == row) {
return -1;
}
result--;
}
}
}
return result;
}
private int getColumnNumber(int row, int column) {
int result = column;
if (seatChecker == null) {
return -1;
}
for (int i = row; i <= row; i++) {
for (int j = 0; j < column; j++) {
if (!seatChecker.isValidSeat(i, j)) {
if (j == column) {
return -1;
}
result--;
}
}
}
return result;
}
}