b197747d291b8800c2144d15be5316d4abf5cd16.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package eVVM.apk.widget;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Matrix;
  7. import android.graphics.Paint;
  8. import android.util.AttributeSet;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.widget.FrameLayout;
  12. /**
  13. * Created by Android Studio.
  14. * User: erlang
  15. * Date: 2019/6/21
  16. * Describe: ZoomView
  17. */
  18. public class ZoomView extends FrameLayout {
  19. private static final String TAG = "ZoomView";
  20. public ZoomView(Context context, AttributeSet attrs, int defStyle) {
  21. super(context, attrs, defStyle);
  22. }
  23. public ZoomView(Context context, AttributeSet attrs) {
  24. super(context, attrs);
  25. }
  26. public ZoomView(final Context context) {
  27. super(context);
  28. }
  29. /**
  30. * Zooming view listener interface.
  31. */
  32. public interface ZoomViewListener {
  33. void onZoomStarted(float zoom, float zoomx, float zoomy);
  34. void onZooming(float zoom, float zoomx, float zoomy);
  35. void onZoomEnded(float zoom, float zoomx, float zoomy);
  36. }
  37. // zooming
  38. float zoom = 1.0f;
  39. float maxZoom = 2.0f;
  40. float smoothZoom = 1.0f;
  41. float zoomX, zoomY;
  42. float smoothZoomX, smoothZoomY;
  43. private boolean scrolling; // NOPMD by karooolek on 29.06.11 11:45
  44. // minimap variables
  45. private boolean showMinimap = false;
  46. private int miniMapColor = Color.WHITE;
  47. private int miniMapHeight = -1;
  48. private String miniMapCaption;
  49. private float miniMapCaptionSize = 10.0f;
  50. private int miniMapCaptionColor = Color.WHITE;
  51. // touching variables
  52. private long lastTapTime;
  53. private float touchStartX, touchStartY;
  54. private float touchLastX, touchLastY;
  55. private float startd;
  56. private boolean pinching;
  57. private float lastd;
  58. private float lastdx1, lastdy1;
  59. private float lastdx2, lastdy2;
  60. // drawing
  61. private final Matrix m = new Matrix();
  62. private final Paint p = new Paint();
  63. // listener
  64. ZoomViewListener listener;
  65. private Bitmap ch;
  66. public float getZoom() {
  67. return zoom;
  68. }
  69. public float getMaxZoom() {
  70. return maxZoom;
  71. }
  72. public void setMaxZoom(final float maxZoom) {
  73. if (maxZoom < 1.0f) {
  74. return;
  75. }
  76. this.maxZoom = maxZoom;
  77. }
  78. public void setMiniMapEnabled(final boolean showMiniMap) {
  79. this.showMinimap = showMiniMap;
  80. }
  81. public boolean isMiniMapEnabled() {
  82. return showMinimap;
  83. }
  84. public void setMiniMapHeight(final int miniMapHeight) {
  85. if (miniMapHeight < 0) {
  86. return;
  87. }
  88. this.miniMapHeight = miniMapHeight;
  89. }
  90. public int getMiniMapHeight() {
  91. return miniMapHeight;
  92. }
  93. public void setMiniMapColor(final int color) {
  94. miniMapColor = color;
  95. }
  96. public int getMiniMapColor() {
  97. return miniMapColor;
  98. }
  99. public String getMiniMapCaption() {
  100. return miniMapCaption;
  101. }
  102. public void setMiniMapCaption(final String miniMapCaption) {
  103. this.miniMapCaption = miniMapCaption;
  104. }
  105. public float getMiniMapCaptionSize() {
  106. return miniMapCaptionSize;
  107. }
  108. public void setMiniMapCaptionSize(final float size) {
  109. miniMapCaptionSize = size;
  110. }
  111. public int getMiniMapCaptionColor() {
  112. return miniMapCaptionColor;
  113. }
  114. public void setMiniMapCaptionColor(final int color) {
  115. miniMapCaptionColor = color;
  116. }
  117. public void zoomTo(final float zoom, final float x, final float y) {
  118. this.zoom = Math.min(zoom, maxZoom);
  119. zoomX = x;
  120. zoomY = y;
  121. smoothZoomTo(this.zoom, x, y);
  122. }
  123. public void smoothZoomTo(final float zoom, final float x, final float y) {
  124. smoothZoom = clamp(1.0f, zoom, maxZoom);
  125. smoothZoomX = x;
  126. smoothZoomY = y;
  127. if (listener != null) {
  128. listener.onZoomStarted(smoothZoom, x, y);
  129. }
  130. }
  131. public ZoomViewListener getListener() {
  132. return listener;
  133. }
  134. public void setListner(final ZoomViewListener listener) {
  135. this.listener = listener;
  136. }
  137. public float getZoomFocusX() {
  138. return zoomX * zoom;
  139. }
  140. public float getZoomFocusY() {
  141. return zoomY * zoom;
  142. }
  143. @Override
  144. public boolean dispatchTouchEvent(final MotionEvent ev) {
  145. // single touch
  146. if (ev.getPointerCount() == 1) {
  147. processSingleTouchEvent(ev);
  148. }
  149. // // double touch
  150. if (ev.getPointerCount() == 2) {
  151. processDoubleTouchEvent(ev);
  152. }
  153. // redraw
  154. getRootView().invalidate();
  155. invalidate();
  156. return true;
  157. }
  158. private void processSingleTouchEvent(final MotionEvent ev) {
  159. final float x = ev.getX();
  160. final float y = ev.getY();
  161. final float w = miniMapHeight * (float) getWidth() / getHeight();
  162. final float h = miniMapHeight;
  163. final boolean touchingMiniMap = x >= 10.0f && x <= 10.0f + w
  164. && y >= 10.0f && y <= 10.0f + h;
  165. if (showMinimap && smoothZoom > 1.0f && touchingMiniMap) {
  166. processSingleTouchOnMinimap(ev);
  167. } else {
  168. processSingleTouchOutsideMinimap(ev);
  169. }
  170. }
  171. private void processSingleTouchOnMinimap(final MotionEvent ev) {
  172. final float x = ev.getX();
  173. final float y = ev.getY();
  174. final float w = miniMapHeight * (float) getWidth() / getHeight();
  175. final float h = miniMapHeight;
  176. final float zx = (x - 10.0f) / w * getWidth();
  177. final float zy = (y - 10.0f) / h * getHeight();
  178. smoothZoomTo(smoothZoom, zx, zy);
  179. }
  180. public void magnifier() {
  181. if (smoothZoom != 2.0f) {
  182. smoothZoomTo(maxZoom, zoomX, zoomY);
  183. } else {
  184. smoothZoomTo(1.0f, zoomX, zoomY);
  185. }
  186. }
  187. private void processSingleTouchOutsideMinimap(final MotionEvent ev) {
  188. final float x = ev.getX();
  189. final float y = ev.getY();
  190. float lx = x - touchStartX;
  191. float ly = y - touchStartY;
  192. final float l = (float) Math.hypot(lx, ly);
  193. float dx = x - touchLastX;
  194. float dy = y - touchLastY;
  195. touchLastX = x;
  196. touchLastY = y;
  197. switch (ev.getAction()) {
  198. case MotionEvent.ACTION_DOWN:
  199. touchStartX = x;
  200. touchStartY = y;
  201. touchLastX = x;
  202. touchLastY = y;
  203. dx = 0;
  204. dy = 0;
  205. lx = 0;
  206. ly = 0;
  207. scrolling = false;
  208. break;
  209. case MotionEvent.ACTION_MOVE:
  210. if (scrolling || (smoothZoom > 1.0f && l > 30.0f)) {
  211. if (!scrolling) {
  212. scrolling = true;
  213. ev.setAction(MotionEvent.ACTION_CANCEL);
  214. super.dispatchTouchEvent(ev);
  215. }
  216. smoothZoomX -= dx / zoom;
  217. smoothZoomY -= dy / zoom;
  218. return;
  219. }
  220. break;
  221. case MotionEvent.ACTION_OUTSIDE:
  222. case MotionEvent.ACTION_UP:
  223. // tap
  224. if (l < 30.0f) {
  225. // check double tap
  226. if (System.currentTimeMillis() - lastTapTime < 500) {
  227. if (smoothZoom == 1.0f) {
  228. smoothZoomTo(maxZoom, x, y);
  229. } else {
  230. smoothZoomTo(1.0f, getWidth() / 2.0f,
  231. getHeight() / 2.0f);
  232. }
  233. lastTapTime = 0;
  234. ev.setAction(MotionEvent.ACTION_CANCEL);
  235. super.dispatchTouchEvent(ev);
  236. return;
  237. }
  238. lastTapTime = System.currentTimeMillis();
  239. performClick();
  240. }
  241. break;
  242. default:
  243. break;
  244. }
  245. ev.setLocation(zoomX + (x - 0.5f * getWidth()) / zoom, zoomY
  246. + (y - 0.5f * getHeight()) / zoom);
  247. ev.getX();
  248. ev.getY();
  249. super.dispatchTouchEvent(ev);
  250. }
  251. private void processDoubleTouchEvent(final MotionEvent ev) {
  252. final float x1 = ev.getX(0);
  253. final float dx1 = x1 - lastdx1;
  254. lastdx1 = x1;
  255. final float y1 = ev.getY(0);
  256. final float dy1 = y1 - lastdy1;
  257. lastdy1 = y1;
  258. final float x2 = ev.getX(1);
  259. final float dx2 = x2 - lastdx2;
  260. lastdx2 = x2;
  261. final float y2 = ev.getY(1);
  262. final float dy2 = y2 - lastdy2;
  263. lastdy2 = y2;
  264. // pointers distance
  265. final float d = (float) Math.hypot(x2 - x1, y2 - y1);
  266. final float dd = d - lastd;
  267. lastd = d;
  268. final float ld = Math.abs(d - startd);
  269. Math.atan2(y2 - y1, x2 - x1);
  270. switch (ev.getAction()) {
  271. case MotionEvent.ACTION_DOWN:
  272. startd = d;
  273. pinching = false;
  274. break;
  275. case MotionEvent.ACTION_MOVE:
  276. if (pinching || ld > 30.0f) {
  277. pinching = true;
  278. final float dxk = 0.5f * (dx1 + dx2);
  279. final float dyk = 0.5f * (dy1 + dy2);
  280. smoothZoomTo(Math.max(1.0f, zoom * d / (d - dd)), zoomX - dxk
  281. / zoom, zoomY - dyk / zoom);
  282. }
  283. break;
  284. case MotionEvent.ACTION_UP:
  285. default:
  286. pinching = false;
  287. break;
  288. }
  289. ev.setAction(MotionEvent.ACTION_CANCEL);
  290. super.dispatchTouchEvent(ev);
  291. }
  292. private float clamp(final float min, final float value, final float max) {
  293. return Math.max(min, Math.min(value, max));
  294. }
  295. private float lerp(final float a, final float b, final float k) {
  296. return a + (b - a) * k;
  297. }
  298. private float bias(final float a, final float b, final float k) {
  299. return Math.abs(b - a) >= k ? a + k * Math.signum(b - a) : b;
  300. }
  301. @Override
  302. protected void dispatchDraw(final Canvas canvas) {
  303. // do zoom
  304. zoom = lerp(bias(zoom, smoothZoom, 0.05f), smoothZoom, 0.2f);
  305. smoothZoomX = clamp(0.5f * getWidth() / smoothZoom, smoothZoomX,
  306. getWidth() - 0.5f * getWidth() / smoothZoom);
  307. smoothZoomY = clamp(0.5f * getHeight() / smoothZoom, smoothZoomY,
  308. getHeight() - 0.5f * getHeight() / smoothZoom);
  309. zoomX = lerp(bias(zoomX, smoothZoomX, 0.1f), smoothZoomX, 0.35f);
  310. zoomY = lerp(bias(zoomY, smoothZoomY, 0.1f), smoothZoomY, 0.35f);
  311. if (zoom != smoothZoom && listener != null) {
  312. listener.onZooming(zoom, zoomX, zoomY);
  313. }
  314. final boolean animating = Math.abs(zoom - smoothZoom) > 0.0000001f
  315. || Math.abs(zoomX - smoothZoomX) > 0.0000001f
  316. || Math.abs(zoomY - smoothZoomY) > 0.0000001f;
  317. // nothing to draw
  318. if (getChildCount() == 0) {
  319. return;
  320. }
  321. // prepare matrix
  322. m.setTranslate(0.5f * getWidth(), 0.5f * getHeight());
  323. m.preScale(zoom, zoom);
  324. m.preTranslate(
  325. -clamp(0.5f * getWidth() / zoom, zoomX, getWidth() - 0.5f
  326. * getWidth() / zoom),
  327. -clamp(0.5f * getHeight() / zoom, zoomY, getHeight() - 0.5f
  328. * getHeight() / zoom));
  329. // get view
  330. final View v = getChildAt(0);
  331. m.preTranslate(v.getLeft(), v.getTop());
  332. // get drawing cache if available
  333. if (animating && ch == null && isAnimationCacheEnabled()) {
  334. v.setDrawingCacheEnabled(true);
  335. ch = v.getDrawingCache();
  336. }
  337. // draw using cache while animating
  338. if (animating && isAnimationCacheEnabled() && ch != null) {
  339. p.setColor(0xffffffff);
  340. canvas.drawBitmap(ch, m, p);
  341. } else { // zoomed or cache unavailable
  342. ch = null;
  343. canvas.save();
  344. canvas.concat(m);
  345. v.draw(canvas);
  346. canvas.restore();
  347. }
  348. // draw minimap
  349. if (showMinimap) {
  350. if (miniMapHeight < 0) {
  351. miniMapHeight = getHeight() / 4;
  352. }
  353. canvas.translate(10.0f, 10.0f);
  354. p.setColor(0x80000000 | 0x00ffffff & miniMapColor);
  355. final float w = miniMapHeight * (float) getWidth() / getHeight();
  356. final float h = miniMapHeight;
  357. canvas.drawRect(0.0f, 0.0f, w, h, p);
  358. if (miniMapCaption != null && miniMapCaption.length() > 0) {
  359. p.setTextSize(miniMapCaptionSize);
  360. p.setColor(miniMapCaptionColor);
  361. p.setAntiAlias(true);
  362. canvas.drawText(miniMapCaption, 10.0f,
  363. 10.0f + miniMapCaptionSize, p);
  364. p.setAntiAlias(false);
  365. }
  366. p.setColor(0x80000000 | 0x00ffffff & miniMapColor);
  367. final float dx = w * zoomX / getWidth();
  368. final float dy = h * zoomY / getHeight();
  369. canvas.drawRect(dx - 0.5f * w / zoom, dy - 0.5f * h / zoom, dx
  370. + 0.5f * w / zoom, dy + 0.5f * h / zoom, p);
  371. canvas.translate(-10.0f, -10.0f);
  372. }
  373. // redraw
  374. getRootView().invalidate();
  375. invalidate();
  376. }
  377. }