@@ -81,6 +81,10 @@ static float mag(float x, float y) {
8181 return (float ) Math .sqrt (x *x +y *y );
8282 }
8383
84+ static float clamp (float x , float a , float b ) {
85+ return ((x <a )?a :((x >b )?b :x ));
86+ }
87+
8488 static float dot (float x1 , float y1 , float x2 , float y2 ) {
8589 return x1 *x2 +y1 +y2 ;
8690 }
@@ -149,6 +153,7 @@ public class Bean extends ImageView {
149153 public boolean grabbed ;
150154 public float grabx , graby ;
151155 public long grabtime ;
156+ private float grabx_offset , graby_offset ;
152157
153158 public Bean (Context context , AttributeSet as ) {
154159 super (context , as );
@@ -236,17 +241,20 @@ public boolean onTouchEvent(MotionEvent e) {
236241 switch (e .getAction ()) {
237242 case MotionEvent .ACTION_DOWN :
238243 grabbed = true ;
244+ grabx_offset = e .getRawX () - x ;
245+ graby_offset = e .getRawY () - y ;
239246 va = 0 ;
240247 // fall
241248 case MotionEvent .ACTION_MOVE :
242- grabx = e .getRawX ();
243- graby = e .getRawY ();
249+ grabx = e .getRawX () - grabx_offset ;
250+ graby = e .getRawY () - graby_offset ;
244251 grabtime = e .getEventTime ();
245252 break ;
246253 case MotionEvent .ACTION_CANCEL :
247254 case MotionEvent .ACTION_UP :
248255 grabbed = false ;
249- va = randfrange (-5 ,5 );
256+ float a = randsign () * clamp (mag (vx , vy ) * 0.33f , 0 , 1080f );
257+ va = randfrange (a *0.5f , a );
250258 break ;
251259 }
252260 return true ;
@@ -308,47 +316,6 @@ public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime)
308316 if (!(v2 instanceof Bean )) continue ;
309317 Bean nv2 = (Bean ) v2 ;
310318 final float overlap = nv .overlap (nv2 );
311- if (false && overlap < 0 ) {
312- // angle pointing from nv2 to nv
313- final float dx = nv .x - nv2 .x ;
314- final float dy = nv .y - nv2 .y ;
315- final float ang = (float ) Math .atan2 (dx , dy );
316-
317- if (false ) {
318- nv .vx -= Math .cos (ang ) * overlap * 0.5f ;
319- nv .vy -= Math .sin (ang ) * overlap * 0.5f ;
320- nv2 .vx += Math .cos (ang ) * overlap * 0.5f ;
321- nv2 .vy += Math .sin (ang ) * overlap * 0.5f ;
322- }
323-
324-
325- // first, move them apart
326- nv .x -= Math .cos (ang ) * overlap /2 ;
327- nv .y -= Math .sin (ang ) * overlap /2 ;
328- nv2 .x += Math .cos (ang ) * overlap /2 ;
329- nv2 .y += Math .sin (ang ) * overlap /2 ;
330-
331- // next, figure out velocities
332- final float sap = 0f ; // randfrange(0,0.25f);
333-
334- final float mag1 = mag (nv .vx , nv .vy ) * (1f -sap );
335- final float mag2 = mag (nv2 .vx , nv2 .vy ) * (1f -sap );
336-
337-
338- // hacky way to transfer "momentum"
339- nv .vx = mag2 * (float )Math .cos (ang );
340- nv .vy = mag2 * (float )Math .sin (ang );
341- nv2 .vx = -mag1 * (float )Math .cos (ang );
342- nv2 .vy = -mag1 * (float )Math .sin (ang );
343-
344- final float totalva = nv .va + nv2 .va ;
345- final float frac = randfrange (0.25f ,0.75f );
346- nv .va = totalva * frac ;
347- nv2 .va = totalva * (1f -frac );
348- // nv.va += randfrange(-20,20);
349- // nv2.va += randfrange(-20,20);
350-
351- }
352319 }
353320
354321 nv .setRotation (nv .a );
@@ -375,17 +342,28 @@ protected void onSizeChanged (int w, int h, int oldw, int oldh) {
375342 boardWidth = w ;
376343 boardHeight = h ;
377344// android.util.Log.d("Nyandroid", "resized: " + w + "x" + h);
378- post (new Runnable () { public void run () {
379- reset ();
345+ }
346+
347+ public void startAnimation () {
348+ stopAnimation ();
349+ if (mAnim == null ) {
350+ post (new Runnable () { public void run () {
351+ reset ();
352+ startAnimation ();
353+ } });
354+ } else {
380355 mAnim .start ();
381- } });
356+ }
382357 }
383358
359+ public void stopAnimation () {
360+ if (mAnim != null ) mAnim .cancel ();
361+ }
384362
385363 @ Override
386364 protected void onDetachedFromWindow () {
387365 super .onDetachedFromWindow ();
388- mAnim . cancel ();
366+ stopAnimation ();
389367 }
390368
391369 @ Override
@@ -428,12 +406,19 @@ public void onStart() {
428406 WindowManager .LayoutParams .FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
429407 | WindowManager .LayoutParams .FLAG_SHOW_WHEN_LOCKED
430408 );
409+ mBoard = new Board (this , null );
410+ setContentView (mBoard );
411+ }
412+
413+ @ Override
414+ public void onPause () {
415+ super .onPause ();
416+ mBoard .stopAnimation ();
431417 }
432418
433419 @ Override
434420 public void onResume () {
435421 super .onResume ();
436- mBoard = new Board (this , null );
437- setContentView (mBoard );
422+ mBoard .startAnimation ();
438423 }
439424}
0 commit comments