編輯:Android開發教程
當我們需要開發一個復雜游戲的時候,而且對程序的執行效率要求很高時,View類就不能滿足需求了,這時必須用 SurfaceView類進行開發。
例如,對速度要求很高的游戲時,View類就不能滿足需求了,這時必須使用SurfaceView類進 行開發。例如,對速度要求很高的游戲,可以使用雙緩沖來顯示。游戲中的背景、人物、動畫等都需要繪制在一個畫布(Canvas) 上,而SurfaceView可以直接訪問一個畫布,SurfaceView 是提供給需要直接畫像素而不是使用窗體部件的應用使用的。 每個 Surface創建一個Canvas對象(但屬性時常改變),用來管理View和Surface上的繪圖操作。
具體示例:
Activity
public class Activity01 extends Activity { GameSurfaceView gameView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); gameView = new GameSurfaceView(this); setContentView(gameView); } }
GameSurfaceView
public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{ private Activity01 mActivity = null; private SurfaceHolder mHolder = null; private Thread mThread = null;; private boolean mLoop = false; private Canvas mCanvas = null; private Paint mPaint = null; private long drawStartTime = 0, drawCostTime = 0, drawTotalTime = 30; private float x, y; private int screenWidth = 480, screenHeight = 800; public GameSurfaceView(Context context) { super(context); mActivity = (Activity01)context; mHolder = this.getHolder(); mHolder.addCallback(this); setFocusable(true); } @Override public void surfaceCreated(SurfaceHolder holder) { mPaint = new Paint(); mPaint.setColor(Color.CYAN); mLoop = true; new Thread(this).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { screenWidth = width; screenHeight = height; x = screenWidth / 2; y = screenHeight / 2; } @Override public void surfaceDestroyed(SurfaceHolder holder) { mLoop = false; } @Override public void run() { while(mLoop){ drawStartTime = SystemClock.uptimeMillis(); synchronized (mHolder) { try { mCanvas = mHolder.lockCanvas(); onDraw(); // repaint } catch (Exception e) { e.printStackTrace(); } finally { if(mCanvas != null){ mHolder.unlockCanvasAndPost(mCanvas); } } } drawCostTime = SystemClock.uptimeMillis() - drawStartTime; Log.i("drawCostTime", "drawCostTime = " + drawCostTime); try { if(drawCostTime < drawTotalTime){ Thread.sleep(drawTotalTime - drawCostTime); // sleep elapse time } } catch (Exception e) { e.printStackTrace(); } } } private void onDraw(){ if(mHolder == null && mPaint == null){ return; } mPaint.setAntiAlias(true); mPaint.setAlpha(100); mPaint.setColor(Color.CYAN); // paint background color mCanvas.drawRect(0, 0, screenWidth, screenHeight, mPaint); mPaint.setColor(Color.BLUE); // paint foreground color mCanvas.drawCircle(x, y, 50, mPaint); } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: this.x = event.getX(); this.y = event.getY(); break; case MotionEvent.ACTION_MOVE: this.x = event.getX(); this.y = event.getY(); break; case MotionEvent.ACTION_UP: this.x = event.getX(); this.y = event.getY(); break; default: this.x = event.getX(); this.y = event.getY(); break; } return true; // true then event can be implemented again } @Override public boolean onKeyDown(int keyCode, KeyEvent event){ super.onKeyDown(keyCode, event); if(keyCode == KeyEvent.ACTION_DOWN){ this.mActivity.finish(); } return true; } }
通過手機的通知系統,可以將應用程序的一些重要消息告知給用戶。流暢、 舒適、友好的應用程序離不開精心設計的消息提醒機制。但是並不是所有的通知 都是用戶想看的,否則只會給用戶
在例子Android簡明開發教程十七:Dialog 顯示圖像 中我們留了一個例子DrawMap()沒有實現,這個例子顯示在線地圖,目前大部分地圖服務器都是將地圖以圖片存儲
其實實現ListView過濾功能最方便的便是使用ArrayAdapter,裡面自帶的 getFilter()方法能很方便的實現此功能,但是在實際的開發中,一般都是繼承於
前面我也寫了有幾個自定義進度的控件,那麼,今天,我再加一個控件,原理跟前面講的差不多,先看看效果:這個是一個以弧線為依托的進度控件,主要包括了兩個圓弧、一個圓、一個文本。