12143a49bdd59da7b2bc0f8cb05732ebecc503fb.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package eVVM.apk.helper.basepicker;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import android.graphics.Rect;
  6. import android.graphics.Typeface;
  7. import android.os.Handler;
  8. import android.util.AttributeSet;
  9. import android.view.GestureDetector;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import java.util.List;
  13. import java.util.Timer;
  14. /**
  15. * Created by Android Studio.
  16. * User: zbb
  17. * Date: 2019/6/4
  18. * Describe: LoopMsgView
  19. */
  20. public class LoopMsgView extends View {
  21. Timer mTimer;
  22. int totalScrollY;
  23. Handler handler;
  24. LoopListener loopListener;
  25. private GestureDetector gestureDetector;
  26. private int mSelectItem;
  27. private GestureDetector.SimpleOnGestureListener simpleOnGestureListener;
  28. Context context;
  29. Paint paintA;
  30. Paint paintB;
  31. Paint paintC;
  32. List arrayList;
  33. int textSize;
  34. int g;
  35. int h;
  36. int colorGray;
  37. int colorBlack;
  38. int colorGrayLight;
  39. float l;
  40. boolean isLoop;
  41. int n;
  42. int o;
  43. int mCurrentItem;
  44. int positon;
  45. int r;
  46. int s;
  47. int t;
  48. int u;
  49. int v;
  50. int w;
  51. float x;
  52. float y;
  53. float z;
  54. public LoopMsgView(Context context) {
  55. super(context);
  56. initLoopView(context);
  57. }
  58. public LoopMsgView(Context context, AttributeSet attributeset) {
  59. super(context, attributeset);
  60. initLoopView(context);
  61. }
  62. public LoopMsgView(Context context, AttributeSet attributeset, int i1) {
  63. super(context, attributeset, i1);
  64. initLoopView(context);
  65. }
  66. private void initLoopView(Context context) {
  67. textSize = 0;
  68. colorGray = 0xffa7a7a7; //0xffafafaf;
  69. colorBlack = 0xff572dc9;// 0xff313131;
  70. colorGrayLight = 0xffdcdcdc; //0xffc5c5c5;
  71. l = 2.0F;
  72. isLoop = true;
  73. positon = -1;
  74. r = 9;
  75. x = 0.0F;
  76. y = 0.0F;
  77. z = 0.0F;
  78. totalScrollY = 0;
  79. simpleOnGestureListener = new LoopViewGestureListener(this);
  80. handler = new MessageHandler(this);
  81. this.context = context;
  82. setTextSize(16F);
  83. }
  84. static int getSelectItem(LoopMsgView loopview) {
  85. return loopview.getCurrentItem();
  86. }
  87. static void b(LoopMsgView loopview) {
  88. loopview.f();
  89. }
  90. private void d() {
  91. if (arrayList == null) {
  92. return;
  93. }
  94. paintA = new Paint();
  95. paintA.setColor(colorGray);
  96. paintA.setAntiAlias(true);
  97. paintA.setTypeface(Typeface.MONOSPACE);
  98. paintA.setTextSize(textSize);
  99. paintB = new Paint();
  100. paintB.setColor(colorBlack);
  101. paintB.setAntiAlias(true);
  102. paintB.setTextScaleX(1.05F);
  103. paintB.setTypeface(Typeface.MONOSPACE);
  104. paintB.setTextSize(textSize);
  105. paintC = new Paint();
  106. paintC.setColor(colorGrayLight);
  107. paintC.setAntiAlias(true);
  108. paintC.setTypeface(Typeface.MONOSPACE);
  109. paintC.setTextSize(textSize);
  110. if (android.os.Build.VERSION.SDK_INT >= 11) {
  111. setLayerType(1, null);
  112. }
  113. gestureDetector = new GestureDetector(context, simpleOnGestureListener);
  114. gestureDetector.setIsLongpressEnabled(false);
  115. e();
  116. t = (int) ((float) h * l * (float) (r - 1));
  117. s = (int) ((double) (t * 2) / Math.PI);
  118. u = (int) ((double) t / Math.PI);
  119. v = g + textSize;
  120. n = (int) (((float) s - l * (float) h) / 2.0F);
  121. o = (int) (((float) s + l * (float) h) / 2.0F);
  122. if (positon == -1) {
  123. if (isLoop) {
  124. positon = (arrayList.size() + 1) / 2;
  125. } else {
  126. positon = 0;
  127. }
  128. }
  129. mCurrentItem = positon;
  130. }
  131. private void e() {
  132. Rect rect = new Rect();
  133. for (int i1 = 0; i1 < arrayList.size(); i1++) {
  134. // String s1 = (String) arrayList.get(i1);
  135. // wangpeng:调整可触摸区域的宽度为4个字符
  136. paintB.getTextBounds("0000", 0, "0000".length(), rect);
  137. int j1 = rect.width();
  138. // wangpeng:调整区域增大2倍,提升体验。
  139. j1 = (int) (j1 * 3.0f);
  140. if (j1 > g) {
  141. g = j1;
  142. }
  143. paintB.getTextBounds("\u661F\u671F", 0, 2, rect);
  144. j1 = rect.height();
  145. if (j1 > h) {
  146. h = j1;
  147. }
  148. }
  149. }
  150. private void f() {
  151. int i1 = (int) ((float) totalScrollY % (l * (float) h));
  152. Timer timer = new Timer();
  153. mTimer = timer;
  154. timer.schedule(new MTimer(this, i1, timer), 0L, 10L);
  155. }
  156. public final void setNotLoop() {
  157. isLoop = false;
  158. }
  159. /**
  160. * 设置是否循环滚动
  161. *
  162. * @param cyclic
  163. */
  164. public final void setCyclic(boolean cyclic) {
  165. isLoop = cyclic;
  166. }
  167. public final void setTextSize(float size) {
  168. if (size > 0.0F) {
  169. textSize = (int) (context.getResources().getDisplayMetrics().density * size);
  170. }
  171. }
  172. public final void setCurrentItem(int position) {
  173. this.positon = position;
  174. totalScrollY = 0;
  175. f();
  176. invalidate();
  177. }
  178. public final void setListener(LoopListener LoopListener) {
  179. loopListener = LoopListener;
  180. }
  181. public final void setArrayList(List arraylist) {
  182. arrayList = arraylist;
  183. d();
  184. invalidate();
  185. }
  186. public final int getCurrentItem() {
  187. if (mCurrentItem <= 0) {
  188. return 0;
  189. }
  190. return mCurrentItem;
  191. }
  192. public final String getCurrentItemValue(){
  193. return String.valueOf(arrayList.get(getCurrentItem())).trim();
  194. }
  195. protected final void b(float f1) {
  196. Timer timer = new Timer();
  197. mTimer = timer;
  198. timer.schedule(new LoopTimerTask(this, f1, timer), 0L, 20L);
  199. }
  200. protected final void b(int i1) {
  201. Timer timer = new Timer();
  202. mTimer = timer;
  203. timer.schedule(new MyTimerTask(this, i1, timer), 0L, 20L);
  204. }
  205. protected final void c() {
  206. if (loopListener != null) {
  207. (new Handler()).postDelayed(new LoopRunnable(this), 200L);
  208. }
  209. }
  210. protected void onDraw(Canvas canvas) {
  211. // wangpeng: 触摸分析辅助线
  212. // canvas.drawLine( 0 ,getHeight() / 2, getWidth(), getHeight() / 2, paintA);
  213. // canvas.drawLine( getWidth() / 2 ,0 , getWidth()/ 2, getHeight() , paintA);
  214. String as[];
  215. if (arrayList == null) {
  216. super.onDraw(canvas);
  217. return;
  218. }
  219. as = new String[r];
  220. w = (int) ((float) totalScrollY / (l * (float) h));
  221. mCurrentItem = positon + w % arrayList.size();
  222. int i1;
  223. if (!isLoop) {
  224. if (mCurrentItem < 0) {
  225. mCurrentItem = 0;
  226. }
  227. if (mCurrentItem > arrayList.size() - 1) {
  228. mCurrentItem = arrayList.size() - 1;
  229. }
  230. // break;
  231. } else {
  232. if (mCurrentItem < 0) {
  233. mCurrentItem = arrayList.size() + mCurrentItem;
  234. }
  235. if (mCurrentItem > arrayList.size() - 1) {
  236. mCurrentItem = mCurrentItem - arrayList.size();
  237. }
  238. // continue;
  239. }
  240. do {
  241. int j2 = (int) ((float) totalScrollY % (l * (float) h));
  242. int k1 = 0;
  243. while (k1 < r) {
  244. int l1 = mCurrentItem - (4 - k1);
  245. if (isLoop) {
  246. i1 = l1;
  247. if (l1 < 0) {
  248. i1 = l1 + arrayList.size();
  249. }
  250. l1 = i1;
  251. if (i1 > arrayList.size() - 1) {
  252. l1 = i1 - arrayList.size();
  253. }
  254. as[k1] = (String) arrayList.get(l1);
  255. } else if (l1 < 0) {
  256. as[k1] = "";
  257. } else if (l1 > arrayList.size() - 1) {
  258. as[k1] = "";
  259. } else {
  260. as[k1] = (String) arrayList.get(l1);
  261. }
  262. k1++;
  263. }
  264. k1 = (v - g) / 2;
  265. canvas.drawLine(0.0F, n, v, n, paintC);
  266. canvas.drawLine(0.0F, o, v, o, paintC);
  267. int j1 = 0;
  268. while (j1 < r) {
  269. canvas.save();
  270. double d1 = ((double) ((float) (h * j1) * l - (float) j2) * 3.1415926535897931D) / (double) t;
  271. float f1 = (float) (90D - (d1 / 3.1415926535897931D) * 180D);
  272. if (f1 >= 90F || f1 <= -90F) {
  273. canvas.restore();
  274. } else {
  275. int i2 = (int) ((double) u - Math.cos(d1) * (double) u - (Math.sin(d1) * (double) h) / 2D);
  276. canvas.translate(0.0F, i2);
  277. canvas.scale(1.0F, (float) Math.sin(d1));
  278. String str = as[j1];
  279. int zoomTextSize = textSize;
  280. double zoom = ((double) textSize - str.length() * 2) / textSize * 1.2;
  281. zoomTextSize = (int) (textSize * zoom);
  282. if (zoomTextSize < 10) {
  283. zoomTextSize = 10;
  284. }
  285. paintA.setTextSize(zoomTextSize);
  286. paintB.setTextSize(zoomTextSize);
  287. int startX = (int) (n + (getLeft() * 0.5));
  288. Rect rect = new Rect();
  289. paintB.getTextBounds(str, 0, str.length(), rect);
  290. int itemWidth = rect.width();
  291. int maxWidth = getWidth();
  292. maxWidth -= 2 * startX;
  293. if (itemWidth > 0) {
  294. startX += (maxWidth - itemWidth) * 0.5;
  295. }
  296. if (i2 <= n && h + i2 >= n) {
  297. canvas.save();
  298. canvas.clipRect(0, 0, v, n - i2);
  299. canvas.drawText(as[j1], startX, h, paintA);
  300. canvas.restore();
  301. canvas.save();
  302. canvas.clipRect(0, n - i2, v, (int) ((float) h * l));
  303. canvas.drawText(as[j1], startX, h, paintB);
  304. canvas.restore();
  305. } else if (i2 <= o && h + i2 >= o) {
  306. canvas.save();
  307. canvas.clipRect(0, 0, v, o - i2);
  308. canvas.drawText(as[j1], startX, h, paintB);
  309. canvas.restore();
  310. canvas.save();
  311. canvas.clipRect(0, o - i2, v, (int) ((float) h * l));
  312. canvas.drawText(as[j1], startX, h, paintA);
  313. canvas.restore();
  314. } else if (i2 >= n && h + i2 <= o) {
  315. canvas.clipRect(0, 0, v, (int) ((float) h * l));
  316. canvas.drawText(as[j1], startX, h, paintB);
  317. mSelectItem = arrayList.indexOf(as[j1]);
  318. } else {
  319. canvas.clipRect(0, 0, v, (int) ((float) h * l));
  320. canvas.drawText(as[j1], startX, h, paintA);
  321. }
  322. canvas.restore();
  323. }
  324. j1++;
  325. }
  326. super.onDraw(canvas);
  327. return;
  328. } while (true);
  329. }
  330. protected void onMeasure(int i1, int j1) {
  331. d();
  332. setMeasuredDimension(v, s);
  333. }
  334. public boolean onTouchEvent(MotionEvent motionevent) {
  335. switch (motionevent.getAction()) {
  336. case MotionEvent.ACTION_DOWN:
  337. x = motionevent.getRawY();
  338. break;
  339. case MotionEvent.ACTION_MOVE:
  340. y = motionevent.getRawY();
  341. z = x - y;
  342. x = y;
  343. totalScrollY = (int) ((float) totalScrollY + z);
  344. if (!isLoop) {
  345. if (totalScrollY > (int) ((float) (-positon) * (l * (float) h))) {
  346. break; /* Loop/switch isn't completed */
  347. }
  348. totalScrollY = (int) ((float) (-positon) * (l * (float) h));
  349. }
  350. break;
  351. case MotionEvent.ACTION_UP:
  352. default:
  353. if (!gestureDetector.onTouchEvent(motionevent) && motionevent.getAction() == 1) {
  354. f();
  355. }
  356. return true;
  357. }
  358. if (totalScrollY < (int) ((float) (arrayList.size() - 1 - positon) * (l * (float) h))) {
  359. invalidate();
  360. } else {
  361. totalScrollY = (int) ((float) (arrayList.size() - 1 - positon) * (l * (float) h));
  362. invalidate();
  363. }
  364. if (!gestureDetector.onTouchEvent(motionevent) && motionevent.getAction() == 1) {
  365. f();
  366. }
  367. return true;
  368. }
  369. }