Android 游戏开发之View框架
MainActivity.java
[java]
package com.soai.view;
import android.os.Bundle;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//全屏显示
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new MyView(this));
}
} www.zzzyk.com
MyView.java
[java]
package com.soai.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
/**
*
* @author SoAi
*
*/
public class MyView extends View {
private int textX = 20,textY = 20;
public MyView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
//创建一个画笔实例
Paint paint = new Paint();
paint.setColor(Color.RED);
//绘制文本
canvas.drawText("Game", textX, textY, paint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取用户触屏的X轴赋值给文本的X坐标
textX = (int)event.getX();
//获取用户触屏的Y轴赋值给文本的Y坐标
textY = (int)event.getY();
invalidate();
return true;
}
}
MyView需要继承View,实现里面的onDraw(Canvas canvas)方法,Canvas为画布类相当于一张白纸,而Paint为画笔类恰好是画图工具的笔,利用这两个类可以很好的实现游戏中的绘图。
调用invalidate()方法 则重新绘图一下,也就是调用onDraw()方法一次。如果在其它类中调用,则需要调用postInvalidate();这个方法。
补充:移动开发 , Android ,