PageTreeNode.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package com.poqop.document;
  2. import android.graphics.*;
  3. import java.lang.ref.SoftReference;
  4. /**
  5. *ÏÔʾÄÚÈÝ¡£
  6. * @author Administrator
  7. *
  8. */
  9. class PageTreeNode {
  10. private static final int SLICE_SIZE = 65535;
  11. private Bitmap bitmap;
  12. private SoftReference<Bitmap> bitmapWeakReference;
  13. private boolean decodingNow;
  14. private final RectF pageSliceBounds;
  15. private final Page page;
  16. private PageTreeNode[] children;
  17. private final int treeNodeDepthLevel;
  18. private Matrix matrix = new Matrix();
  19. private final Paint bitmapPaint = new Paint();
  20. private DocumentView documentView;
  21. private boolean invalidateFlag;
  22. private Rect targetRect;
  23. private RectF targetRectF;
  24. PageTreeNode(DocumentView documentView, RectF localPageSliceBounds, Page page, int treeNodeDepthLevel, PageTreeNode parent) {
  25. this.documentView = documentView;
  26. this.pageSliceBounds = evaluatePageSliceBounds(localPageSliceBounds, parent);
  27. this.page = page;
  28. this.treeNodeDepthLevel = treeNodeDepthLevel;
  29. }
  30. /*
  31. * ¸üÐÂÏÔʾ
  32. */
  33. public void updateVisibility() {
  34. invalidateChildren();
  35. if (children != null) {
  36. for (PageTreeNode child : children) {
  37. child.updateVisibility();
  38. }
  39. }
  40. if (isVisible()) {
  41. if (!thresholdHit()) {
  42. if (getBitmap() != null && !invalidateFlag) {
  43. restoreBitmapReference();
  44. } else {
  45. decodePageTreeNode();
  46. }
  47. }
  48. }
  49. if (!isVisibleAndNotHiddenByChildren()) {
  50. stopDecodingThisNode();
  51. setBitmap(null);
  52. }
  53. }
  54. public void invalidate() {
  55. invalidateChildren();
  56. invalidateRecursive();
  57. updateVisibility();
  58. }
  59. private void invalidateRecursive() {
  60. invalidateFlag = true;
  61. if (children != null) {
  62. for (PageTreeNode child : children) {
  63. child.invalidateRecursive();
  64. }
  65. }
  66. stopDecodingThisNode();
  67. }
  68. void invalidateNodeBounds() {
  69. targetRect = null;
  70. targetRectF = null;
  71. if (children != null) {
  72. for (PageTreeNode child : children) {
  73. child.invalidateNodeBounds();
  74. }
  75. }
  76. }
  77. void draw(Canvas canvas) {
  78. if (getBitmap() != null) {
  79. canvas.drawBitmap(getBitmap(), new Rect(0, 0, getBitmap().getWidth(), getBitmap().getHeight()), getTargetRect(), bitmapPaint);
  80. }
  81. if (children == null) {
  82. return;
  83. }
  84. for (PageTreeNode child : children) {
  85. child.draw(canvas);
  86. }
  87. }
  88. private boolean isVisible() {
  89. return RectF.intersects(documentView.getViewRect(), getTargetRectF());
  90. }
  91. private RectF getTargetRectF() {
  92. if (targetRectF == null) {
  93. targetRectF = new RectF(getTargetRect());
  94. }
  95. return targetRectF;
  96. }
  97. private void invalidateChildren() {
  98. if (thresholdHit() && children == null && isVisible()) {
  99. final int newThreshold = treeNodeDepthLevel * 2;
  100. children = new PageTreeNode[]
  101. {
  102. new PageTreeNode(documentView, new RectF(0, 0, 0.5f, 0.5f), page, newThreshold, this),
  103. new PageTreeNode(documentView, new RectF(0.5f, 0, 1.0f, 0.5f), page, newThreshold, this),
  104. new PageTreeNode(documentView, new RectF(0, 0.5f, 0.5f, 1.0f), page, newThreshold, this),
  105. new PageTreeNode(documentView, new RectF(0.5f, 0.5f, 1.0f, 1.0f), page, newThreshold, this)
  106. };
  107. }
  108. if (!thresholdHit() && getBitmap() != null || !isVisible()) {
  109. recycleChildren();
  110. }
  111. }
  112. private boolean thresholdHit() {
  113. float zoom = documentView.zoomModel.getZoom();
  114. int mainWidth = documentView.getWidth();
  115. float height = page.getPageHeight(mainWidth, zoom);
  116. return (mainWidth * zoom * height) / (treeNodeDepthLevel * treeNodeDepthLevel) > SLICE_SIZE;
  117. }
  118. public Bitmap getBitmap() {
  119. return bitmapWeakReference != null ? bitmapWeakReference.get() : null;
  120. }
  121. private void restoreBitmapReference() {
  122. setBitmap(getBitmap());
  123. }
  124. private void decodePageTreeNode() {
  125. if (isDecodingNow()) {
  126. return;
  127. }
  128. setDecodingNow(true);
  129. documentView.decodeService.decodePage(this, page.index, new DecodeService.DecodeCallback() {
  130. public void decodeComplete(final Bitmap bitmap) {
  131. documentView.post(new Runnable() {
  132. public void run() {
  133. setBitmap(bitmap);
  134. invalidateFlag = false;
  135. setDecodingNow(false);
  136. page.setAspectRatio(documentView.decodeService.getPageWidth(page.index), documentView.decodeService.getPageHeight(page.index));
  137. invalidateChildren();
  138. }
  139. });
  140. }
  141. }, documentView.zoomModel.getZoom(), pageSliceBounds);
  142. }
  143. private RectF evaluatePageSliceBounds(RectF localPageSliceBounds, PageTreeNode parent) {
  144. if (parent == null) {
  145. return localPageSliceBounds;
  146. }
  147. final Matrix matrix = new Matrix();
  148. matrix.postScale(parent.pageSliceBounds.width(), parent.pageSliceBounds.height());
  149. matrix.postTranslate(parent.pageSliceBounds.left, parent.pageSliceBounds.top);
  150. final RectF sliceBounds = new RectF();
  151. matrix.mapRect(sliceBounds, localPageSliceBounds);
  152. return sliceBounds;
  153. }
  154. private void setBitmap(Bitmap bitmap) {
  155. if (bitmap != null && bitmap.getWidth() == -1 && bitmap.getHeight() == -1) {
  156. return;
  157. }
  158. if (this.bitmap != bitmap) {
  159. if (bitmap != null) {
  160. if (this.bitmap != null) {
  161. this.bitmap.recycle();
  162. }
  163. bitmapWeakReference = new SoftReference<Bitmap>(bitmap);
  164. documentView.postInvalidate();
  165. }
  166. this.bitmap = bitmap;
  167. }
  168. }
  169. private boolean isDecodingNow() {
  170. return decodingNow;
  171. }
  172. private void setDecodingNow(boolean decodingNow) {
  173. if (this.decodingNow != decodingNow) {
  174. this.decodingNow = decodingNow;
  175. if (decodingNow) {
  176. documentView.progressModel.increase();
  177. } else {
  178. documentView.progressModel.decrease();
  179. }
  180. }
  181. }
  182. private Rect getTargetRect() {
  183. if (targetRect == null) {
  184. matrix.reset();
  185. matrix.postScale(page.bounds.width(), page.bounds.height());
  186. matrix.postTranslate(page.bounds.left, page.bounds.top);
  187. RectF targetRectF = new RectF();
  188. matrix.mapRect(targetRectF, pageSliceBounds);
  189. targetRect = new Rect((int) targetRectF.left, (int) targetRectF.top, (int) targetRectF.right, (int) targetRectF.bottom);
  190. }
  191. return targetRect;
  192. }
  193. private void stopDecodingThisNode() {
  194. if (!isDecodingNow()) {
  195. return;
  196. }
  197. documentView.decodeService.stopDecoding(this);
  198. setDecodingNow(false);
  199. }
  200. private boolean isHiddenByChildren() {
  201. if (children == null) {
  202. return false;
  203. }
  204. for (PageTreeNode child : children) {
  205. if (child.getBitmap() == null) {
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. private void recycleChildren() {
  212. if (children == null) {
  213. return;
  214. }
  215. for (PageTreeNode child : children) {
  216. child.recycle();
  217. }
  218. if (!childrenContainBitmaps()) {
  219. children = null;
  220. }
  221. }
  222. private boolean containsBitmaps() {
  223. return getBitmap() != null || childrenContainBitmaps();
  224. }
  225. private boolean childrenContainBitmaps() {
  226. if (children == null) {
  227. return false;
  228. }
  229. for (PageTreeNode child : children) {
  230. if (child.containsBitmaps()) {
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. private void recycle() {
  237. stopDecodingThisNode();
  238. setBitmap(null);
  239. if (children != null) {
  240. for (PageTreeNode child : children) {
  241. child.recycle();
  242. }
  243. }
  244. }
  245. private boolean isVisibleAndNotHiddenByChildren() {
  246. return isVisible() && !isHiddenByChildren();
  247. }
  248. }