此文章是参考国外大神做的修改
大神的中文翻译地址:
http://www.open-open.com/lib/view/open1452263908573.html
接下来上效果图:
雪花类我做了修改,定义了九张图片,根据传入的类型绘制不同的雪花
int[] snows = {R.mipmap.snow, R.mipmap.snowmiddle, R.mipmap.snowsmall,
R.mipmap.snow2big, R.mipmap.snow2middle, R.mipmap.snow2small,
R.mipmap.snow3big, R.mipmap.snow3middle, R.mipmap.snow3small};
public Bitmap getBitMap(int type) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;//内存可被回收
opt.inInputShareable = true;
//获取资源图片
return BitmapFactory.decodeResource(contexts.getResources(), snows[type]);
}
protected void resize(int width, int height) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
snowflakes = new SnowFlake[NUM_SNOWFLAKES];
Random random = new Random();
for (int i = 0; i < NUM_SNOWFLAKES; i++) {
snowflakes[i] = SnowFlake.create(width, height, paint,random.nextInt(9),context);
}
}
在雪花OnDraw方法不停地重绘改变位置
public void draw(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
move(width, height);
// canvas.drawCircle(position.x, position.y, flakeSize, paint);
canvas.drawBitmap(getBitMap(getType()), position.x, position.y, paint);
}
源码下载: