hjw
2023-04-20 a75addbc22f25b5fb219d3979f7bcd9cbf4942c3
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
package com.jwipc.nodka_alarmpower;
 
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
 
 
 
public class LoopView extends View {
 
    public enum ACTION {
        CLICK, FLING, DAGGLE
    }
    Context context;
 
    Handler handler;
    private GestureDetector gestureDetector;
    OnItemSelectedListener onItemSelectedListener;
 
    ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor();
    private ScheduledFuture<?> mFuture;
 
    Paint paintOuterText;
    Paint paintCenterText;
    Paint paintIndicator;
 
    List<String> items;
 
    int textSize;
    int maxTextWidth = 50;
    int maxTextHeight = 50;
 
    int colorGray;
    int colorBlack;
    int colorLightGray;
 
    float lineSpacingMultiplier;
    boolean isLoop;
 
    int firstLineY;
    int secondLineY;
 
    int totalScrollY;
    int initPosition;
    private int selectedItem;
    int preCurrentIndex;
    int change;
 
    int itemsVisible;
 
    int measuredHeight;
    int measuredWidth;
    int paddingLeft = 10;
    int paddingRight = 0;
 
    int halfCircumference;
    int radius;
 
    private int mOffset = 0;
    private float previousY;
    long startTime = 0;
    
    final int WHAT_INVALIDATE_LOOP_VIEW = 1000;
    final int WHAT_SMOOTH_SCROLL = 2000;
    final int WHAT_ITEM_SELECTED = 3000;
    
    
    
    
 
    public LoopView(Context context) {
        super(context);
        initLoopView(context);
    }
 
    public LoopView(Context context, AttributeSet attributeset) {
        super(context, attributeset);
        initLoopView(context);
    }
 
    public LoopView(Context context, AttributeSet attributeset, int defStyleAttr) {
        super(context, attributeset, defStyleAttr);
        initLoopView(context);
    }
 
    private void initLoopView(Context context) {
        this.context = context;
        
        handler();
        gestureDetector = new GestureDetector(context, new LoopViewGestureListener(this));
        gestureDetector.setIsLongpressEnabled(false);
        
        lineSpacingMultiplier = 1.6F;
        isLoop = true;
        itemsVisible = 5;
        textSize = 0;
        colorGray = 0xffafafaf;
        colorBlack = 0xff313131;
        colorLightGray = 0xff000000;
 
        totalScrollY = 0;
        initPosition = 0;
 
        initPaints();
 
        setTextSize(16F);
        
    }
 
    private void initPaints() {
        paintOuterText = new Paint();
        paintOuterText.setColor(colorGray);
        paintOuterText.setAntiAlias(true);
        paintOuterText.setTypeface(Typeface.MONOSPACE);
        paintOuterText.setTextSize(textSize);
 
        paintCenterText = new Paint();
        paintCenterText.setColor(colorBlack);
        paintCenterText.setAntiAlias(true);
        paintCenterText.setTextScaleX(1.05F);
        paintCenterText.setTypeface(Typeface.MONOSPACE);
        paintCenterText.setTextSize(textSize);
 
        paintIndicator = new Paint();
        paintIndicator.setColor(colorLightGray);
        paintIndicator.setAntiAlias(true);
 
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            setLayerType(LAYER_TYPE_SOFTWARE, null);
        }
    }
 
    private void remeasure() {
        if (items == null) {
            return;
        }
        
        measureTextWidthHeight();
 
        halfCircumference = (int) (maxTextHeight * lineSpacingMultiplier * (itemsVisible - 1));
        measuredHeight = (int) ((halfCircumference * 2) / Math.PI);
        radius = (int) (halfCircumference / Math.PI);
        int extraRightWidth = (int) (maxTextWidth * 0.05) + 1;
        if (paddingRight<=extraRightWidth) {
            paddingRight = extraRightWidth;
        }
        measuredWidth = maxTextWidth + paddingLeft + paddingRight;
        firstLineY = (int) ((measuredHeight - maxTextHeight * lineSpacingMultiplier) / 2.0F);
        secondLineY = (int) ((measuredHeight + maxTextHeight * lineSpacingMultiplier) / 2.0F);
 
        preCurrentIndex = initPosition;
    }
 
    private void measureTextWidthHeight() {
        Rect rect = new Rect();
        for (int i = 0; i < items.size(); i++) {
            String s1 = items.get(i);
            int textWidth = maxTextWidth;
            if (textWidth > maxTextWidth) {
                maxTextWidth = textWidth;
            }
            int textHeight = maxTextHeight;
            if (textHeight > maxTextHeight) {
                maxTextHeight = textHeight;
            }
        }
 
    }
 
    void smoothScroll(ACTION action) {
        cancelFuture();
        if (action==ACTION.FLING||action==ACTION.DAGGLE) {
            float itemHeight = lineSpacingMultiplier * maxTextHeight;
            mOffset = (int) ((totalScrollY%itemHeight + itemHeight) % itemHeight);
            if ((float) mOffset > itemHeight / 2.0F) {
                mOffset = (int) (itemHeight - (float) mOffset);
            } else {
                mOffset = -mOffset;
            }
        }
        mFuture = mExecutor.scheduleWithFixedDelay(new SmoothScrollTimerTask(this, mOffset), 0, 10, TimeUnit.MILLISECONDS);
    }
 
    protected final void scrollBy(float velocityY) {
        cancelFuture();
        // 修改这个值可以改变滑行速度
        int velocityFling = 50;
        mFuture = mExecutor.scheduleWithFixedDelay(new InertiaTimerTask(this, velocityY), 0, velocityFling, TimeUnit.MILLISECONDS);
    }
 
    public void cancelFuture() {
        if (mFuture!=null&&!mFuture.isCancelled()) {
            mFuture.cancel(true);
            mFuture = null;
        }
    }
 
    public final void setNotLoop() {
        isLoop = false;
    }
 
    public final void setTextSize(float size) {
        if (size > 0.0F) {
            textSize = (int) (context.getResources().getDisplayMetrics().density * size);
            paintOuterText.setTextSize(textSize);
            paintCenterText.setTextSize(textSize);
        }
    }
 
    public final void setInitPosition(int initPosition) {
        this.initPosition = initPosition;
    }
 
    public final void setListener(OnItemSelectedListener OnItemSelectedListener) {
        onItemSelectedListener = OnItemSelectedListener;
    }
 
    public final void setItems(List<String> items) {
        this.items = items;
        remeasure();
        invalidate();
    }
 
    @Override
    public int getPaddingLeft() {
        return paddingLeft;
    }
 
    @Override
    public int getPaddingRight() {
        return paddingRight;
    }
 
    public void setViewPadding(int left, int top, int right, int bottom) {
        paddingLeft = left;
        paddingRight = right;
    }
 
    public final int getSelectedItem() {
        return selectedItem;
    }
 
    protected final void onItemSelected() {
        if (onItemSelectedListener != null) {
            postDelayed(new OnItemSelectedRunnable(this), 200L);
        }
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        if (items == null) {
            return;
        }
        
        change = (int) (totalScrollY / (lineSpacingMultiplier * maxTextHeight));
        
        
        preCurrentIndex = initPosition + change % items.size();        
        if (!isLoop) {
            if (preCurrentIndex < 0) {
                preCurrentIndex = 0;
            }
            if (preCurrentIndex > items.size() - 1) {
                preCurrentIndex = items.size() - 1;
            }
        } else {
            if (preCurrentIndex < 0) {
                preCurrentIndex = items.size() + preCurrentIndex;
            }
            if (preCurrentIndex > items.size() - 1) {
                preCurrentIndex = preCurrentIndex - items.size();
            }
        }
        
        
        
        String as[] = new String[itemsVisible];
        int k1 = 0;
        while (k1 < itemsVisible) {
            int l1 = preCurrentIndex - (itemsVisible / 2 - k1);
            if (isLoop) {
                if (l1 < 0) {
                    l1 = l1 + items.size();
                }
                if (l1 > items.size() - 1) {
                    l1 = l1 - items.size();
                }
                as[k1] = items.get(l1);
            }  else if (l1 < 0) {
                as[k1] = "";
            } else if (l1 > items.size() - 1) {
                as[k1] = "";
            } else {
                as[k1] = items.get(l1);
            }
            k1++;
        }
        
        
        canvas.drawLine(0.0F, firstLineY, measuredWidth, firstLineY, paintIndicator);
        canvas.drawLine(0.0F, secondLineY, measuredWidth, secondLineY, paintIndicator);
        
        int m1 = paddingLeft;
        int j1 = 0;
        int j2 = (int) (totalScrollY % (lineSpacingMultiplier * maxTextHeight));
        while (j1 < itemsVisible) {
            canvas.save();
            float itemHeight = maxTextHeight * lineSpacingMultiplier;
            double radian = ((itemHeight * j1 - j2) * Math.PI) / halfCircumference;
            
            float angle = (float) (90D - (radian / Math.PI) * 180D);
            if (angle >= 90F || angle <= -90F) {
                canvas.restore();
            } else {
                int translateY = (int) (radius - Math.cos(radian) * radius - (Math.sin(radian) * maxTextHeight) / 2D);
                
                canvas.translate(0.0F, translateY);
                canvas.scale(1.0F, (float) Math.sin(radian));
                if (translateY <= firstLineY && maxTextHeight + translateY >= firstLineY) {
                    canvas.save();
                    canvas.clipRect(0, 0, measuredWidth, firstLineY - translateY);
                    canvas.drawText(as[j1], m1, maxTextHeight, paintOuterText);
                    canvas.restore();
                    canvas.save();
                    canvas.clipRect(0, firstLineY - translateY, measuredWidth, (int) (itemHeight));
                    canvas.drawText(as[j1], m1, maxTextHeight, paintCenterText);
                    canvas.restore();
                } else if (translateY <= secondLineY && maxTextHeight + translateY >= secondLineY) {
                    canvas.save();
                    canvas.clipRect(0, 0, measuredWidth, secondLineY - translateY);
                    canvas.drawText(as[j1], m1, maxTextHeight, paintCenterText);
                    canvas.restore();
                    canvas.save();
                    canvas.clipRect(0, secondLineY - translateY, measuredWidth, (int) (itemHeight));
                    canvas.drawText(as[j1], m1, maxTextHeight, paintOuterText);
                    canvas.restore();
                } else if (translateY >= firstLineY && maxTextHeight + translateY <= secondLineY) {
                    canvas.clipRect(0, 0, measuredWidth, (int) (itemHeight));
                    canvas.drawText(as[j1], m1, maxTextHeight-10, paintCenterText);
                    selectedItem = items.indexOf(as[j1]);
                } else {
                    canvas.clipRect(0, 0, measuredWidth, (int) (itemHeight));
                    canvas.drawText(as[j1], m1, maxTextHeight, paintOuterText);
                }
                canvas.restore();
            }
            j1++;
        }
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        remeasure();
        setMeasuredDimension(measuredWidth, measuredHeight);
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean eventConsumed = gestureDetector.onTouchEvent(event);
        float itemHeight = lineSpacingMultiplier * maxTextHeight;
 
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startTime = System.currentTimeMillis();
                cancelFuture();
                previousY = event.getRawY();
                break;
 
            case MotionEvent.ACTION_MOVE:
                float dy = previousY - event.getRawY();
                previousY = event.getRawY();
 
                totalScrollY = (int) (totalScrollY + dy);
 
                if (!isLoop) {
                    float top = -initPosition * itemHeight;
                    float bottom = (items.size() - 1 - initPosition) * itemHeight;
 
                    if (totalScrollY < top) {
                        totalScrollY = (int) top;
                    } else if (totalScrollY > bottom) {
                        totalScrollY = (int) bottom;
                    }
                }
                break;
 
            case MotionEvent.ACTION_UP:
            default:
                if (!eventConsumed) {
                    float y = event.getY();
                    double l = Math.acos((radius - y) / radius) * radius;
                    int circlePosition = (int) ((l + itemHeight / 2) / itemHeight);
 
                    float extraOffset = (totalScrollY % itemHeight + itemHeight) % itemHeight;
                    mOffset = (int) ((circlePosition - itemsVisible / 2) * itemHeight - extraOffset);
 
                    if ((System.currentTimeMillis() - startTime) > 120) {
                        smoothScroll(ACTION.DAGGLE);
                    } else {
                        smoothScroll(ACTION.CLICK);
                    }
                }
                break;
        }
 
        invalidate();
        return true;
    }
 
    public void handler()
    {
        handler = new Handler()
        {
            @Override
            public final void handleMessage(Message msg) {
                switch (msg.what) {
                    case WHAT_INVALIDATE_LOOP_VIEW:
                        invalidate();
                        break;
 
                    case WHAT_SMOOTH_SCROLL:
                        smoothScroll(LoopView.ACTION.FLING);
                        break;
 
                    case WHAT_ITEM_SELECTED:
                        onItemSelected();
                        break;
                }
            }
        };
    }
 
    class LoopViewGestureListener extends android.view.GestureDetector.SimpleOnGestureListener {
 
        final LoopView loopView;
 
        LoopViewGestureListener(LoopView loopview) {
            loopView = loopview;
        }
 
        @Override
        public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            loopView.scrollBy(velocityY);
            return true;
        }
    }
 
    class InertiaTimerTask extends TimerTask {
 
        float a;
        final float velocityY;
        final LoopView loopView;
 
        InertiaTimerTask(LoopView loopview, float velocityY) {
            super();
            loopView = loopview;
            this.velocityY = velocityY;
            a = Integer.MAX_VALUE;
        }
 
        @Override
        public final void run() {
            if (a == Integer.MAX_VALUE) {
                if (Math.abs(velocityY) > 2000F) {
                    if (velocityY > 0.0F) {
                        a = 2000F;
                    } else {
                        a = -2000F;
                    }
                } else {
                    a = velocityY;
                }
            }
            if (Math.abs(a) >= 0.0F && Math.abs(a) <= 20F) {
                loopView.cancelFuture();
                loopView.handler.sendEmptyMessage(loopView.WHAT_SMOOTH_SCROLL);
                return;
            }
            int i = (int) ((a * 10F) / 1000F);
            LoopView loopview = loopView;
            loopview.totalScrollY = loopview.totalScrollY - i;
            if (!loopView.isLoop) {
                float itemHeight = loopView.lineSpacingMultiplier * loopView.maxTextHeight;
                if (loopView.totalScrollY <= (int) ((float) (-loopView.initPosition) * itemHeight)) {
                    a = 40F;
                    loopView.totalScrollY = (int) ((float) (-loopView.initPosition) * itemHeight);
                } else if (loopView.totalScrollY >= (int) ((float) (loopView.items.size() - 1 - loopView.initPosition) * itemHeight)) {
                    loopView.totalScrollY = (int) ((float) (loopView.items.size() - 1 - loopView.initPosition) * itemHeight);
                    a = -40F;
                }
            }
            if (a < 0.0F) {
                a = a + 20F;
            } else {
                a = a - 20F;
            }
            loopView.handler.sendEmptyMessage(loopView.WHAT_INVALIDATE_LOOP_VIEW);
        }
    }
 
    public interface OnItemSelectedListener {
        void onItemSelected(int index);
    }
 
    class OnItemSelectedRunnable implements Runnable {
        final LoopView loopView;
 
        OnItemSelectedRunnable(LoopView loopview) {
            loopView = loopview;
        }
 
        @Override
        public final void run() {
            loopView.onItemSelectedListener.onItemSelected(loopView.getSelectedItem());
        }
    }
 
    class SmoothScrollTimerTask extends TimerTask {
 
        int realTotalOffset;
        int realOffset;
        int offset;
        final LoopView loopView;
 
        SmoothScrollTimerTask(LoopView loopview, int offset) {
            this.loopView = loopview;
            this.offset = offset;
            realTotalOffset = Integer.MAX_VALUE;
            realOffset = 0;
        }
 
        @Override
        public final void run() {
            if (realTotalOffset == Integer.MAX_VALUE) {
                realTotalOffset = offset;
            }
            realOffset = (int) ((float) realTotalOffset * 0.1F);
 
            if (realOffset == 0) {
                if (realTotalOffset < 0) {
                    realOffset = -1;
                } else {
                    realOffset = 1;
                }
            }
            if (Math.abs(realTotalOffset) <= 0) {
                loopView.cancelFuture();
                loopView.handler.sendEmptyMessage(loopView.WHAT_ITEM_SELECTED);
            } else {
                loopView.totalScrollY = loopView.totalScrollY + realOffset;
                loopView.handler.sendEmptyMessage(loopView.WHAT_INVALIDATE_LOOP_VIEW);
                realTotalOffset = realTotalOffset - realOffset;
            }
        }
    }
 
 
 
 
 
 
 
 
}