Back to project page AdoreLib.
The source code is released under:
MIT License
If you think the Android project AdoreLib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.ylinval.adore.adorelib; /*from w ww . j a v a 2 s. c om*/ import android.content.Context; import android.os.Handler; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.widget.Scroller; /** * Created by jdourlens on 5/9/14. */ public class AdoreView extends SurfaceView implements SurfaceHolder.Callback { //used to keep track of time between updates and amount of time to sleep for long lastUpdate = 0; long sleepTime = 0; //Game engine AdoreFragment fragment; //objects which house info about the screen SurfaceHolder surfaceHolder; Context context; int mMaxScrollX = 400; int mMaxScrollY = 400; GestureDetector mGD = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // beware, it can scroll to infinity fragment.onScroll(e1, e2, distanceX, distanceY); return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float vX, float vY) { mScroller.fling(getScrollX(), getScrollY(), -(int) vX, -(int) vY, 0, (int) mMaxScrollX, 0, (int) mMaxScrollY); invalidate(); // don't remember if it's needed return true; } @Override public boolean onDown(MotionEvent e) { if (!mScroller.isFinished()) { // is flinging mScroller.forceFinished(true); // to stop flinging on touch } return true; // else won't work } @Override public boolean onSingleTapConfirmed(MotionEvent e) { fragment.onTap(e, e.getX(), e.getY()); return (true); } } ); //our Thread class which houses the game loop private AdoreLoop thread; private Scroller mScroller; //class constructors public AdoreView(Context contextS, AttributeSet attrs, int defStyle) { super(contextS, attrs, defStyle); context = contextS; } public AdoreView(Context contextS, AttributeSet attrs) { super(contextS, attrs); context = contextS; } //initialization code public void InitView(AdoreFragment fragment) { //initialize our screen holder SurfaceHolder holder = getHolder(); holder.addCallback(this); this.fragment = fragment; //initialize our Thread class. A call will be made to start it later thread = new AdoreLoop(holder, context, new Handler(), fragment); setFocusable(true); mScroller = new Scroller(context); } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceDestroyed(SurfaceHolder arg0) { boolean retry = true; //code to end gameloop thread.state = AdoreLoop.PAUSED; while (retry) { try { //code to kill Thread thread.join(); retry = false; } catch (InterruptedException e) { } } } @Override public void surfaceCreated(SurfaceHolder arg0) { if (thread.state == AdoreLoop.PAUSED) { //When game is opened again in the Android OS thread = new AdoreLoop(getHolder(), context, new Handler(), this.fragment); thread.start(); } else { //creating the game Thread for the first time thread.start(); } } @Override public boolean onTouchEvent(MotionEvent event) { return mGD.onTouchEvent(event); } }