仿微信生成群组头像

作者: EsonJack | 来源:发表于2016-04-25 22:18 被阅读1934次

    背景

    项目中涉及到群组聊天的功能,由于前期项目比较紧张,群组头像并没有按照群成员头像去自动生成,即类似微信群组头像的效果,今天进行一下效果优化,效果如下:


    仿微信生成群组头像

    实现步骤

    • 获取群组成员头像
      可以采用开源图片加载库把图片加载至本地即可。说明一下,这里并没有在线去加载头像,而是考虑到在其它地方有单独加载头像的地方,所以头像都是从本地直接读取。
    • 生成群组头像
      先上代码

    public Bitmap generateGroupAvator(List<Bitmap> data) {
    switch (data == null ? 0 : data.size()) {
    case 1: {
    return data.get(0);
    }
    case 2: {
    int widthSize = mSize / 2;
    int heightSize = mSize / 2;
    Bitmap groupAvator = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(groupAvator);
    canvas.drawColor(0xFFe1e2e3);
    for (int i = 0; i < data.size(); i++) {
    Bitmap bitmap = data.get(i);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(widthSize * 1.0f / width, heightSize * 1.0f / height);
    Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    canvas.drawBitmap(newBit, i % 2 * widthSize, (mSize - heightSize) / 2 + i / 2 * heightSize, null);
    }
    canvas.save();
    canvas.restore();
    return groupAvator;
    }
    case 3: {
    int widthSize = mSize / 2;
    int heightSize = mSize / 2;
    Bitmap groupAvator = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(groupAvator);
    canvas.drawColor(0xFFe1e2e3);
    for (int i = 0; i < data.size(); i++) {
    Bitmap bitmap = data.get(i);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(widthSize * 1.0f / width, heightSize * 1.0f / height);
    Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    if (i == 0) {
    canvas.drawBitmap(newBit, (mSize - widthSize) / 2, 0, null);
    } else {
    canvas.drawBitmap(newBit, i / 2 * widthSize, heightSize, null);
    }
    }
    canvas.save();
    canvas.restore();
    return groupAvator;
    }
    case 4: {
    int widthSize = mSize / 2;
    int heightSize = mSize / 2;
    Bitmap groupAvator = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(groupAvator);
    canvas.drawColor(0xFFe1e2e3);
    for (int i = 0; i < data.size(); i++) {
    Bitmap bitmap = data.get(i);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(widthSize * 1.0f / width, heightSize * 1.0f / height);
    Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    canvas.drawBitmap(newBit, i % 2 * widthSize, i / 2 * heightSize, null);
    }
    canvas.save();
    canvas.restore();
    return groupAvator;
    }
    case 5: {
    int widthSize = mSize / 3;
    int heightSize = mSize / 3;
    Bitmap groupAvator = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(groupAvator);
    canvas.drawColor(0xFFe1e2e3);
    for (int i = 0; i < data.size(); i++) {
    Bitmap bitmap = data.get(i);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(widthSize * 1.0f / width, heightSize * 1.0f / height);
    Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    if (i < 2) {
    canvas.drawBitmap(newBit, widthSize / 2 + i * widthSize, heightSize / 2, null);
    } else {
    canvas.drawBitmap(newBit, i % 3 * widthSize, heightSize / 2 + heightSize, null);
    }
    }
    canvas.save();
    canvas.restore();
    return groupAvator;
    }
    case 6: {
    int widthSize = mSize / 3;
    int heightSize = mSize / 3;
    Bitmap groupAvator = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(groupAvator);
    canvas.drawColor(0xFFe1e2e3);
    for (int i = 0; i < data.size(); i++) {
    Bitmap bitmap = data.get(i);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(widthSize * 1.0f / width, heightSize * 1.0f / height);
    Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    canvas.drawBitmap(newBit, i % 3 * widthSize, heightSize / 2 + i / 3 * heightSize, null);
    }
    canvas.save();
    canvas.restore();
    return groupAvator;
    }
    case 7: {
    int widthSize = mSize / 3;
    int heightSize = mSize / 3;
    Bitmap groupAvator = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(groupAvator);
    canvas.drawColor(0xFFe1e2e3);
    for (int i = 0; i < data.size(); i++) {
    Bitmap bitmap = data.get(i);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(widthSize * 1.0f / width, heightSize * 1.0f / height);
    Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    if (i == 0) {
    canvas.drawBitmap(newBit, widthSize, 0, null);
    } else {
    canvas.drawBitmap(newBit, (i + 2) % 3 * widthSize, (i + 2) / 3 * heightSize, null);
    }
    }
    canvas.save();
    canvas.restore();
    return groupAvator;
    }
    case 8: {
    int widthSize = mSize / 3;
    int heightSize = mSize / 3;
    Bitmap groupAvator = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(groupAvator);
    canvas.drawColor(0xFFe1e2e3);
    for (int i = 0; i < data.size(); i++) {
    Bitmap bitmap = data.get(i);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(widthSize * 1.0f / width, heightSize * 1.0f / height);
    Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    if (i < 2) {
    canvas.drawBitmap(newBit, widthSize / 2 + i * widthSize, 0, null);
    } else {
    canvas.drawBitmap(newBit, (i + 1) % 3 * widthSize, (i + 1) / 3 * heightSize, null);
    }
    }
    canvas.save();
    canvas.restore();
    return groupAvator;
    }
    case 9: {
    int widthSize = mSize / 3;
    int heightSize = mSize / 3;
    Bitmap groupAvator = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(groupAvator);
    for (int i = 0; i < data.size(); i++) {
    Bitmap bitmap = data.get(i);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(widthSize * 1.0f / width, heightSize * 1.0f / height);
    Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    canvas.drawBitmap(newBit, i % 3 * widthSize, i / 3 * heightSize, null);
    }
    canvas.save();
    canvas.restore();
    return groupAvator;
    }
    default:
    break;
    }
    return null;
    }

    - 实现思路
    根据图片张数,计算出每张头像在群组头像中的尺寸,然后缩放头像,最后把缩放后的头像画到合适的位置即可。

    相关文章

      网友评论

      本文标题:仿微信生成群组头像

      本文链接:https://www.haomeiwen.com/subject/bqyalttx.html