android SurfaceView使用例子

來源:互聯網
上載者:User

做遊戲一般用SurfaceView

[java] 
package com.himi;  
import android.content.Context;  
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;  
import android.view.animation.Animation;  
/** 
* @author Himi 
*/  
public class MySurfaceView extends SurfaceView implements Callback, Runnable {// 備忘1  
private SurfaceHolder sfh;  
private Thread th;  
private Canvas canvas;  
private Paint paint;  
private int ScreenW, ScreenH;  
public MySurfaceView(Context context) {  
super(context);  
th = new Thread(this);  
sfh = this.getHolder();  
sfh.addCallback(this); // 備忘1  
paint = new Paint();  
paint.setAntiAlias(true);  
paint.setColor(Color.RED);  
this.setKeepScreenOn(true);// 保持螢幕常亮  
}  
@Override  
public void startAnimation(Animation animation) {  
super.startAnimation(animation);  
}  
public void surfaceCreated(SurfaceHolder holder) {  
ScreenW = this.getWidth();// 備忘2  
ScreenH = this.getHeight();  
th.start();  
}  
private void draw() {  
try {  
canvas = sfh.lockCanvas(); // 得到一個canvas執行個體  
canvas.drawColor(Color.WHITE);// 刷屏  
canvas.drawText("Himi", 100, 100, paint);// 畫文字文本  
canvas.drawText("這就是簡單的一個遊戲架構", 100, 130, paint);  
sfh.unlockCanvasAndPost(canvas); // 將畫好的畫布提交  
} catch (Exception ex) {  
} finally { // 備忘3  
if (canvas != null)  
sfh.unlockCanvasAndPost(canvas);  
}  
}  
public void run() {  
while (true) {  
draw();  
try {  
Thread.sleep(100);  
} catch (InterruptedException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}  
}  
}  
public void surfaceChanged(SurfaceHolder holder, int format, int width,  
int height) {  
}  
public void surfaceDestroyed(SurfaceHolder holder) {  
// TODO Auto-generated method stub  
}  
}  

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.