当前位置: 编程技术>移动开发
本页文章导读:
▪范例8-《很幼稚的‘小’鸡快跑》 实例8--《很幼稚的‘小’鸡快跑》
实例8--《小鸡快跑》,原书用的是java me 提供的sprite,我使用的我自己写的androidSprite。没什么好说的,很幼稚,但是还是要发,我要实现自己的承诺。下面.........
▪ 应用OCUnit做单元测试 使用OCUnit做单元测试
具体步骤:
http://developer.apple.com/tools/unittest.html
但是步骤说明中少了一点,需要在新建的test target中的Run Script添加以下Script:
# Run the unit tests in this test bundle.
"${SYSTEM_.........
▪ 下传mp3到服务器 上传mp3到服务器
private void doFileUpload(){
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String existingFileNam.........
[1]范例8-《很幼稚的‘小’鸡快跑》
来源: 互联网 发布时间: 2014-02-18
实例8--《很幼稚的‘小’鸡快跑》
呵呵,有用就好。。。
实例8--《小鸡快跑》,原书用的是java me 提供的sprite,我使用的我自己写的androidSprite。没什么好说的,很幼稚,但是还是要发,我要实现自己的承诺。下面给出效果图:
mainView.java
package com.wjh.demon_7; 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.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; //具体游戏相关 public AndroidSprite m_CockSprite; //小鸡精灵 public Bitmap m_BackImg; //背景图像 //画笔 public Paint backPaint = null; public Paint forePaint = null; public MainView(Context context) { super(context); // TODO Auto-generated constructor stub setFocusable(true); getHolder().addCallback(this); holder = this.getHolder(); // 具体游戏相关代码 m_BackImg = BitmapFactory.decodeResource(getResources(), R.drawable.back); Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.cock); //创建小鸡精灵 m_CockSprite = new AndroidSprite( image, 95, 85 ); //设置精灵当前显示的“帧”编号 m_CockSprite.setFrame( 0 ); } @Override public void run() { // TODO Auto-generated method stub while(isGame) { input(); logic(); doDraw(); try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //开始游戏主线程 public void start() { //getWidth与getHeight可分别获取屏幕的宽度和高度 int nPosX = getWidth(); int nPosY = getHeight() - 90; //设置精灵左上角的位置 m_CockSprite.setPosition( nPosX , nPosY ); // 新建画笔 backPaint = new Paint(); backPaint.setColor(Color.BLACK); forePaint = new Paint(); forePaint.setColor(Color.WHITE); m_BackImg = Bitmap.createScaledBitmap(m_BackImg, getWidth(),getHeight(), false); 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() { int n = m_CockSprite.getFrame(); n ++; //getFrameSequenceLength可获取精灵图像中“帧”的个数 if( n >= m_CockSprite.getFrameSequenceLength() ) n = 0; m_CockSprite.setFrame(n); //设置精灵的位置,如果精灵超出屏幕左侧,则让精灵从右侧重新出现 int nPosX = m_CockSprite.getX(); int nPosY = m_CockSprite.getY(); nPosX -= 6; if( nPosX < -95 ) nPosX = getWidth(); m_CockSprite.setPosition( nPosX, nPosY ); } 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); canvas.drawBitmap(m_BackImg, 0, 0,forePaint); //显示精灵的当前“帧” m_CockSprite.paint(canvas,forePaint); } @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_7.zip
源代码:Demon_7.rar
1 楼
mengqingshan
2011-06-08
很有启发意义 学习了
2 楼
一口三个汉堡
2011-06-08
mengqingshan 写道
很有启发意义 学习了
呵呵,有用就好。。。
[2] 应用OCUnit做单元测试
来源: 互联网 发布时间: 2014-02-18
使用OCUnit做单元测试
具体步骤:
http://developer.apple.com/tools/unittest.html
但是步骤说明中少了一点,需要在新建的test target中的Run Script添加以下Script:
# Run the unit tests in this test bundle.
"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests"
[3] 下传mp3到服务器
来源: 互联网 发布时间: 2014-02-18
上传mp3到服务器
private void doFileUpload(){ HttpURLConnection conn = null; DataOutputStream dos = null; DataInputStream inStream = null; String existingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mypic.png"; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1*1024*1024; String responseFromServer = ""; String urlString = "http://mywebsite.com/directory/upload.php"; try { FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) ); URL url = new URL(/blog_article/urlString/index.html); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); dos = new DataOutputStream( conn.getOutputStream() ); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); Log.e("Debug","File is written"); fileInputStream.close(); dos.flush(); dosbackground-color: transparent; margin: 0px; color: #000000; font-size: 14px; vertical-align:
最新技术文章: