android游戏开发自学笔记2 基础知识

 最近迫于生存压力,不得不给人兼职打工。故在博文中加了个求点击的链接。麻烦有时间的博友们帮我点击一下。没时间的不用勉强啊。不过请放心,我是做技术的,肯定链接没病毒,就是我打工的淘宝店铺。嘻嘻。http://shop108130013.taobao.com。谢谢捧场。以后就每周写篇原创的技术博客回报大家,实在是迫于生计,无所不用其极。请谅解。


昨晚到刚才写了自学笔记1,现在给笔记2起个头,马上就要去忙工作了。起个头的原因是,提醒自己必须不断学习。不要有1无2了。嘿嘿。

今天工作忙好了,就继续学习。
第四章的开始,作者介绍如何创建一个activity等。这些都是基础的android应用知识,略过。

输入设备:touchscreen, keyboard 和 accelerometer.

 Touch Events:单点触屏和多点触屏;key events这些都是基础知识。略过吧。加速器状态的读取倒是平台里我接触很少的部分,但是看了一下作者介绍,也是调用接口的实现。也略过。需要时候,再回来补补知识。

 File Handing: 读取Assets,访问外部存储。

基本的图像编程包含以下几点:

使用Wake Locks, 全屏处理,UI线程的Render机制,屏幕处理,位图的使用,文字的使用。

 

位图处理简单实例代码:

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class BitmapTest extends Activity {
    
    class RenderView extends View {
        Bitmap bob565;
        Bitmap bob4444;
        Rect dst = new Rect();
        
        public RenderView(Context context) {
            super(context);
            try {
                AssetManager assetManager = context.getAssets();
                InputStream inputStream = assetManager.open("bobrgb888.png");
                bob565 = BitmapFactory.decodeStream(inputStream);
                inputStream.close();
                Log.d("BitmapText","bobrgb888.png format: " + bob565.getConfig());
                inputStream = assetManager.open("bobargb8888.png");
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_4444;
                bob4444 = BitmapFactory.decodeStream(inputStream, null, options);
                inputStream.close();
                Log.d("BitmapText","bobargb8888.png format: " + bob4444.getConfig());
            } catch (IOException e) {
                // silently ignored, bad coder monkey, baaad!
            } finally {
                // we should really close our input streams here.
            }
        }
        
        protected void onDraw(Canvas canvas) {
            dst.set(50, 50, 350, 350);
            canvas.drawBitmap(bob565, null, dst, null);
            canvas.drawBitmap(bob4444, 100, 100, null);
            invalidate();
        }
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new RenderView(this));
    }
}

文字处理相关代码:

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class FontTest extends Activity {
    class RenderView extends View {
        Paint paint;
        Typeface font;
        Rect bounds = new Rect();
        
        public RenderView(Context context) {
            super(context);
            paint = new Paint();
            font = Typeface.createFromAsset(context.getAssets(), "font.ttf");
        }

        protected void onDraw(Canvas canvas) {
            paint.setColor(Color.YELLOW);
            paint.setTypeface(font);
            paint.setTextSize(28);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("This is a test!", canvas.getWidth() / 2, 100,paint);
            String text = "This is another test o_O";
            paint.setColor(Color.WHITE);
            paint.setTextSize(18);
            paint.setTextAlign(Paint.Align.LEFT);
            paint.getTextBounds(text, 0, text.length(), bounds);
            canvas.drawText(text, canvas.getWidth() - bounds.width(), 140,paint);
            invalidate();
        }
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new RenderView(this));
    }
}


SurfaceView的使用:

Motivation:

SurfaceHolder and Locking:SurfaceHolder.lockCanvas()和SurfaceHolder.unlockAndPost()

Surface Creation and Validity:surfaceHolder.getSurface().isValid() 检查surface是否被创建。

The SurfaceViewTest Activity

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class SurfaceViewTest extends Activity {
    FastRenderView renderView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        renderView = new FastRenderView(this);
        setContentView(renderView);
    }
    
    protected void onResume() {
        super.onResume();
        renderView.resume();
    }
    
    protected void onPause() {
        super.onPause();
        renderView.pause();
    }
    
    class FastRenderView extends SurfaceView implements Runnable {
        Thread renderThread = null;
        SurfaceHolder holder;
        volatile boolean running = false;
        
        public FastRenderView(Context context) {
            super(context);
            holder = getHolder();
        }
        
        public void resume() {
            running = true;
            renderThread = new Thread(this);
            renderThread.start();
        }
    
        public void run() {
            while(running) {
                if(!holder.getSurface().isValid()) continue;
                Canvas canvas = holder.lockCanvas();
                canvas.drawRGB(255, 0, 0);
                holder.unlockCanvasAndPost(canvas);
            }
        }
    
        public void pause() {
            running = false;
            while(true) {
                try {
                    renderThread.join();
                } catch (InterruptedException e) {
                    // retry
                }
            }
        }
    
    }
}




你可能感兴趣的:(android游戏开发自学笔记2 基础知识)