SurfaceView的简单应用

一如既往的直接上代码:(主要就两个文件)

MainActivity:

package com.example.game2;


import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceView;


public class MainActivity extends Activity {


private MyView mSurfaceView;
private int x;
private int y;
private int miDTX;
private int miDTY;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSurfaceView=new MyView(this);
setContentView(mSurfaceView);

}
//触笔事件
public boolean onTouchEvent(MotionEvent event)
{
return true;
}

// 按键按下事件
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return true;
}

public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch (keyCode)
{
//方向键
case KeyEvent.KEYCODE_DPAD_LEFT:
if(mSurfaceView.x>=0)
{
mSurfaceView.x-=3;
}
break;
//方向键
case KeyEvent.KEYCODE_DPAD_RIGHT:
if(mSurfaceView.x<220)
{
mSurfaceView.x+=3;
}
break;

//上方向键
case KeyEvent.KEYCODE_DPAD_UP:
if(mSurfaceView.y>=0)
{
mSurfaceView.y-=3;
}
break;
//下方向键
case KeyEvent.KEYCODE_DPAD_DOWN:
if(mSurfaceView.y<200)
{
mSurfaceView.y+=3;
}
break;
}
return false;
}
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
{
return true;
}
}


MyView:

package com.example.game2;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.SurfaceView;


public class MyView extends SurfaceView implements Callback, Runnable {
int[]ID={
//R.drawable.p00,
R.drawable.p01,
R.drawable.p02,
R.drawable.p03,
R.drawable.p04,
R.drawable.p05,
R.drawable.p06,
R.drawable.p07,
R.drawable.p08,
};
int[]ImageID={
//R.drawable.a0000,
R.drawable.a0001,
R.drawable.a0002,
R.drawable.a0003,
R.drawable.a0004,
R.drawable.a0005,
R.drawable.a0006,
R.drawable.a0007,

};


private SurfaceHolder msfh;
private boolean flag;
private Paint paint;
private Canvas cv;
private boolean count = true;
float x = 100;
float y = 50;
private Thread th;
private int i=0;
private int mICount;

private Bitmap bmp;


public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
msfh = this.getHolder();
msfh.addCallback(this);


paint = new Paint();
paint.setColor(Color.RED);
}


@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub


}


@Override
// surfaceView创建时激发
public void surfaceCreated(SurfaceHolder arg0) {
// 开启线程
th = new Thread(this);
th.start();
flag = true;


}


@Override
// surfaceview关闭时激发
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
// 停止循环
flag = false;
}


// 绘图
public void draw() {
cv = msfh.lockCanvas();

/*if (count) {
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.p56);
count = false;
} else {
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.p57);
count = true;
}*/
cv.drawColor(Color.WHITE);
if ( mICount < 100 )
{
mICount++;
}

paint.setColor(Color.RED);

cv.drawText("装在进度:"+mICount+"%......", 100, 197, paint);
i++;
//if(i==ID.length)
if(i==ImageID.length)
{
i=0;
}
//bmp = BitmapFactory.decodeResource(getResources(),ID[i]);
bmp = BitmapFactory.decodeResource(getResources(), ImageID[i]);

cv.drawBitmap(bmp, x, y, paint);
//cv.drawText("this is games", x, y, paint);
msfh.unlockCanvasAndPost(cv);

}


@Override
/* 触屏响应事件onTouchEvent */


public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
/* 取得x的值 */
x = event.getX();
y = event.getY();
// invalidate();//刷新试图自动调用onDraw//不可在子线程中调用
postInvalidate();//刷新试图自动调用onDraw//可在子线程中调用

return super.onTouchEvent(event);
}

// 绘图线程
public void run() 
{
while (flag) 
{
try 
{
draw();
Thread.sleep(5);

catch (Exception e) 
{
// TODO: handle exception
}
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值