1f830b68d7047e28c26183e28f45b604cb80eb2f.svn-base 13 KB

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