PageRenderTime 13ms CodeModel.GetById 9ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Research/Example projects/Android OGL/ModelRenderer/src/edu/union/android/ViewAnimator.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 51 lines | 43 code | 8 blank | 0 comment | 5 complexity | af3c308837860a611a3ace8327c664ba MD5 | raw file
 1package edu.union.android;
 2
 3import android.os.Handler;
 4import android.os.Message;
 5import android.os.SystemClock;
 6import android.view.View;
 7
 8public class ViewAnimator extends Handler {
 9	boolean running;
10	View view;
11	long nextTime;
12	long step;
13	
14	public static final int NEXT = 0;
15	
16	public ViewAnimator(View view) {
17		this(view, 30);
18	}
19	
20	public ViewAnimator(View view, int fps) {
21		this.running = false;
22		this.view = view;
23		this.step = 1000/fps;
24	}
25	
26	public void start() {
27		if (!running) {
28			running = true;
29			Message msg = obtainMessage(NEXT);
30			sendMessageAtTime(msg, SystemClock.uptimeMillis());
31			nextTime = SystemClock.uptimeMillis()+step;
32		}
33	}
34	
35	public void stop() {
36		running = false;
37	}
38	
39	public void handleMessage(Message msg) {
40		if (running && msg.what == NEXT) {
41			view.invalidate();
42			msg = obtainMessage(NEXT);
43			long current = SystemClock.uptimeMillis();
44			if (nextTime < current) {
45				nextTime = nextTime + step;
46			}
47			sendMessageAtTime(msg, nextTime);
48			nextTime += step;
49		}
50	}
51}