Android 马赛克(Mosaics)效果


layout: post
title: Android 马赛克(Mosaics)效果
date: 2015-12-13
categories: blog
tags: [技术,Android]
category: Android
description: 使用两种不同的方案实现Android平台下的马赛克效果


本文首发于KuTear,转载请注明
代码地址
GitHub

前几天看见开源项目[ExplosionField]效果好赞,看了下代码,他的实现大致就是在原界面之上覆盖一成自定义的View,获取到点击的那个View的内容(Bitmap),然后在覆盖的那个自定义View的特定位置画出来,之后就是对这个Bitmap做一些列拆分,变化重绘的过程.在这里根据他对bitmap的拆分,感觉用来实现Bitmap的效果也是不错的,就试着做一做.

在这里介绍使用两种方式实现马赛克效果.开始之前先看看效果


Screenshot from 2015-12-13 19-09-50.png

感觉还可以

  1. 直接绘制

    public static Bitmap getMosaicsBitmap(Bitmap bmp, double precent) {
    long start = System.currentTimeMillis();
    int bmpW = bmp.getWidth();
    int bmpH = bmp.getHeight();
    Bitmap resultBmp = Bitmap.createBitmap(bmpW, bmpH, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(resultBmp);
    Paint paint = new Paint();
    double unit;
    if (precent == 0) {
        unit = bmpW;
    } else {
        unit = 1 / precent;
    }
    double resultBmpW = bmpW / unit;
    double resultBmpH = bmpH / unit;
    for (int i = 0; i < resultBmpH; i++) {
        for (int j = 0; j < resultBmpW; j++) {
            int pickPointX = (int) (unit * (j + 0.5));
            int pickPointY = (int) (unit * (i + 0.5));
            int color;
            if (pickPointX >= bmpW || pickPointY >= bmpH) {
                color = bmp.getPixel(bmpW / 2, bmpH / 2);
            } else {
                color = bmp.getPixel(pickPointX, pickPointY);
            }
            paint.setColor(color);
            canvas.drawRect((int) (unit * j), (int) (unit * i), (int) (unit * (j + 1)), (int) (unit * (i + 1)), paint);
        }
    }
    canvas.setBitmap(null);
    long end = System.currentTimeMillis();
    Log.v(TAG, "DrawTime:" + (end - start));
    return resultBmp;
    } 
    
  2. 修改像素点

    public static Bitmap getMosaicsBitmaps(Bitmap bmp, double precent) {
    long start = System.currentTimeMillis();
    int bmpW = bmp.getWidth();
    int bmpH = bmp.getHeight();
    int[] pixels = new int[bmpH * bmpW];
    bmp.getPixels(pixels, 0, bmpW, 0, 0, bmpW, bmpH);
    int raw = (int) (bmpW * precent);
    int unit;
    if (raw == 0) {
        unit = bmpW;
    } else {
        unit = bmpW / raw; //原来的unit*unit像素点合成一个,使用原左上角的值
    }
    if (unit >= bmpW || unit >= bmpH) {
        return getMosaicsBitmap(bmp, precent);
    }
    for (int i = 0; i < bmpH; ) {
        for (int j = 0; j < bmpW; ) {
            int leftTopPoint = i * bmpW + j;
            for (int k = 0; k < unit; k++) {
                for (int m = 0; m < unit; m++) {
                    int point = (i + k) * bmpW + (j + m);
                    if (point < pixels.length) {
                        pixels[point] = pixels[leftTopPoint];
                    }
                }
            }
            j += unit;
        }
        i += unit;
    }
    long end = System.currentTimeMillis();
    Log.v(TAG, "DrawTime:" + (end - start));
    return Bitmap.createBitmap(pixels, bmpW, bmpH, Bitmap.Config.ARGB_8888);
}

从效率上来看,第二中方式效率会高10倍,只要是因为第一种方式绘制的次数太多了,而绘制是比较费时间的.这里,特别提示,不要在大量的循环语句内部使用Log.v(...),这是一个很耗时间的操作.

你可能感兴趣的:(Android 马赛克(Mosaics)效果)