Game Development http://blog.csdn.net/yexing/article/details/6139135
Basic Description (NOTE: The APIs are not included in Android SDK)
xxx
Classes Summary
Paint
Typeface Load fonts: Typeface font = Typeface.createFromAsset(context.getAssets(), "font.ttf");
Then you can set it to Paint with Paint.setTypeface.
Canvas
View
SurfaceView Provides a dedicated drawing surface embedded inside of a view hierarchy. One of the purposes of this class is to provide a surface in which a
secondary thread can render into the screen.
Generally, SurfaceView is used to play media (video), and display images from Camera.
xxx
Canvas
save & restore Saves the current states of Canvas onto a private stack, then you can execute operation to translate, scale, rotate, skew, concat or clipRect, clipPath
will all operate as usual. After restore()
is invoked, the previous states of Canvas is restored. save() and restore() is invoked in pairs.
xxx
View (Details: http://developer.android.com/reference/android/view/View.html) To use costomzied view, invoke setContentView(new YourCustomizedView()) in YourActivity.onCreate.
Override onKeyDown @Override public boolean onKeyDown(int keyCode, KeyEvent event)
{
//Process key event
....
postInvalidate(); //Post a message to invalidate
....
super.onKeyDown(xxxxxx);
}
xxx
Handle Touch Event You must define the region of all components on screen in advance, and then process touch event according the position.
@Override public boolean onTouchEvent(MotionEvent event) { if (arg0.getAction() == MotionEvent.ACTION_DOWN) { int ax = (int) event.getX(); int ay = (int) event.getY();
//RECT_BTN_xxx are predefined rect of UP/DOWN/LEFT/RIGHT key on screen, they are type of Rect. if (RECT_BTN_UP.contains(ax, ay)) { y -= 10; } else if (RECT_BTN_DOWN.contains(ax, ay)) { y += 10; } else if (RECT_BTN_LEFT.contains(ax, ay)) { x -= 10; } else if (RECT_BTN_RIGHT.contains(ax, ay)) { x += 10; }
postInvalidate(); //Post a message to invalidate }
super.onTouchEvent(xxx); }
SurfaceView (Details: http://developer.android.com/reference/android/view/SurfaceView.html) As the name gives away, the SurfaceView class is a View that handles a Surface, another class
of the Android API. A Surface is an abstraction of a raw buffer that is
used by the screen compositor for rendering that specific View. The
screen compositor is the mastermind behind all rendering on Android, and is ultimately responsible for pushing all pixels to the GPU. Surface is a more direct way to render things to the screen.
Sample:
public class YourSurfaceView extends SurfaceView implements Callback { public static final String tag = "GameView";
DrawThread mDrawThread;
public YourSurfaceView (Context context) { super(context);