首先实现背景循环
package com.example.android.planegame; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new PlaneView(this)); } }
package com.example.android.planegame; import android.graphics.Canvas; /** * Created by Administrator on 2016/7/25. */ public abstract class BaseDraw { /** * 绘制 * */ abstract void onDraw(Canvas canvas); /** * 销毁 * */ abstract void onDestory(); }
package com.example.android.planegame; import android.content.Context; import android.graphics.Canvas; import android.graphics.PixelFormat; import android.view.SurfaceHolder; import android.view.SurfaceView; /** * Created by Administrator on 2016/7/25. */ public class PlaneView extends SurfaceView implements SurfaceHolder.Callback, Runnable { /** * 游戏是否继续 */ private boolean ifStop = true; private SurfaceHolder holder = null; /** * 画笔 */ private Canvas canvas = null; /** * 绘制背景用的类 */ private DrawBackgound drawBackgound = null; public PlaneView(Context context) { super(context); holder = this.getHolder(); holder.addCallback(this); // 让整个界面透明 holder.setFormat(PixelFormat.TRANSPARENT); setZOrderOnTop(true); drawBackgound = new DrawBackgound(context); } @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { //启动线程 开始绘制 new Thread(this).start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { drawBackgound.onDestory(); } @Override public void run() { while (ifStop) { //锁定画布 canvas = holder.lockCanvas(); if (null != canvas) { //绘制背景 try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } drawBackgound.onDraw(canvas); //解锁画布并提交改变 holder.unlockCanvasAndPost(canvas); } } } }
package com.example.android.planegame; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; import android.util.DisplayMetrics; import android.util.Log; /** * Created by Administrator on 2016/7/25. */ public class DrawBackgound extends BaseDraw { private Bitmap backGround = null; private Rect rectBp = null; private Rect rectBg = null; private Rect rectBg2 = null; private int screenWidth = 0; private int screenHeigth = 0; //偏移量 private int offset = 0; public DrawBackgound(Context context) { backGround = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg2); //未获取屏幕的宽高则获取宽高 if (screenWidth == 0 && screenHeigth == 0) { Resources resources = context.getResources(); DisplayMetrics dm = resources.getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeigth = dm.heightPixels; } } @Override void onDraw(Canvas canvas) { //判断偏移量 if (offset >= screenHeigth) { offset = 0; } else { offset = offset + 1; } rectBp = new Rect(0, 0, backGround.getWidth(), backGround.getHeight()); rectBg = new Rect(0, offset, screenWidth, screenHeigth + offset); rectBg2 = new Rect(0, -screenHeigth + offset, screenWidth, offset); canvas.drawBitmap(backGround, rectBp, rectBg, null); canvas.drawBitmap(backGround, rectBp, rectBg2, null); } @Override void onDestory() { backGround.recycle(); } }
浙公网安备 33010602011771号