| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- package eVVM.apk.helper.basepicker;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.graphics.Typeface;
- import android.os.Handler;
- import android.text.TextPaint;
- import android.text.TextUtils;
- import android.util.AttributeSet;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.view.View;
- import java.util.List;
- import java.util.Timer;
- /**
- * Created by Android Studio.
- * User: zbb
- * Date: 2019/6/4
- * Describe: LoopMsgView
- */
- public class LoopMsgView extends View {
- Timer mTimer;
- int totalScrollY;
- Handler handler;
- LoopListener loopListener;
- private GestureDetector gestureDetector;
- private int mSelectItem;
- private GestureDetector.SimpleOnGestureListener simpleOnGestureListener;
- Context context;
- Paint paintA;
- Paint paintB;
- Paint paintC;
- List arrayList;
- int textSize;
- int g;
- int h;
- int colorGray;
- int colorBlack;
- int colorGrayLight;
- float l;
- boolean isLoop;
- int n;
- int o;
- int mCurrentItem;
- int positon;
- int r;
- int s;
- int t;
- int u;
- int v;
- int w;
- float x;
- float y;
- float z;
- public LoopMsgView(Context context) {
- super(context);
- initLoopView(context);
- }
- public LoopMsgView(Context context, AttributeSet attributeset) {
- super(context, attributeset);
- initLoopView(context);
- }
- public LoopMsgView(Context context, AttributeSet attributeset, int i1) {
- super(context, attributeset, i1);
- initLoopView(context);
- }
- private void initLoopView(Context context) {
- textSize = 0;
- colorGray = 0xffa7a7a7; //0xffafafaf;
- colorBlack = 0xff572dc9;// 0xff313131;
- colorGrayLight = 0xffdcdcdc; //0xffc5c5c5;
- l = 2.0F;
- isLoop = true;
- positon = -1;
- r = 9;
- x = 0.0F;
- y = 0.0F;
- z = 0.0F;
- totalScrollY = 0;
- simpleOnGestureListener = new LoopViewGestureListener(this);
- handler = new MessageHandler(this);
- this.context = context;
- setTextSize(16F);
- }
- static int getSelectItem(LoopMsgView loopview) {
- return loopview.getCurrentItem();
- }
- static void b(LoopMsgView loopview) {
- loopview.f();
- }
- private void d() {
- if (arrayList == null) {
- return;
- }
- paintA = new Paint();
- paintA.setColor(colorGray);
- paintA.setAntiAlias(true);
- paintA.setTypeface(Typeface.MONOSPACE);
- paintA.setTextSize(textSize);
- paintB = new Paint();
- paintB.setColor(colorBlack);
- paintB.setAntiAlias(true);
- paintB.setTextScaleX(1.05F);
- paintB.setTypeface(Typeface.MONOSPACE);
- paintB.setTextSize(textSize);
- paintC = new Paint();
- paintC.setColor(colorGrayLight);
- paintC.setAntiAlias(true);
- paintC.setTypeface(Typeface.MONOSPACE);
- paintC.setTextSize(textSize);
- //paintA.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
- if (android.os.Build.VERSION.SDK_INT >= 11) {
- setLayerType(1, null);
- }
- gestureDetector = new GestureDetector(context, simpleOnGestureListener);
- gestureDetector.setIsLongpressEnabled(false);
- e();
- t = (int) ((float) h * l * (float) (r - 1));
- s = (int) ((double) (t * 2) / Math.PI);
- u = (int) ((double) t / Math.PI);
- v = g + textSize;
- n = (int) (((float) s - l * (float) h) / 2.0F);
- o = (int) (((float) s + l * (float) h) / 2.0F);
- if (positon == -1) {
- if (isLoop) {
- positon = (arrayList.size() + 1) / 2;
- } else {
- positon = 0;
- }
- }
- mCurrentItem = positon;
- }
- private void e() {
- Rect rect = new Rect();
- for (int i1 = 0; i1 < arrayList.size(); i1++) {
- // String s1 = (String) arrayList.get(i1);
- // wangpeng:调整可触摸区域的宽度为4个字符
- paintB.getTextBounds("000000000", 0, "000000000".length(), rect);
- int j1 = rect.width();
- // wangpeng:调整区域增大2倍,提升体验。
- j1 = (int) (j1 * 3.0f);
- if (j1 > g) {
- g = j1;
- }
- paintB.getTextBounds("\u661F\u671F", 0, 2, rect);
- j1 = rect.height();
- if (j1 > h) {
- h = j1;
- }
- }
- }
- private void f() {
- int i1 = (int) ((float) totalScrollY % (l * (float) h));
- Timer timer = new Timer();
- mTimer = timer;
- timer.schedule(new MTimer(this, i1, timer), 0L, 10L);
- }
- public final void setNotLoop() {
- isLoop = false;
- }
- /**
- * 设置是否循环滚动
- *
- * @param cyclic
- */
- public final void setCyclic(boolean cyclic) {
- isLoop = cyclic;
- }
- public final void setTextSize(float size) {
- if (size > 0.0F) {
- textSize = (int) (context.getResources().getDisplayMetrics().density * size);
- }
- }
- public final void setCurrentItem(int position) {
- this.positon = position;
- totalScrollY = 0;
- f();
- invalidate();
- }
- public final void setListener(LoopListener LoopListener) {
- loopListener = LoopListener;
- }
- public final void setArrayList(List arraylist) {
- arrayList = arraylist;
- d();
- invalidate();
- }
- public final int getCurrentItem() {
- if (mCurrentItem <= 0) {
- return 0;
- }
- return mCurrentItem;
- }
- public final String getCurrentItemValue(){
- return String.valueOf(arrayList.get(getCurrentItem())).trim();
- }
- protected final void b(float f1) {
- Timer timer = new Timer();
- mTimer = timer;
- timer.schedule(new LoopTimerTask(this, f1, timer), 0L, 20L);
- }
- protected final void b(int i1) {
- Timer timer = new Timer();
- mTimer = timer;
- timer.schedule(new MyTimerTask(this, i1, timer), 0L, 20L);
- }
- protected final void c() {
- if (loopListener != null) {
- (new Handler()).postDelayed(new LoopRunnable(this), 200L);
- }
- }
- protected void onDraw(Canvas canvas) {
- // wangpeng: 触摸分析辅助线
- // canvas.drawLine( 0 ,getHeight() / 2, getWidth(), getHeight() / 2, paintA);
- // canvas.drawLine( getWidth() / 2 ,0 , getWidth()/ 2, getHeight() , paintA);
- String as[];
- if (arrayList == null) {
- super.onDraw(canvas);
- return;
- }
- as = new String[r];
- w = (int) ((float) totalScrollY / (l * (float) h));
- mCurrentItem = positon + w % arrayList.size();
- int i1;
- if (!isLoop) {
- if (mCurrentItem < 0) {
- mCurrentItem = 0;
- }
- if (mCurrentItem > arrayList.size() - 1) {
- mCurrentItem = arrayList.size() - 1;
- }
- // break;
- } else {
- if (mCurrentItem < 0) {
- mCurrentItem = arrayList.size() + mCurrentItem;
- }
- if (mCurrentItem > arrayList.size() - 1) {
- mCurrentItem = mCurrentItem - arrayList.size();
- }
- // continue;
- }
- do {
- int j2 = (int) ((float) totalScrollY % (l * (float) h));
- int k1 = 0;
- while (k1 < r) {
- int l1 = mCurrentItem - (4 - k1);
- if (isLoop) {
- i1 = l1;
- if (l1 < 0) {
- i1 = l1 + arrayList.size();
- }
- l1 = i1;
- if (i1 > arrayList.size() - 1) {
- l1 = i1 - arrayList.size();
- }
- as[k1] = (String) arrayList.get(l1);
- } else if (l1 < 0) {
- as[k1] = "";
- } else if (l1 > arrayList.size() - 1) {
- as[k1] = "";
- } else {
- as[k1] = (String) arrayList.get(l1);
- }
- k1++;
- }
- k1 = (v - g) / 2;
- canvas.drawLine(0.0F, n, v, n, paintC);
- canvas.drawLine(0.0F, o, v, o, paintC);
- int j1 = 0;
- while (j1 < r) {
- canvas.save();
- double d1 = ((double) ((float) (h * j1) * l - (float) j2) * 3.1415926535897931D) / (double) t;
- float f1 = (float) (90D - (d1 / 3.1415926535897931D) * 180D);
- if (f1 >= 90F || f1 <= -90F) {
- canvas.restore();
- } else {
- int i2 = (int) ((double) u - Math.cos(d1) * (double) u - (Math.sin(d1) * (double) h) / 2D);
- canvas.translate(0.0F, i2);
- canvas.scale(1.0F, (float) Math.sin(d1));
- String str = as[j1];
- int zoomTextSize = textSize;
- double zoom = ((double) textSize - str.length() * 2) / textSize * 1.2;
- zoomTextSize = (int) (textSize * zoom);
- if (zoomTextSize < 23) {
- zoomTextSize = 23;
- }
- paintA.setTextSize(zoomTextSize);
- paintB.setTextSize(zoomTextSize);
- int startX = (int) (n + (getLeft() * 0.5));
- Rect rect = new Rect();
- paintB.getTextBounds(str, 0, str.length(), rect);
- int itemWidth = rect.width();
- int maxWidth = getWidth();
- maxWidth -= 2 * startX;
- if (itemWidth > 0) {
- startX += (maxWidth - itemWidth) * 0.5;
- }
- if (i2 <= n && h + i2 >= n) {
- canvas.save();
- canvas.clipRect(0, 0, v, n - i2);
- canvas.drawText(as[j1], startX, h, paintA);
- canvas.restore();
- canvas.save();
- canvas.clipRect(0, n - i2, v, (int) ((float) h * l));
- canvas.drawText(as[j1], startX, h, paintB);
- canvas.restore();
- } else if (i2 <= o && h + i2 >= o) {
- canvas.save();
- canvas.clipRect(0, 0, v, o - i2);
- canvas.drawText(as[j1], startX, h, paintB);
- canvas.restore();
- canvas.save();
- canvas.clipRect(0, o - i2, v, (int) ((float) h * l));
- canvas.drawText(as[j1], startX, h, paintA);
- canvas.restore();
- } else if (i2 >= n && h + i2 <= o) {
- canvas.clipRect(0, 0, v, (int) ((float) h * l));
- canvas.drawText(as[j1], startX, h, paintB);
- mSelectItem = arrayList.indexOf(as[j1]);
- } else {
- canvas.clipRect(0, 0, v, (int) ((float) h * l));
- canvas.drawText(as[j1], startX, h, paintA);
- }
- canvas.restore();
- }
- j1++;
- }
- super.onDraw(canvas);
- return;
- } while (true);
- }
- protected void onMeasure(int i1, int j1) {
- d();
- setMeasuredDimension(v, s);
- }
- public boolean onTouchEvent(MotionEvent motionevent) {
- switch (motionevent.getAction()) {
- case MotionEvent.ACTION_DOWN:
- x = motionevent.getRawY();
- break;
- case MotionEvent.ACTION_MOVE:
- y = motionevent.getRawY();
- z = x - y;
- x = y;
- totalScrollY = (int) ((float) totalScrollY + z);
- if (!isLoop) {
- if (totalScrollY > (int) ((float) (-positon) * (l * (float) h))) {
- break; /* Loop/switch isn't completed */
- }
- totalScrollY = (int) ((float) (-positon) * (l * (float) h));
- }
- break;
- case MotionEvent.ACTION_UP:
- default:
- if (!gestureDetector.onTouchEvent(motionevent) && motionevent.getAction() == 1) {
- f();
- }
- return true;
- }
- if (totalScrollY < (int) ((float) (arrayList.size() - 1 - positon) * (l * (float) h))) {
- invalidate();
- } else {
- totalScrollY = (int) ((float) (arrayList.size() - 1 - positon) * (l * (float) h));
- invalidate();
- }
- if (!gestureDetector.onTouchEvent(motionevent) && motionevent.getAction() == 1) {
- f();
- }
- return true;
- }
- }
|