02 Android 植物人大战僵尸-背景图层布置
生活随笔
收集整理的這篇文章主要介紹了
02 Android 植物人大战僵尸-背景图层布置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.背景圖層布置效果
該圖層主要是2張圖片過程,草地和放置卡片的狀態圖層過程,屬于靜態圖片范疇
2. 背景圖片的屏幕適配
這里主要根據圖片的縮放比對原始圖片進行重新繪制,達到適配屏幕的效果
- 縮放比寬 = 屏幕界面寬 / 圖片本身寬
- 縮放比高 = 屏幕界面高 / 圖片本身高
屏幕寬高獲取方法如下
/*** 獲取屏幕大小* * @param context* @return*/public static int[] getDeviceInfo(Context context) {if ((deviceWidthHeight[0] == 0) && (deviceWidthHeight[1] == 0)) {DisplayMetrics metrics = new DisplayMetrics();((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);deviceWidthHeight[0] = metrics.widthPixels;deviceWidthHeight[1] = metrics.heightPixels;}return deviceWidthHeight;}根據縮放比重新繪制圖片的方法如下
public static Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {if (bitmap != null) {int width = bitmap.getWidth();int height = bitmap.getHeight();int newWidth = w;int newHeight = h;// 縮放比float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// 圖片矩陣Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);return resizedBitmap;} else {return null;}}這里需要設置下橫屏顯示和無狀態欄的設置,AnroidMainfest.xml
android:screenOrientation="landscape"android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >3. 背景圖片繪制到sufaceView中
其中 放置卡片的起始 X 坐標是 (界面寬度-卡片寬度) / 2
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {@Overridepublic void run() {while (gameRunFlag) {// 這里需要考慮線程同步synchronized (mSurfaceHolder) {try {// 鎖住畫布才能繪圖mCanvas = mSurfaceHolder.lockCanvas();mCanvas.drawBitmap(Config.gameBg, 0, 0, mPaint);// 放置卡片的起始 X 坐標是 (界面寬度-卡片寬度) / 2mCanvas.drawBitmap(Config.seekBank, (Config.screenWidth - Config.seekBank.getWidth()) /2 , 0, mPaint);} catch (Exception e) {e.printStackTrace();} finally {// 解鎖并提交mSurfaceHolder.unlockCanvasAndPost(mCanvas);}try {Thread.sleep(60);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}4. 完整 Demo 下載地址
https://github.com/sufadi/BotanyWarZombies
總結
以上是生活随笔為你收集整理的02 Android 植物人大战僵尸-背景图层布置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 8.0以上系统应用如何保
- 下一篇: Android 超级简单的沉浸式状态栏