Changeset 8:16f40c44c354 for android/src/org
- Timestamp:
- 07/08/10 01:14:22 (14 years ago)
- Branch:
- default
- Tags:
- tip
- Convert:
- svn:f819dc9c-ca78-df11-852c-0018fe7759ca/trunk@9
- Location:
- android/src/org/webstrips/android
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
android/src/org/webstrips/android/ComicViewerActivity.java
r2 r8 35 35 import android.widget.ProgressBar; 36 36 import android.widget.RelativeLayout; 37 import android.widget.TextView; 37 38 38 39 public class ComicViewerActivity extends Activity { 39 40 41 private CanvasThread thread; 42 40 43 private StripPane pane; 41 44 45 private TextView title; 46 42 47 private RelativeLayout overlay; 43 48 … … 87 92 } 88 93 progress.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); 94 pane.setFrozen(visible); 89 95 } 90 96 }; 91 97 98 final Handler overlayHandler = new Handler() { 99 public void handleMessage(Message msg) { 100 boolean visible = msg.getData().getBoolean("visible"); 101 overlay.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); 102 } 103 }; 104 105 final Handler comicHandler = new Handler() { 106 public void handleMessage(Message msg) { 107 String t = msg.getData().getString("title"); 108 title.setText(t); 109 } 110 }; 111 92 112 final Handler errorHandler = new Handler() { 93 113 public void handleMessage(Message msg) { … … 246 266 247 267 progress.setVisibility(View.INVISIBLE); 268 269 title = (TextView) findViewById(R.id.view_title); 248 270 } 249 271 … … 253 275 254 276 } 255 277 256 278 @Override 257 279 protected void onPause() { 280 if (thread != null) { 281 boolean retry = true; 282 thread.run = false; 283 while (retry) { 284 try { 285 thread.join(); 286 retry = false; 287 } catch (InterruptedException e) { 288 // we will try it again and again... 289 } 290 } 291 } 292 258 293 super.onPause(); 259 294 try { … … 268 303 expectedStrip = null; 269 304 305 270 306 } 271 307 … … 292 328 0); 293 329 330 thread = new CanvasThread(); 331 thread.run = true; 332 thread.start(); 294 333 } 295 334 … … 483 522 String title = EscapeSequences 484 523 .stripAmpersandSequence(((strip == null) ? "" : strip 485 .getTitle() 486 + " - ")); 524 .getTitle())); 487 525 488 526 setTitle(control.getComic().getComicName() + " - " + title); 489 527 528 Message msg = comicHandler.obtainMessage(); 529 Bundle b = new Bundle(); 530 b.putString("title", title); 531 msg.setData(b); 532 comicHandler.sendMessage(msg); 533 490 534 currentStrip = strip; 491 535 expectedStrip = currentStrip.getIdentifier(); … … 622 666 } 623 667 668 private static final long FRAME_LENGTH = 1000 / 25; 669 670 private static final int OVERLAY_DELAY = 80; 671 672 private int overlayCountdown = OVERLAY_DELAY; 673 674 class CanvasThread extends Thread { 675 676 private boolean run = false; 677 678 @Override 679 public void run() { 680 681 while (run) { 682 683 long miliseconds = System.currentTimeMillis(); 684 685 if (pane != null) 686 pane.tick(); 687 688 if (!dragging && !pane.isEmpty() && !pane.isFrozen()) { 689 if (overlayCountdown > -1) 690 overlayCountdown--; 691 } else { 692 if (overlayCountdown != OVERLAY_DELAY) { 693 Message msg = overlayHandler.obtainMessage(); 694 Bundle b = new Bundle(); 695 b.putBoolean("visible", false); 696 msg.setData(b); 697 overlayHandler.sendMessage(msg); 698 } 699 700 overlayCountdown = OVERLAY_DELAY; 701 } 702 703 if (overlayCountdown == 0) { 704 Message msg = overlayHandler.obtainMessage(); 705 Bundle b = new Bundle(); 706 b.putBoolean("visible", true); 707 msg.setData(b); 708 overlayHandler.sendMessage(msg); 709 } 710 711 try { 712 long sl = Math.max(0, FRAME_LENGTH - Math.abs(System.currentTimeMillis() - miliseconds)); 713 sleep(sl); 714 } catch (InterruptedException e) { 715 break; 716 } 717 718 } 719 720 } 721 722 } 624 723 } -
android/src/org/webstrips/android/StripPane.java
r2 r8 14 14 public class StripPane extends SurfaceView implements SurfaceHolder.Callback { 15 15 16 private CanvasThread thread;17 16 18 17 private boolean dirty = true; 19 18 20 19 private static Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 21 22 private static class StipPaneData { 23 20 21 private static Paint frozenpaint; 22 23 static { 24 25 frozenpaint = new Paint(Paint.ANTI_ALIAS_FLAG); 26 27 frozenpaint.setAlpha(100); 28 29 } 30 31 private boolean frozen = false; 32 33 private static class StipPaneData { 34 24 35 public PointF offset = new PointF(); 25 36 26 37 public FloatTracker zoom = new FloatTracker(1, 1); 27 38 28 39 public Bitmap comicStrip; 29 40 30 41 } 31 42 32 private FloatTracker velocityX = new FloatTracker(0, 0, 0.2f, 0.5f), velocityY = new FloatTracker(0, 0, 0.2f, 0.5f); 33 43 private FloatTracker velocityX = new FloatTracker(0, 0, 0.2f, 0.5f), 44 velocityY = new FloatTracker(0, 0, 0.2f, 0.5f); 45 34 46 private StipPaneData data = new StipPaneData(); 35 47 … … 37 49 super(context, attr); 38 50 getHolder().addCallback(this); 39 thread = new CanvasThread(getHolder());40 41 51 42 52 } 43 53 44 54 private RectF canvasRect = new RectF(); 45 55 46 56 public void setDirty() { 47 57 dirty = true; 48 58 } 49 59 50 60 public void setVelocity(float vx, float vy) { 51 61 52 62 velocityX.setValue(vx); 53 63 velocityY.setValue(vy); 54 64 dirty = true; 55 65 } 56 66 67 public void setFrozen(boolean frozen) { 68 this.frozen = frozen; 69 this.dirty = true; 70 } 71 57 72 public Object getPersistentData() { 58 73 return data; 59 74 } 60 75 61 76 public void setPersistentData(Object o) { 62 77 if (o instanceof StripPane.StipPaneData) { … … 65 80 } 66 81 } 67 82 68 83 @Override 69 84 public void onDraw(Canvas canvas) { … … 71 86 return; 72 87 canvas.drawColor(Color.BLACK); 73 74 canvas.drawBitmap(data.comicStrip, null, canvasRect, paint); 88 89 canvas.drawBitmap(data.comicStrip, null, canvasRect, 90 frozen ? frozenpaint : paint); 75 91 } 76 92 … … 80 96 81 97 data.comicStrip = strip; 82 98 83 99 data.offset.x = 0; 84 100 data.offset.y = 0; … … 86 102 } 87 103 } 88 104 89 105 public Bitmap getComicStrip() { 90 106 return data.comicStrip; 91 107 } 108 109 public boolean isEmpty() { 110 return data.comicStrip == null; 111 } 112 113 public boolean isFrozen() { 114 return frozen; 115 } 92 116 93 117 public void surfaceChanged(SurfaceHolder holder, int format, int width, … … 97 121 98 122 public void surfaceCreated(SurfaceHolder holder) { 99 thread = new CanvasThread(getHolder());100 thread.run = true;101 thread.start();102 123 dirty = true; 124 canDraw = true; 103 125 } 104 126 105 127 public void surfaceDestroyed(SurfaceHolder holder) { 106 boolean retry = true; 107 thread.run = false; 108 while (retry) { 128 canDraw = false; 129 } 130 131 private boolean canDraw = false; 132 133 private static final long FRAME_LENGTH = 1000 / 20; 134 135 public void tick() { 136 137 if (!canDraw) 138 return; 139 140 if (!frozen) 141 dirty |= !data.zoom.onGoal() || !velocityX.onGoal() 142 || !velocityY.onGoal(); 143 144 Canvas c = null; 145 146 if (dirty) { 147 109 148 try { 110 thread.join(); 111 retry = false; 112 } catch (InterruptedException e) { 113 // we will try it again and again... 149 150 data.zoom.track(); 151 152 c = getHolder().lockCanvas(null); 153 synchronized (this) { 154 155 if (data.comicStrip != null) { 156 float width = data.zoom.getValue() 157 * data.comicStrip.getWidth(); 158 float height = data.zoom.getValue() 159 * data.comicStrip.getHeight(); 160 161 if (!frozen) { 162 data.offset.x -= velocityX.track(); 163 data.offset.y -= velocityY.track(); 164 } 165 166 data.offset.x = c.getWidth() > width ? -((c.getWidth() - width) / 2) 167 : Math.min(width - c.getWidth(), Math.max(0, 168 data.offset.x)); 169 170 data.offset.y = c.getHeight() > height ? -((c 171 .getHeight() - height) / 2) : Math.min(height 172 - c.getHeight(), Math.max(0, data.offset.y)); 173 174 canvasRect.left = -data.offset.x; 175 canvasRect.top = -data.offset.y; 176 canvasRect.right = -data.offset.x + width; 177 canvasRect.bottom = -data.offset.y + height; 178 } 179 180 onDraw(c); 181 dirty = false; 182 } 183 184 } finally { 185 186 // do this in a finally so that if an exception is 187 // thrown 188 // during the above, we don't leave the Surface in an 189 // inconsistent state 190 191 if (c != null) { 192 193 getHolder().unlockCanvasAndPost(c); 194 195 } 196 114 197 } 115 198 } 116 } 117 118 private static final long FRAME_LENGTH = 1000 / 20; 119 120 class CanvasThread extends Thread { 121 122 private SurfaceHolder surfaceHolder; 123 124 private boolean run = false; 125 126 public CanvasThread(SurfaceHolder surfaceHolder) { 127 this.surfaceHolder = surfaceHolder; 128 } 129 130 @Override 131 public void run() { 132 133 Canvas c; 134 135 while (run) { 136 137 c = null; 138 139 long miliseconds = System.currentTimeMillis(); 140 141 dirty |= !data.zoom.onGoal() || !velocityX.onGoal() || !velocityY.onGoal(); 142 143 if (dirty) { 144 145 try { 146 147 data.zoom.track(); 148 149 c = surfaceHolder.lockCanvas(null); 150 synchronized (surfaceHolder) { 151 152 if (data.comicStrip != null) { 153 float width = data.zoom.getValue() * data.comicStrip.getWidth(); 154 float height = data.zoom.getValue() * data.comicStrip.getHeight(); 155 156 data.offset.x -= velocityX.track(); 157 data.offset.y -= velocityY.track(); 158 159 data.offset.x = c.getWidth() > width ? -((c.getWidth() - width) / 2) : 160 Math.min(width - c.getWidth(), Math.max(0, data.offset.x)); 161 162 data.offset.y = c.getHeight() > height ? -((c.getHeight() - height) / 2) : 163 Math.min(height - c.getHeight(), Math.max(0, data.offset.y)); 164 165 canvasRect.left = -data.offset.x; 166 canvasRect.top = -data.offset.y; 167 canvasRect.right = -data.offset.x + width; 168 canvasRect.bottom = -data.offset.y + height; 169 } 170 171 172 173 onDraw(c); 174 dirty = false; 175 } 176 177 } finally { 178 179 // do this in a finally so that if an exception is 180 // thrown 181 // during the above, we don't leave the Surface in an 182 // inconsistent state 183 184 if (c != null) { 185 186 surfaceHolder.unlockCanvasAndPost(c); 187 188 } 189 190 } 191 } 192 193 try { 194 sleep(Math.min(FRAME_LENGTH, Math.abs(System.currentTimeMillis() - miliseconds))); 195 } catch (InterruptedException e) { 196 break; 197 } 198 199 } 200 201 } 202 203 } 204 205 206 207 199 200 } 201 208 202 }
Note: See TracChangeset
for help on using the changeset viewer.