android 仿微信群聊头像 合成图片

android 仿微信群聊头像 合成图片,微信中可以显示出群头像为多个用户的头像网格,这里讲方法已经封装好,

如果有记得点赞哦!!

热更新框架:https://github.com/jasonliyihang/speed_tools

网络优化工具集:https://github.com/woshiliyihang/pre-network

先看效果:
在这里插入图片描述
使用例子:

@Override
public void onClick(View v) {
    PNUtils.runThread(new Runnable() {
        @Override
        public void run() {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.cx_facebook_icon);
            Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.cx_google_icon);
            Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(), R.mipmap.cx_twitter);
            Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(), R.mipmap.cx_weixin_icon);
            List<Bitmap> bitmaps=new ArrayList<>();
            bitmaps.add(bitmap);
            bitmaps.add(bitmap1);
            bitmaps.add(bitmap3);
            bitmaps.add(bitmap4);
            final Bitmap bitmap2=PNUtils.createHeaderBitmap(bitmaps, 300, 300, 2, "#000000");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    imageView.setImageBitmap(bitmap2);
                }
            });
        }
    });
}

public static Bitmap createHeaderBitmap(List<Bitmap> bitmaps,int maxWidth,int maxHeight, int columnNums, String backgroundColor) {
    if (bitmaps.size()==0)
        return null;
    int itemWidth=maxWidth/columnNums;
    int itemHeight=maxHeight/columnNums;
    int rows=-1;
    Bitmap bitmap2 = Bitmap.createBitmap(maxWidth, maxHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas=new Canvas(bitmap2);
    canvas.drawColor(Color.parseColor(backgroundColor));
    for (int i=0; i<bitmaps.size(); i++) {
        int column=i%columnNums;
        if (column==0)
            rows++;
        canvas.drawBitmap(resizeBitmap(bitmaps.get(i), itemWidth), column*itemWidth, rows*itemHeight, null);
    }
    return bitmap2;
}


/**
 * 重新修改图片大小
 */
public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {
    float scaleWidth = ((float) newWidth) / bitmap.getWidth();
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleWidth);
    Bitmap bmpScale = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    return bmpScale;
}

原文地址:http://pincha.shop/search/blogInfo/2932

发布了187 篇原创文章 · 获赞 65 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/mhhyoucom/article/details/86630850