这个实例的本意展示j2me的字体,和滚屏的实现。我只实现了android自带的三种字体.
下面给出效果图:
下面给出MainView的代码
package com.wjh.demon_6;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
public class MainView extends SurfaceView implements Callback,Runnable{
Thread gameThread = null;
boolean isGame = true;
SurfaceHolder holder = null;
//游戏相关
int m_nPosY = 0;
Paint[] paints = null;
Paint backPaint = null;
public MainView(Context context) {
super(context);
// TODO Auto-generated constructor stub
setFocusable(true);
getHolder().addCallback(this);
holder = this.getHolder();
// 设定字体
paints = new Paint[3];
for(int i=0;i<3;i++)
{
paints[i] = new Paint();
}
paints[0].setColor(Color.RED);
paints[0].setTypeface(Typeface.MONOSPACE);
paints[0].setTextSize(15);
paints[1].setColor(Color.GREEN);
paints[1].setTypeface(Typeface.SANS_SERIF);
paints[1].setTextSize(20);
paints[2].setColor(Color.BLUE);
paints[2].setTypeface(Typeface.SERIF);
paints[2].setTextSize(25);
//创建背景画笔
backPaint = new Paint();
backPaint.setColor(Color.BLACK);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(isGame)
{
input();
logic();
doDraw();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//开始游戏主线程
public void start()
{
if(gameThread == null)
{
gameThread = new Thread(this);
gameThread.start();
}
}
//停止游戏主线程
public void stop()
{
isGame = false;
if(gameThread != null)
{
try {
gameThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//输入判断
public void input()
{
}
//逻辑判断
public void logic()
{
//不断更改m_nPosY的值,实现文字滚屏
m_nPosY ++;
//如果文字已滚至屏幕最下方,则重新设置文字的纵坐标
if( m_nPosY > getHeight() )
m_nPosY = 0;
}
public void doDraw()
{
Canvas c = null;
try
{
c = holder.lockCanvas();
synchronized (holder) {
paint(c);
}
}finally{
if(c != null)
{
holder.unlockCanvasAndPost(c);
}
}
}
//画图
public void paint(Canvas canvas)
{
//用黑色清屏
canvas.drawRect(0, 0, getWidth(), getHeight(), backPaint);
//设置当前字体,并以不同字体输出“2008年奥运会”几个字
for(int i=0;i<3;i++)
{
canvas.drawText("2008年奥运会", 20+30*i,m_nPosY-(40-25*i), paints[i]);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
stop();
}
}
apk文件包(修改后缀为apk):Demon_6.zip
源文件包:Demon_6.rar