Page.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.poqop.document;
  2. import android.graphics.Canvas;
  3. import android.graphics.Color;
  4. import android.graphics.Paint;
  5. import android.graphics.RectF;
  6. import android.text.TextPaint;
  7. /**
  8. * 显示每页内容,分割线,和页数
  9. * @author Administrator
  10. */
  11. class Page {
  12. final int index;
  13. RectF bounds;
  14. private PageTreeNode node;
  15. private DocumentView documentView;
  16. private final TextPaint textPaint = textPaint();
  17. private final Paint fillPaint = fillPaint();
  18. private final Paint strokePaint = strokePaint();
  19. Page(DocumentView documentView, int index) {
  20. this.documentView = documentView;
  21. this.index = index;
  22. node = new PageTreeNode(documentView, new RectF(0, 0, 1, 1), this, 1, null);
  23. }
  24. private float aspectRatio;
  25. float getPageHeight(int mainWidth, float zoom) {
  26. return mainWidth / getAspectRatio() * zoom;
  27. }
  28. public int getTop() {
  29. return Math.round(bounds.top);
  30. }
  31. public void draw(Canvas canvas) {
  32. if (!isVisible()) {
  33. return;
  34. }
  35. canvas.drawRect(bounds, fillPaint);
  36. //渲染文本
  37. canvas.drawText("Page " + (index + 1), bounds.centerX(), bounds.centerY(), textPaint); //pagenumber
  38. node.draw(canvas);
  39. //画线
  40. canvas.drawLine(bounds.left, bounds.top, bounds.right, bounds.top, strokePaint);
  41. canvas.drawLine(bounds.left, bounds.bottom, bounds.right, bounds.bottom, strokePaint);
  42. }
  43. private Paint strokePaint() {
  44. final Paint strokePaint = new Paint();
  45. strokePaint.setColor(Color.BLACK);
  46. strokePaint.setStyle(Paint.Style.STROKE);
  47. strokePaint.setStrokeWidth(10);
  48. return strokePaint;
  49. }
  50. private Paint fillPaint() {
  51. final Paint fillPaint = new Paint();
  52. fillPaint.setColor(Color.GRAY);
  53. fillPaint.setStyle(Paint.Style.FILL);
  54. return fillPaint;
  55. }
  56. private TextPaint textPaint() {
  57. final TextPaint paint = new TextPaint();
  58. paint.setColor(Color.BLACK);
  59. paint.setAntiAlias(true);
  60. paint.setTextSize(50);
  61. paint.setTextAlign(Paint.Align.CENTER);
  62. return paint;
  63. }
  64. public float getAspectRatio() {
  65. return aspectRatio;
  66. }
  67. public void setAspectRatio(float aspectRatio) {
  68. if (this.aspectRatio != aspectRatio) {
  69. this.aspectRatio = aspectRatio;
  70. documentView.invalidatePageSizes();
  71. }
  72. }
  73. public boolean isVisible() {
  74. return RectF.intersects(documentView.getViewRect(), bounds);
  75. }
  76. public void setAspectRatio(int width, int height) {
  77. setAspectRatio(width * 1.0f / height);
  78. }
  79. void setBounds(RectF pageBounds) {
  80. bounds = pageBounds;
  81. node.invalidateNodeBounds();
  82. }
  83. public void updateVisibility() {
  84. node.updateVisibility();
  85. }
  86. public void invalidate() {
  87. node.invalidate();
  88. }
  89. }