android雪花效果

此文章是参考国外大神做的修改

大神的中文翻译地址:

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);
    }

源码下载:

下载地址点这里

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值