Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2025438
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: 嵌入式

2011-08-04 22:16:33

  1. Docs
    • Game Development
      http://blog.csdn.net/yexing/article/details/6139135
    • Basic Description
      (NOTE: The APIs are not included in Android SDK)

    • xxx
  2. 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
  3. 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
  4. View
    (Details: http://developer.android.com/reference/android/view/View.html)
    To use costomzied view, invoke setContentView(new YourCustomizedView()) in YourActivity.onCreate.

    With the following steps to customzie view
    • Define a new class derived from View
    • Override onDraw to draw contents
      @Override
      protecte void onDraw(Canvas canvas)
      {
          super.onDraw(canvas);

          canvas.drawBitmap(bmp, x1, y1, paint1);
          canvas.drawText("your text", x2, y2, paint2);
      }
    • Handle Key Event
      • Invoke setFocusable(true) in construct method.
      • 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);
      }
  5. 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);

            //Get SurfaceHolder
            SurfaceHolder surfaceHolder = getHolder();
           
            //Set callback
            surfaceHolder.addCallback(this);
           
            //Create draw thread
            mDrawThread = new GameThread(surfaceHolder);
        }
       
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
        {
        }
       
        public void surfaceCreated(SurfaceHolder arg0)
        {
            //Start draw thread
            mDrawThread.start();
        }
       
        public void surfaceDestroyed(SurfaceHolder arg0)
        {
            //Will stop draw thread
            mDrawThread.run = false;
        }
       

        class DrawThread extends Thread
        {
            SurfaceHolder mSurfaceHolder;

            boolean run = true;
           
            public DrawThread(SurfaceHolder surfaceHolder)
            {
                mSurfaceHolder = surfaceHolder;
            }
           
            @Override
            public void run()
            {
                // TODO Auto-generated method stub
                int i = 0;
                while(run)
                {
                    Canvas c = null;
                    try
                    {
                        synchronized (mSurfaceHolder)
                        {
                            //Draw a number every second
                            c = surfaceHolder.lockCanvas();
                            c.drawARGB(255, 255, 255, 255);
                            c.drawText("" + i++, 100, 100, new Paint());
                            Thread.sleep(1000);
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                    finally
                    {
                        if (c != null)
                        {
                            mSurfaceHolder.unlockCanvasAndPost(c);
                        }
                    }
                }
            }
        }
    }

  6. xxx
阅读(1588) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~