关于QQ群头像以及微信讨论组头像的工具类

来源:互联网 发布:乾隆身世 知乎 编辑:程序博客网 时间:2024/05/18 00:23

QQ群头像以及微信讨论组头像工具类介绍

 介绍:

     由于段时间公司项目需求,在翻了网上很多代码后发现,很多人用的是自定义View的办法来实现此类头像的效果,但是,这样一来就必须改变项目中原有的控件,而且当需要将此图片内容传递到后台时,就会变的非常麻烦,所以我就从网上找了一个Demo来自己修改,将其变成了一个可以直接生成一张头像图片的工具类,这样一来无论你需要什么样的操作都会非常方便,可以将这张生成的图片随意设置,下面放几张项目的图片给各位看一下:

       下面是微信的讨论组头像展示:

    主要是用到了三个类:

     这里的JoinBitmaps主要是底层对该图片的一些剪裁等处理,JoinLayout是对头像中具体图片绘制时的参数进行设置,所以,这两个类其实是网上找的牛人写好的,TaolunBitmapUtils则是我对一些具体方法的封装,下面将贴出具体代码内容:

 

public class JoinBitmaps {    public static final void join(Canvas canvas, int dimension, List<Bitmap> bitmaps) {        if (bitmaps == null)            return;        int count = Math.min(bitmaps.size(), JoinLayout.max());        float[] size = JoinLayout.size(count);        join(canvas, dimension, bitmaps, count, size);    }    public static final void join(Canvas canvas, int dimension, List<Bitmap> bitmaps, int count,                                  float[] size) {        join(canvas, dimension, bitmaps, count, size, 0.15f);    }    public static final void join(Canvas canvas, int dimension, List<Bitmap> bitmaps,                                  float gapSize) {        if (bitmaps == null)            return;        int count = Math.min(bitmaps.size(), JoinLayout.max());        float[] size = JoinLayout.size(count);        join(canvas, dimension, bitmaps, count, size, gapSize);    }    public static final void join(Canvas canvas, int dimension, List<Bitmap> bitmaps, int count,                                  float[] size, float gapSize) {        if (bitmaps == null)            return;        // 旋转角度        float[] rotation = JoinLayout.rotation(count);        // paint        Paint paint = new Paint();        paint.setAntiAlias(true);        Matrix matrixJoin = new Matrix();        // scale as join size        matrixJoin.postScale(size[0], size[0]);        canvas.save();        // canvas.drawColor(Color.RED);        // index<count bitmaps.size 可能会越界        for (int index = 0; index < count; index++) {            Bitmap bitmap = bitmaps.get(index);            // MATRIX            Matrix matrix = new Matrix();            // scale as destination            matrix.postScale((float) dimension / bitmap.getWidth(),                    (float) dimension / bitmap.getHeight());            canvas.save();            matrix.postConcat(matrixJoin);            float[] offset = JoinLayout.offset(count, index, dimension, size);            canvas.translate(offset[0], offset[1]);            // 缩放            Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),                    bitmap.getHeight(), matrix, true);            // 裁剪            Bitmap bitmapOk = createMaskBitmap(newBitmap, newBitmap.getWidth(),                    newBitmap.getHeight(), (int) rotation[index], gapSize);            canvas.drawBitmap(bitmapOk, 0, 0, paint);            canvas.restore();        }        canvas.restore();    }    public static final Bitmap createMaskBitmap(Bitmap bitmap, int viewBoxW, int viewBoxH,                                                int rotation, float gapSize) {        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),                Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(output);        final Paint paint = new Paint();        paint.setAntiAlias(true);// 抗锯齿        paint.setFilterBitmap(true);        int center = Math.round(viewBoxW / 2f);        canvas.drawCircle(center, center, center, paint);        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        canvas.drawBitmap(bitmap, 0, 0, paint);        if (rotation != 360) {            Matrix matrix = new Matrix();            // 根据原图的中心位置旋转            matrix.setRotate(rotation, viewBoxW / 2, viewBoxH / 2);            canvas.setMatrix(matrix);            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));            canvas.drawCircle(viewBoxW * (1.5f - gapSize), center, center, paint);        }        return output;    }    public static final Bitmap createBitmap(int width, int height, List<Bitmap> bitmaps) {        int count = Math.min(bitmaps.size(), JoinLayout.max());        float[] size = JoinLayout.size(count);        return createBitmap(width, height, bitmaps, count, size, 0.15f);    }    public static final Bitmap createBitmap(int width, int height, List<Bitmap> bitmaps,                                            int count, float[] size) {        return createBitmap(width, height, bitmaps, count, size, 0.15f);    }    public static final Bitmap createBitmap(int width, int height, List<Bitmap> bitmaps,                                            int count, float[] size, float gapSize) {        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(output);        int dimen = Math.min(width, height);        join(canvas, dimen, bitmaps, count, size, gapSize);        return output;    }}

  

public class JoinLayout {    public static final String TAG = JoinLayout.class.getSimpleName();    public static int max() {        return 5;    }    private static final float[][] rotations = { new float[] { 360.0f }, new float[] { 45.0f, 360.0f },            new float[] { 120.0f, 0.0f, -120.0f }, new float[] { 90.0f, 180.0f, -90.0f, 0.0f },            new float[] { 144.0f, 72.0f, 0.0f, -72.0f, -144.0f }, };    public static float[] rotation(int count) {        return count > 0 && count <= rotations.length ? rotations[count - 1] : null;    }    //参数一:圆的半径    private static final float[][] sizes = { new float[] { 0.92f, 0.9f },            new float[] { 0.51f, 0.65f }, new float[] { 0.47f, 0.8f },            new float[] { 0.43f, 0.91f }, new float[] { 0.38f, 0.80f } };    public static float[] size(int count) {        return count > 0 && count <= sizes.length ? sizes[count - 1] : null;    }    public static float[] offset(int count, int index, float dimension, float[] size) {        switch (count) {            case 1:                return offset1(index, dimension, size);            case 2:                return offset2(index, dimension, size);            case 3:                return offset3(index, dimension, size);            case 4:                return offset4(index, dimension, size);            case 5:                return offset5(index, dimension, size);            default:                break;        }        return new float[] { 0f, 0f };    }    /**     * 5个头像     *     * @param index     *            下标     * @param dimension     *            画布边长(正方形)     * @param size     *            size[0]缩放 size[1]边距     * @return 下标index X,Y轴坐标     */    private static float[] offset5(int index, float dimension, float[] size) {        // 圆的直径        float cd = (float) dimension * size[0];        // 边距        float s1 = -cd * size[1];        float x1 = 0;        float y1 = s1;        float x2 = (float) (s1 * Math.cos(19 * Math.PI / 180));        float y2 = (float) (s1 * Math.sin(18 * Math.PI / 180));        float x3 = (float) (s1 * Math.cos(54 * Math.PI / 180));        float y3 = (float) (-s1 * Math.sin(54 * Math.PI / 180));        float x4 = (float) (-s1 * Math.cos(54 * Math.PI / 180));        float y4 = (float) (-s1 * Math.sin(54 * Math.PI / 180));        float x5 = (float) (-s1 * Math.cos(19 * Math.PI / 180));        float y5 = (float) (s1 * Math.sin(18 * Math.PI / 180));        // Log.d(TAG, "x1:" + x1 + "/y1:" + y1);        // Log.d(TAG, "x2:" + x2 + "/y2:" + y2);        // Log.d(TAG, "x3:" + x3 + "/y3:" + y3);        // Log.d(TAG, "x4:" + x4 + "/y4:" + y4);        // Log.d(TAG, "x5:" + x5 + "/y5:" + y5);        // 居中 Y轴偏移量        float xx1 = (dimension - cd - y3 - s1) / 2 * 0.91f;        // 居中 X轴偏移量        float xxc1 = (dimension - cd) / 2;        // xx1 = xxc1 = -s1;        // xx1 = xxc1 = 0;        switch (index) {            case 0:                // return new float[] { s1 + xxc1, xx1 };                return new float[] { x1 + xxc1, y1 + xx1 };            case 1:                return new float[] { x2 + xxc1, y2 + xx1 };            case 2:                return new float[] { x3 + xxc1, y3 + xx1 };            case 3:                return new float[] { x4 + xxc1, y4 + xx1 };            case 4:                return new float[] { x5 + xxc1, y5 + xx1 };            default:                break;        }        return new float[] { 0f, 0f };    }    /**     * 4个头像     *     * @param index     *            下标     * @param dimension     *            画布边长(正方形)     * @param size     *            size[0]缩放 size[1]边距     * @return 下标index X,Y轴坐标     */    private static float[] offset4(int index, float dimension, float[] size) {        // 圆的直径        float cd = (float) dimension * size[0];        // 边距        float s1 = cd * size[1];        float x1 = 0;        float y1 = 0;        float x2 = s1;        float y2 = y1;        float x3 = s1;        float y3 = s1;        float x4 = x1;        float y4 = y3;        // Log.d(TAG, "x1:" + x1 + "/y1:" + y1);        // Log.d(TAG, "x2:" + x2 + "/y2:" + y2);        // Log.d(TAG, "x3:" + x3 + "/y3:" + y3);        // Log.d(TAG, "x4:" + x4 + "/y4:" + y4);        // 居中 X轴偏移量        float xx1 = (dimension - cd - s1) / 2;        switch (index) {            case 0:                return new float[] { x1 + xx1, y1 + xx1 };            case 1:                return new float[] { x2 + xx1, y2 + xx1 };            case 2:                return new float[] { x3 + xx1, y3 + xx1 };            case 3:                return new float[] { x4 + xx1, y4 + xx1 };            default:                break;        }        return new float[] { 0f, 0f };    }    /**     * 3个头像     *     * @param index     *            下标     * @param dimension     *            画布边长(正方形)     * @param size     *            size[0]缩放 size[1]边距     * @return 下标index X,Y轴坐标     */    private static float[] offset3(int index, float dimension, float[] size) {        // 圆的直径        float cd = (float) dimension * size[0];        // 边距        float s1 = cd * size[1];        // 第二个圆的 Y坐标        float y2 = s1 * (3 / 2);        // 第二个圆的 X坐标        float x2 = s1 - y2 / 1.73205f;        // 第三个圆的 X坐标        float x3 = s1 * 2 - x2;        // 居中 Y轴偏移量        float xx1 = (dimension - cd - y2) / 2 * 0.18f;        // 居中 X轴偏移量        float xxc1 = (dimension - cd) / 2 - s1;        // xx1 = xxc1 = 0;        switch (index) {            case 0:                return new float[] { s1 + xxc1, xx1 };            case 1:                return new float[] { x2 + xxc1, y2 + xx1 };            case 2:                return new float[] { x3 + xxc1, y2 + xx1 };            default:                break;        }        return new float[] { 0f, 0f };    }    /**     * 2个头像     *     * @param index     *            下标     * @param dimension     *            画布边长(正方形)     * @param size     *            size[0]缩放 size[1]边距     * @return 下标index X,Y轴坐标     */    private static float[] offset2(int index, float dimension, float[] size) {        // 圆的直径        float cd = (float) dimension * size[0];        // 边距        float s1 = cd * size[1];        float x1 = 0;        float y1 = 0;        float x2 = s1;        float y2 = s1;        // Log.d(TAG, "x1:" + x1 + "/y1:" + y1);        // Log.d(TAG, "x2:" + x2 + "/y2:" + y2);        // 居中 X轴偏移量        float xx1 = (dimension - cd - s1) / 2;        switch (index) {            case 0:                return new float[] { x1 + xx1, y1 + xx1 };            case 1:                return new float[] { x2 + xx1, y2 + xx1 };            default:                break;        }        return new float[] { 0f, 0f };    }    /**     * 1个头像     *     * @param index     *            下标     * @param dimension     *            画布边长(正方形)     * @param size     *            size[0]缩放 size[1]边距     * @return 下标index X,Y轴坐标     */    private static float[] offset1(int index, float dimension, float[] size) {        // 圆的直径        float cd = (float) dimension * size[0];        float offset = (dimension - cd) / 2;        return new float[] { offset, offset };    }}


    最后就是我整理的一些方法,里面都有很详细的注解,相信各位都能看得明白。

public class TaolunBitmapUtils {    private static Bitmap[] mbitmap;    /**     *     * @param tempWidth  最终图片的宽     * @param tempHeight  最终图片的高     * @param bitmaps    放入里面的图片集合,可放1-5个数量     * @return   返回最终的Bitmap类型,并且是经过圆形剪裁的最终图片     *     *  仿照QQ讨论组的头像,此方法的背景颜色已固定,可以在此代码修改     */    public static Bitmap CircleTaolunBitmap(int tempWidth, int tempHeight, List<Bitmap> bitmaps){        if(bitmaps.size()<1 && bitmaps.size() >5){            return null;        }        Bitmap bitmap = bitmaps.get(0);        if(bitmap == null){            return null;        }        Bitmap canvasBitmap = Bitmap.createBitmap(tempWidth, tempHeight,                Bitmap.Config.ARGB_8888);        Canvas localCanvas = new Canvas(canvasBitmap);        localCanvas.drawColor(Color.parseColor("#d1dedf"));        JoinBitmaps.join(localCanvas, Math.min(tempWidth, tempHeight),bitmaps);        return GetRoundedCornerBitmap(canvasBitmap);    }    /**     *     * @param tempWidth   最终图片的宽     * @param tempHeight  最终图片的高     * @param bitmaps     放入里面的图片集合,可放1-5个数量     * @param background  可以设定最终图的背景颜色     * @return   返回最终的Bitmap类型,并且是经过圆形剪裁的最终图片     *     * 仿QQ讨论组头像的方法,此方法的背景颜色可以修改     */    public static Bitmap CircleTaolunBitmap(int tempWidth, int tempHeight, List<Bitmap> bitmaps,int background){        //防止输入的集合为空        if(bitmaps.size()<1 && bitmaps.size() >5){            return null;        }        //取出第一张图片检验是否为空        Bitmap bitmap = bitmaps.get(0);        if(bitmap == null){            return null;        }        Bitmap canvasBitmap = Bitmap.createBitmap(tempWidth, tempHeight,                Bitmap.Config.ARGB_8888);        Canvas localCanvas = new Canvas(canvasBitmap);        localCanvas.drawColor(background);        JoinBitmaps.join(localCanvas, Math.min(tempWidth, tempHeight),bitmaps);        return GetRoundedCornerBitmap(canvasBitmap);    }    private static int padding = 2; /** 图片之间的距离 */    private static int cornor = 0;/** 内部图片的圆角值 */    private static Bitmap[] paramList ;    /**     *    此方法是仿微信讨论组头像的制作方法     *     * @param context 上下文对象     * @param tempWidth 所显示最终图片的宽     * @param tempHeight 所显示最终图片的高     * @param bitmaps  内层图片存放的bitmap集合     * @return   仿微信讨论组头像的最终图片bitmap     */    public static Bitmap SquareTaolunzuBitmap(Context context,int tempWidth,int tempHeight,List<Bitmap> bitmaps){           // 重置padding                padding = 2;        paramList = bitmaps.toArray(new Bitmap[bitmaps.size()]);        if (paramList.length < 1 && paramList.length > 9) {            return null;        }        // 先取一个获取宽和高        Bitmap tempBitmap = (Bitmap) paramList[0];        if (tempBitmap == null) {            return null;        }        // 创建一个空格的bitmap        Bitmap canvasBitmap = Bitmap.createBitmap(tempWidth, tempHeight,                Bitmap.Config.ARGB_8888);        // 头像的数量        int bitmapCount = paramList.length;        Canvas localCanvas = new Canvas(canvasBitmap);        localCanvas.drawColor(Color.parseColor("#d1dedf"));        int colum = 0;        if (bitmapCount > 1 && bitmapCount < 5) {            colum = 2;        } else if (bitmapCount > 4 && bitmapCount < 10) {            colum = 3;        } else {            colum = 1;        }        float scale = 1.0F / colum;        // 根据列数缩小        Bitmap scaledBitmap = scaleBitmap(scale, tempBitmap);        if (padding > 0) {            padding = dip2px(context, padding);            // 如果有内边距 再次缩小            float paddingScale = (float) (tempWidth - (colum + 1) * padding)                    / colum / scaledBitmap.getWidth();            scaledBitmap = scaleBitmap(paddingScale, scaledBitmap);            scale = scale * paddingScale;        }        // 第一行的 头像个数        int topRowCount = bitmapCount % colum;        // 满行的行数        int rowCount = bitmapCount / colum;        if (topRowCount > 0) {            // 如果第一行头像个数大于零 行数加1            rowCount++;        } else if (topRowCount == 0) {            // 6 或者 9 第一行头像个数和列数一致            topRowCount = colum;        }        // 缩小后头像的宽        int scaledWidth = scaledBitmap.getWidth();        // 缩小后头像的高        int scaledHeight = scaledBitmap.getHeight();        // 第一个头像与画布顶部的距离        int firstTop = ((tempHeight - (rowCount * scaledHeight + (rowCount + 1)                * padding)) / 2)                + padding;        // 第一个头像与画布左部的距离        int firstLeft = ((tempWidth - (topRowCount * scaledWidth + (topRowCount + 1)                * padding)) / 2)                + padding;        for (int i = 0; i < paramList.length; i++) {            if (i == 9) {// 达到上限 停止                break;            }            // 按照最终压缩比例压缩            Bitmap bit = scaleBitmap(scale, (Bitmap) paramList[i]);            if (cornor > 0) {                // 圆角化                bit = GetRoundedCornerBitmap(bit);            }            localCanvas.drawBitmap(bit, firstLeft, firstTop, null);            firstLeft += (scaledWidth + padding);            if (i == topRowCount - 1 | tempWidth - firstLeft < scaledWidth) {                firstTop += (scaledHeight + padding);                firstLeft = padding;            }            bit.recycle();        }        // 重置padding       // padding = 2;        localCanvas.save(Canvas.ALL_SAVE_FLAG);        localCanvas.restore();        return canvasBitmap;    }    /**     *    此方法是可以修改背景的仿微信讨论组头像的制作方法     *     * @param context 上下文对象     * @param tempWidth 所显示最终图片的宽     * @param tempHeight 所显示最终图片的高     * @param bitmaps  内层图片存放的bitmap集合     * @param background  可以修改头像的底层背景     * @return   仿微信讨论组头像的最终图片bitmap     */    public static Bitmap SquareTaolunzuBitmap(Context context,int tempWidth,int tempHeight,List<Bitmap> bitmaps,int background){        // 重置padding        padding = 2;        paramList = bitmaps.toArray(new Bitmap[bitmaps.size()]);        if (paramList.length < 1 && paramList.length > 9) {            return null;        }        // 先取一个获取宽和高        Bitmap tempBitmap = (Bitmap) paramList[0];        if (tempBitmap == null) {            return null;        }        // 创建一个空格的bitmap        Bitmap canvasBitmap = Bitmap.createBitmap(tempWidth, tempHeight,                Bitmap.Config.ARGB_8888);        // 头像的数量        int bitmapCount = paramList.length;        Canvas localCanvas = new Canvas(canvasBitmap);        localCanvas.drawColor(background);        int colum = 0;        if (bitmapCount > 1 && bitmapCount < 5) {            colum = 2;        } else if (bitmapCount > 4 && bitmapCount < 10) {            colum = 3;        } else {            colum = 1;        }        float scale = 1.0F / colum;        // 根据列数缩小        Bitmap scaledBitmap = scaleBitmap(scale, tempBitmap);        if (padding > 0) {            padding = dip2px(context, padding);            // 如果有内边距 再次缩小            float paddingScale = (float) (tempWidth - (colum + 1) * padding)                    / colum / scaledBitmap.getWidth();            scaledBitmap = scaleBitmap(paddingScale, scaledBitmap);            scale = scale * paddingScale;        }        // 第一行的 头像个数        int topRowCount = bitmapCount % colum;        // 满行的行数        int rowCount = bitmapCount / colum;        if (topRowCount > 0) {            // 如果第一行头像个数大于零 行数加1            rowCount++;        } else if (topRowCount == 0) {            // 6 或者 9 第一行头像个数和列数一致            topRowCount = colum;        }        // 缩小后头像的宽        int scaledWidth = scaledBitmap.getWidth();        // 缩小后头像的高        int scaledHeight = scaledBitmap.getHeight();        // 第一个头像与画布顶部的距离        int firstTop = ((tempHeight - (rowCount * scaledHeight + (rowCount + 1)                * padding)) / 2)                + padding;        // 第一个头像与画布左部的距离        int firstLeft = ((tempWidth - (topRowCount * scaledWidth + (topRowCount + 1)                * padding)) / 2)                + padding;        for (int i = 0; i < paramList.length; i++) {            if (i == 9) {// 达到上限 停止                break;            }            // 按照最终压缩比例压缩            Bitmap bit = scaleBitmap(scale, (Bitmap) paramList[i]);            if (cornor > 0) {                // 圆角化                bit = GetRoundedCornerBitmap(bit);            }            localCanvas.drawBitmap(bit, firstLeft, firstTop, null);            firstLeft += (scaledWidth + padding);            if (i == topRowCount - 1 | tempWidth - firstLeft < scaledWidth) {                firstTop += (scaledHeight + padding);                firstLeft = padding;            }            bit.recycle();        }        // 重置padding        //padding = 2;        localCanvas.save(Canvas.ALL_SAVE_FLAG);        localCanvas.restore();        return canvasBitmap;    }    //绘制仿微信讨论组头像内部头像的重新剪裁图片bitmap    按比例缩放图片    private static Bitmap scaleBitmap(float paramFloat, Bitmap paramBitmap) {        Matrix localMatrix = new Matrix();        localMatrix.postScale(paramFloat, paramFloat);        //找出内部图片的最短距离,以这个长度来进行重新绘制得到正方形图片        int chang = Math.min(paramBitmap.getWidth(), paramBitmap.getHeight());        return Bitmap.createBitmap(paramBitmap, 0, 0,chang ,chang               , localMatrix, true);    }    //此方法用于仿微信讨论组头像的尺寸转换   即转换为设备独立像素dip    private static int dip2px(Context context, float value) {        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,                value, context.getResources().getDisplayMetrics()) + 0.5f);    }    /**     *    将图片剪裁成圆形的工具类     * @param bitmap   传入一张图片  bitmap     * @return    返回此图片裁剪成圆形之后的bitmap     */    private static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {        try {            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);            Canvas canvas = new Canvas(output);            final Paint paint = new Paint();            final Rect rect = new Rect(0, 0, bitmap.getWidth(),                    bitmap.getHeight());            final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),                    bitmap.getHeight()));            paint.setAntiAlias(true);            canvas.drawARGB(0, 0, 0, 0);            paint.setColor(Color.BLACK);            int width = bitmap.getWidth();            int height = bitmap.getHeight();            int cornor = Math.min(width,height);            canvas.drawRoundRect(rectF, cornor, cornor, paint);            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));            final Rect src = new Rect(0, 0, bitmap.getWidth(),                    bitmap.getHeight());            canvas.drawBitmap(bitmap, src, rect, paint);            return output;        } catch (Exception e) {            return bitmap;        }    }}


使用:     

当然,最后免不了讲一下使用的步骤:

1.将这三个工具类放到自己项目的文件夹下面;

2.在需要的地方调用相应的方法来生成一张Bitmap;


          2.1qq群头像使用示例:

  private ImageView[] name =new ImageView[id.length];    private List<Bitmap> qq = new ArrayList<>();//作为QQ群头像的数据源
 //QQ3个头像        Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(),R.mipmap.a1);        Bitmap bitmap5 = BitmapFactory.decodeResource(getResources(),R.mipmap.a2);        Bitmap bitmap6 = BitmapFactory.decodeResource(getResources(),R.mipmap.a3);        qq.clear();        qq.add(bitmap4);        qq.add(bitmap5);        qq.add(bitmap6);        Bitmap data2 = TaolunBitmapUtils.CircleTaolunBitmap(150,150,qq);        name[2].setImageBitmap(data2);           //改变头像背景        Bitmap datas2 = TaolunBitmapUtils.CircleTaolunBitmap(150,150,qq, Color.parseColor("#cc805f"));        name[7].setImageBitmap(datas2);
     这里是生成一张具有三个头像的图片例子,这里需要特别注意的是,TaolunBitmapUtils这个类中所有的方法都是需要给一个Bitmap类型的List集合,之后,相应的方法中会自动将集合里面的Bitmap取出来制作成一张头像图片。例如,这里的CircleTaolunBitmap()方法中的最后一个参数qq就是已经写好的Bitmap集合,最终返回的这张Bitmap就是你所需要的图片了,在这里我将它放在了ImageView上面,至于下面的4个参数的CircleTaolunBitmap()中最后一个参数是设置的整个图片的背景色,需要的可以在这里自由设置。



       2.2微信讨论组头像使用示例
   private ImageView[] name =new ImageView[id.length];    private List<Bitmap> wx = new ArrayList<>();//作为微信讨论组头像的数据源

        Bitmap bitmap91 = BitmapFactory.decodeResource(getResources(),R.mipmap.a1);        Bitmap bitmap92 = BitmapFactory.decodeResource(getResources(),R.mipmap.a2);        Bitmap bitmap93 = BitmapFactory.decodeResource(getResources(),R.mipmap.a3);        Bitmap bitmap94 = BitmapFactory.decodeResource(getResources(),R.mipmap.a4);        Bitmap bitmap95 = BitmapFactory.decodeResource(getResources(),R.mipmap.a5);        Bitmap bitmap96 = BitmapFactory.decodeResource(getResources(),R.mipmap.a6);        Bitmap bitmap97 = BitmapFactory.decodeResource(getResources(),R.mipmap.a1);        Bitmap bitmap98 = BitmapFactory.decodeResource(getResources(),R.mipmap.a2);        Bitmap bitmap99 = BitmapFactory.decodeResource(getResources(),R.mipmap.a3);        wx.clear();        wx.add(bitmap91);        wx.add(bitmap92);        wx.add(bitmap93);        wx.add(bitmap94);        wx.add(bitmap95);        wx.add(bitmap96);        wx.add(bitmap97);        wx.add(bitmap98);        wx.add(bitmap99);        Bitmap data9 = TaolunBitmapUtils.SquareTaolunzuBitmap(this,170,170,wx);        name[2].setImageBitmap(data9);        //改变头像背景        Bitmap datas9 = TaolunBitmapUtils.SquareTaolunzuBitmap(this,170,170,wx, Color.parseColor("#a45d97"));        name[5].setImageBitmap(datas9);

     这里是设置微信头像的例子,原理还是跟上面一样,主要调用的是SquareTaolunzuBitmap()这个方法了,第一个参数环境变量,第二三个参数分别是宽跟高,最后一个参数同样是Bitmap类型的List集合,倒数第二行的代码中,多的那一个参数也是对背景颜色的设置,供给这方面有所需要的童鞋们使用。

总结:

     基本的介绍就到这里为止了,如果还有不明白的地方可以参考下面的链接中的Demo,里面我都尽可能详细地加了很多注解,而且,三个页面都是最简单的方式进行呈现的,或者还可以私信我,虽然不常常上线,但是在看到的时候会尽快回复的。

    最后,如果哪里还有不好的地方请各位及时指出,我也会尽力改正的!

关于QQ群头像以及微信讨论组头像的工具类下载链接:http://download.csdn.net/download/yu537476/9955248
关于QQ群头像以及微信讨论组头像的工具类Demo链接:http://download.csdn.net/download/yu537476/9955253   Github地址:https://github.com/byb-software/TaolunBitmapUtils.git

        原创内容请勿随意转载!

  

阅读全文
2 0
原创粉丝点击