cb15c5d5e663bbc3d6e7b8e3dade44371848ae32.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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: zbb
  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. private void processSingleTouchOutsideMinimap(final MotionEvent ev) {
  181. final float x = ev.getX();
  182. final float y = ev.getY();
  183. float lx = x - touchStartX;
  184. float ly = y - touchStartY;
  185. final float l = (float) Math.hypot(lx, ly);
  186. float dx = x - touchLastX;
  187. float dy = y - touchLastY;
  188. touchLastX = x;
  189. touchLastY = y;
  190. switch (ev.getAction()) {
  191. case MotionEvent.ACTION_DOWN:
  192. touchStartX = x;
  193. touchStartY = y;
  194. touchLastX = x;
  195. touchLastY = y;
  196. dx = 0;
  197. dy = 0;
  198. lx = 0;
  199. ly = 0;
  200. scrolling = false;
  201. break;
  202. case MotionEvent.ACTION_MOVE:
  203. if (scrolling || (smoothZoom > 1.0f && l > 30.0f)) {
  204. if (!scrolling) {
  205. scrolling = true;
  206. ev.setAction(MotionEvent.ACTION_CANCEL);
  207. super.dispatchTouchEvent(ev);
  208. }
  209. smoothZoomX -= dx / zoom;
  210. smoothZoomY -= dy / zoom;
  211. return;
  212. }
  213. break;
  214. case MotionEvent.ACTION_OUTSIDE:
  215. case MotionEvent.ACTION_UP:
  216. // tap
  217. if (l < 30.0f) {
  218. // check double tap
  219. if (System.currentTimeMillis() - lastTapTime < 500) {
  220. if (smoothZoom == 1.0f) {
  221. smoothZoomTo(maxZoom, x, y);
  222. } else {
  223. smoothZoomTo(1.0f, getWidth() / 2.0f,
  224. getHeight() / 2.0f);
  225. }
  226. lastTapTime = 0;
  227. ev.setAction(MotionEvent.ACTION_CANCEL);
  228. super.dispatchTouchEvent(ev);
  229. return;
  230. }
  231. lastTapTime = System.currentTimeMillis();
  232. performClick();
  233. }
  234. break;
  235. default:
  236. break;
  237. }
  238. ev.setLocation(zoomX + (x - 0.5f * getWidth()) / zoom, zoomY
  239. + (y - 0.5f * getHeight()) / zoom);
  240. ev.getX();
  241. ev.getY();
  242. super.dispatchTouchEvent(ev);
  243. }
  244. private void processDoubleTouchEvent(final MotionEvent ev) {
  245. final float x1 = ev.getX(0);
  246. final float dx1 = x1 - lastdx1;
  247. lastdx1 = x1;
  248. final float y1 = ev.getY(0);
  249. final float dy1 = y1 - lastdy1;
  250. lastdy1 = y1;
  251. final float x2 = ev.getX(1);
  252. final float dx2 = x2 - lastdx2;
  253. lastdx2 = x2;
  254. final float y2 = ev.getY(1);
  255. final float dy2 = y2 - lastdy2;
  256. lastdy2 = y2;
  257. // pointers distance
  258. final float d = (float) Math.hypot(x2 - x1, y2 - y1);
  259. final float dd = d - lastd;
  260. lastd = d;
  261. final float ld = Math.abs(d - startd);
  262. Math.atan2(y2 - y1, x2 - x1);
  263. switch (ev.getAction()) {
  264. case MotionEvent.ACTION_DOWN:
  265. startd = d;
  266. pinching = false;
  267. break;
  268. case MotionEvent.ACTION_MOVE:
  269. if (pinching || ld > 30.0f) {
  270. pinching = true;
  271. final float dxk = 0.5f * (dx1 + dx2);
  272. final float dyk = 0.5f * (dy1 + dy2);
  273. smoothZoomTo(Math.max(1.0f, zoom * d / (d - dd)), zoomX - dxk
  274. / zoom, zoomY - dyk / zoom);
  275. }
  276. break;
  277. case MotionEvent.ACTION_UP:
  278. default:
  279. pinching = false;
  280. break;
  281. }
  282. ev.setAction(MotionEvent.ACTION_CANCEL);
  283. super.dispatchTouchEvent(ev);
  284. }
  285. private float clamp(final float min, final float value, final float max) {
  286. return Math.max(min, Math.min(value, max));
  287. }
  288. private float lerp(final float a, final float b, final float k) {
  289. return a + (b - a) * k;
  290. }
  291. private float bias(final float a, final float b, final float k) {
  292. return Math.abs(b - a) >= k ? a + k * Math.signum(b - a) : b;
  293. }
  294. @Override
  295. protected void dispatchDraw(final Canvas canvas) {
  296. // do zoom
  297. zoom = lerp(bias(zoom, smoothZoom, 0.05f), smoothZoom, 0.2f);
  298. smoothZoomX = clamp(0.5f * getWidth() / smoothZoom, smoothZoomX,
  299. getWidth() - 0.5f * getWidth() / smoothZoom);
  300. smoothZoomY = clamp(0.5f * getHeight() / smoothZoom, smoothZoomY,
  301. getHeight() - 0.5f * getHeight() / smoothZoom);
  302. zoomX = lerp(bias(zoomX, smoothZoomX, 0.1f), smoothZoomX, 0.35f);
  303. zoomY = lerp(bias(zoomY, smoothZoomY, 0.1f), smoothZoomY, 0.35f);
  304. if (zoom != smoothZoom && listener != null) {
  305. listener.onZooming(zoom, zoomX, zoomY);
  306. }
  307. final boolean animating = Math.abs(zoom - smoothZoom) > 0.0000001f
  308. || Math.abs(zoomX - smoothZoomX) > 0.0000001f
  309. || Math.abs(zoomY - smoothZoomY) > 0.0000001f;
  310. // nothing to draw
  311. if (getChildCount() == 0) {
  312. return;
  313. }
  314. // prepare matrix
  315. m.setTranslate(0.5f * getWidth(), 0.5f * getHeight());
  316. m.preScale(zoom, zoom);
  317. m.preTranslate(
  318. -clamp(0.5f * getWidth() / zoom, zoomX, getWidth() - 0.5f
  319. * getWidth() / zoom),
  320. -clamp(0.5f * getHeight() / zoom, zoomY, getHeight() - 0.5f
  321. * getHeight() / zoom));
  322. // get view
  323. final View v = getChildAt(0);
  324. m.preTranslate(v.getLeft(), v.getTop());
  325. // get drawing cache if available
  326. if (animating && ch == null && isAnimationCacheEnabled()) {
  327. v.setDrawingCacheEnabled(true);
  328. ch = v.getDrawingCache();
  329. }
  330. // draw using cache while animating
  331. if (animating && isAnimationCacheEnabled() && ch != null) {
  332. p.setColor(0xffffffff);
  333. canvas.drawBitmap(ch, m, p);
  334. } else { // zoomed or cache unavailable
  335. ch = null;
  336. canvas.save();
  337. canvas.concat(m);
  338. v.draw(canvas);
  339. canvas.restore();
  340. }
  341. // draw minimap
  342. if (showMinimap) {
  343. if (miniMapHeight < 0) {
  344. miniMapHeight = getHeight() / 4;
  345. }
  346. canvas.translate(10.0f, 10.0f);
  347. p.setColor(0x80000000 | 0x00ffffff & miniMapColor);
  348. final float w = miniMapHeight * (float) getWidth() / getHeight();
  349. final float h = miniMapHeight;
  350. canvas.drawRect(0.0f, 0.0f, w, h, p);
  351. if (miniMapCaption != null && miniMapCaption.length() > 0) {
  352. p.setTextSize(miniMapCaptionSize);
  353. p.setColor(miniMapCaptionColor);
  354. p.setAntiAlias(true);
  355. canvas.drawText(miniMapCaption, 10.0f,
  356. 10.0f + miniMapCaptionSize, p);
  357. p.setAntiAlias(false);
  358. }
  359. p.setColor(0x80000000 | 0x00ffffff & miniMapColor);
  360. final float dx = w * zoomX / getWidth();
  361. final float dy = h * zoomY / getHeight();
  362. canvas.drawRect(dx - 0.5f * w / zoom, dy - 0.5f * h / zoom, dx
  363. + 0.5f * w / zoom, dy + 0.5f * h / zoom, p);
  364. canvas.translate(-10.0f, -10.0f);
  365. }
  366. // redraw
  367. getRootView().invalidate();
  368. invalidate();
  369. }
  370. }