`
crazysumer
  • 浏览: 48631 次
社区版块
存档分类
最新评论

【安卓笔记】Live Wallpaper 二:天上掉下一只猫

 
阅读更多


 掉小猫的功能实现了,思路是,每隔一段时间,产生一堆小猫,放到链表中,再不断地去重绘这些小猫,小猫离开屏幕了,就把小猫从链表中赶走。

先看动态效果图



 

 

主要的代码,其余的配置跟Live Wallpaper一是一样的

public class MyWallpaperService extends WallpaperService {

	@Override
	public Engine onCreateEngine() {
		return new MyEngine();
	}

	class MyEngine extends Engine {
		private Handler h = new Handler();
		private int screenWidth;
		private int screenHeight;
		private Paint p;
		private Canvas c;
		private Bitmap bg;
		private ArrayList<Cat> cats;
		private int time = 60;
		private int timer = 0;
		// 用一个线程来处理绘制动态壁纸的过程
		Runnable dThread = new Runnable() {
			@Override
			public void run() {
				createCats();
				drawWallpaper();
			}
		};

		/*
		 * 工具方法,用于将raw中的资源转化成Bitmap类型
		 */
		private Bitmap loadBitmap(int id) {
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
			return bitmap;
		}

		private void drawWallpaper() {
			SurfaceHolder holder = getSurfaceHolder();
			c = holder.lockCanvas();
			if (c != null) {
				c.drawBitmap(bg, 0, 0, p);// 画出背景
				//循环遍历数组,不断地重绘产生的猫咪
				for (Cat it : cats) {
					it.drawCat(c);
				}

				holder.unlockCanvasAndPost(c);
			}
			h.postDelayed(dThread, 20);

		}

		private void createCats() {
			// 画出动态的猫咪,这里timer跟time也是为了让这段产生小猫的代码不断的执行,同时也不妨碍赶走小猫
			timer++;
			if (timer >= time) {
				int px = (int) (Math.random() * screenWidth);
				Cat cat1 = new Cat(loadBitmap(R.drawable.p_01));
				// Cat cat2 = new Cat(loadBitmap(R.drawable.p_02));
				Cat cat3 = new Cat(loadBitmap(R.drawable.p_03));
				if (cat1 != null) {
					cat1.drawCat(c);
					cat1.setPosition(px, 0);
					cats.add(cat1);
				}
				/*
				 * px = (int) (Math.random() * screenWidth);
				 * 
				 * if (cat2 != null) { cat2.drawCat(c); cat2.setPosition(px, 0);
				 * cats.add(cat2); }
				 */
				px = (int) (Math.random() * screenWidth);
				if (cat3 != null) {
					cat3.drawCat(c);
					cat3.setPosition(px, 0);
					cats.add(cat3);
				}
				timer = 0;
			}
			
			//记得把超出屏幕的猫咪赶走
			for (int i = 0; i < cats.size(); i++) {
				if (cats.get(i).positionY > screenHeight) {
					cats.remove(i);
				}
			}
		}

		public MyEngine() {
			p = new Paint();
			p.setColor(Color.RED);
			p.setAntiAlias(true);
			p.setStyle(Style.STROKE);
			p.setStrokeWidth(3);
			p.setTextSize(30);
			bg = loadBitmap(R.drawable.bg1);
			cats = new ArrayList<MyWallpaperService.Cat>();
		}

		@Override
		public void onCreate(SurfaceHolder surfaceHolder) {
			Log.i("MyEngine----------", "engine created");
			super.onCreate(surfaceHolder);
			setTouchEventsEnabled(true);
		}

		/**
		 * 调用SurfaceHolder.Callback中的回调函数surfaceCreated
		 * */
		@Override
		public void onSurfaceCreated(SurfaceHolder holder) {
			Log.i("MyEngine----------", "surface created");
			super.onSurfaceCreated(holder);
		}

		@Override
		public void onSurfaceChanged(SurfaceHolder holder, int format,
				int width, int height) {
			if (width > 0 && height > 0) {
				this.screenWidth = width;
				this.screenHeight = height;
			}
			super.onSurfaceChanged(holder, format, width, height);
		}

		@Override
		public void onVisibilityChanged(boolean visible) {
			super.onVisibilityChanged(visible);
			if (visible) {
				h.postDelayed(dThread, 20);
			} else {
				h.removeCallbacks(dThread);
			}
		}

		@Override
		public void onSurfaceDestroyed(SurfaceHolder holder) {
			Log.i("MyEngine----------", "surface destoryed");
			super.onSurfaceDestroyed(holder);
			h.removeCallbacks(dThread);
		}

		@Override
		public void onDestroy() {
			Log.i("MyEngine----------", "engine destoryed");
			super.onDestroy();
			h.removeCallbacks(dThread);
		}

		@Override
		public void onTouchEvent(MotionEvent event) {
			super.onTouchEvent(event);
			h.postDelayed(dThread, 20);
		}

	}

	/**
	 * 用一个类来封装猫咪图片的信息
	 * */
	public class Cat {
		Bitmap sourceBitmap;
		int positionX = 0;// 图片上角的X坐标
		int positionY = 0;// 图片左上角的Y坐标
		// 设置猫咪图片的位置
		public void setPosition(int x, int y) {
			positionX = x;
			positionY = y;
		}

		public Cat(Bitmap bitmap) {
			sourceBitmap = bitmap;
		}

		// 画猫咪图片
		public void drawCat(Canvas canvas) {

			if (sourceBitmap != null) {
				canvas.drawBitmap(sourceBitmap, positionX, positionY, null);
			}
			nextFrame();
		}

		// 下一帧
		public void nextFrame() {
			positionY += 1;
		}

	}

}
  

 

 

虽然有密集恐惧症,可是看到猫咪的图片不会,果然是萌物啊眨眼

 

 

 

 

 

 

传送门:

http://zhujiao.iteye.com/blog/1537528

 

 

 

 

  • 大小: 286.3 KB
  • 大小: 293.1 KB
  • 大小: 383.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics