編輯:關於android開發
MySurfaceView類:
package com.fm; 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.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.SurfaceHolder.Callback; public class MySurfaceView extends SurfaceView implements Callback, Runnable { private SurfaceHolder sfh; private Paint paint; private Thread th; private boolean flag; private Canvas canvas; private int screenW, screenH; //首先聲明十個容量的位圖數組 private Bitmap fishBmp[] = new Bitmap[10]; //記錄當前播放幀 private int currentFrame; // /** * SurfaceView初始化函數 */ public MySurfaceView(Context context) { super(context); sfh = this.getHolder(); sfh.addCallback(this); paint = new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); setFocusable(true); //將每張小魚幀圖生成位圖存入小魚幀數組中 for (int i = 0; i < fishBmp.length; i++) { fishBmp[i] = BitmapFactory.decodeResource(this.getResources(), R.drawable.fish0+ i); } } /** * SurfaceView視圖創建,響應此函數 */ @Override public void surfaceCreated(SurfaceHolder holder) { screenW = this.getWidth(); screenH = this.getHeight(); flag = true; //實例線程 th = new Thread(this); //啟動線程 th.start(); } /** * 游戲繪圖 */ public void myDraw() { try { canvas = sfh.lockCanvas(); if (canvas != null) { canvas.drawColor(Color.WHITE); canvas.drawBitmap(fishBmp[currentFrame], 0, 0, paint); } } catch (Exception e) { // TODO: handle exception } finally { if (canvas != null) sfh.unlockCanvasAndPost(canvas); } } /** * 觸屏事件監聽 */ @Override public boolean onTouchEvent(MotionEvent event) { return true; } /** * 按鍵事件監聽 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } /** * 游戲邏輯 */ private void logic() { currentFrame++; if (currentFrame >= fishBmp.length) { currentFrame = 0; } } @Override public void run() { while (flag) { long start = System.currentTimeMillis(); myDraw(); logic(); long end = System.currentTimeMillis(); try { if (end - start < 50) { Thread.sleep(50 - (end - start)); } } catch (InterruptedException e) { e.printStackTrace(); } } } /** * SurfaceView視圖狀態發生改變,響應此函數 */ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } /** * SurfaceView視圖消亡時,響應此函數 */ @Override public void surfaceDestroyed(SurfaceHolder holder) { flag = false; } }
MainActivity類:
package com.fm; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //設置全屏 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); //顯示自定義的SurfaceView視圖 setContentView(new MySurfaceView(this)); } }
實用控件分享:自定義逼真相機光圈View,控件相機光圈view最近手機界開始流行雙攝像頭,大光圈功能也應用而生。所謂大光圈功能就是能夠對照片進行後期重新對焦,其實現的原理
Android 手機衛士--home界面布局,android--home本文實現當從splash界面進入hone界面的時候,產生一種漸進淡入的動畫效果,在onCreate
mvp+retrofit+rxjava,retrofitrxjava引用 retrofit : com.squareup.retrofit2:r
在上一節中講了View與常用Layout之FrameLayout、Linear