Android 仿钉钉群组头像 生成多图片结合头像 (图片个数小于等于4)

1:因为本文章中用到的图片显示框架是Glide,所以先要导入

//图片显示
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'jp.wasabeef:glide-transformations:2.0.1'

2:根据Glide写出以下方法

 /**
     * 获取网络图片的Bitmap
     *
     * @param activity
     * @param uri
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static Bitmap getHttpBitmap(Context activity, String uri) throws ExecutionException, InterruptedException {
        return Glide.with(activity)
                .load(uri)
                .asBitmap()
                .into(1024, 1024)
                .get();
    }
 /**
     * 圆形加载图片
     * 加载 bitmap
     *
     * @param imageAware
     * @param bitmap
     */
    public static void loadCropCircle(ImageView imageAware, Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        Glide.with(AICashApplication.getApplication())
                .load(bytes)
                .placeholder(R.mipmap.pre) //加载中
                .error(R.mipmap.tubiao_70)//加载失败
                .fallback(R.mipmap.pre)//加载为空
                .priority(Priority.HIGH)//加载的优先等级
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)//缓存类型
                .bitmapTransform(new CropCircleTransformation(AICashApplication.getApplication()))
                .into(imageAware);
    }

3:重写Imageview

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.util.AttributeSet;

import com.xvli.aicash.AICashApplication;
import com.xvli.aicash.R;
import com.xvli.aicash.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @param
 * @Comments : 生成多图片结合头像    (图片个数小于等于4)
 * @Author : Lampo
 * @CreateDate : 2018/11/16 14:05
 * @ModifiedBy : Lampo
 * @ModifiedDate : 2018/11/16 14:05
 * @Modified :
 */

public class PuzzleImageView extends android.support.v7.widget.AppCompatImageView {
    private final int marginWhiteWidth = 3; // 中间白色宽度
    private int width, height;

    public PuzzleImageView(Context context) {
        super(context);
    }

    public PuzzleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 设置头像 多个
     *
     * @param list
     */
    public void setImageBackGround(List<String> list) {
        this.width = this.getLayoutParams().width;
        this.height = this.getLayoutParams().height;
        MyAsyncTask asyncTask = new MyAsyncTask();
        if (list.size() < 4) {
            asyncTask.execute(list);
        } else {
            List<String> newList = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                newList.add(list.get(i));
            }
            asyncTask.execute(newList);
        }
    }

    /**
     * 设置头像 单个
     *
     * @param uri
     */
    public void setImageBackGround(String uri) {
        this.width = this.getLayoutParams().width;
        this.height = this.getLayoutParams().height;
        List<String> list = new ArrayList<>();
        list.add(uri);
        MyAsyncTask asyncTask = new MyAsyncTask();
        asyncTask.execute(list);
    }

    /**
     * 异步线程 获取组合bitmap 并且设置
     */
    class MyAsyncTask extends AsyncTask<List<String>, Integer, List<Bitmap>> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected List<Bitmap> doInBackground(List<String>[] lists) {
            List<Bitmap> bitmapList = new ArrayList<>();
            for (final String uri : lists[0]) {
                try {
                    Bitmap bitmap = ImageLoader.getHttpBitmap(AICashApplication.getApplication(), uri);
                    bitmapList.add(bitmap);
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return bitmapList;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
            setImageResource(R.mipmap.tubiao_70);
        }

        @Override
        protected void onPostExecute(List<Bitmap> bitmaps) {
            super.onPostExecute(bitmaps);
            Bitmap bitmap = getAvatar(bitmaps, width, height);
            ImageLoader.loadCropCircle(PuzzleImageView.this, bitmap);
        }
    }

    /**
     * 生成群组头像
     *
     * @param bitmapList
     * @param width
     * @param height
     * @return
     */
    private Bitmap getAvatar(List<Bitmap> bitmapList, int width, int height) {
        final Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        final int listSize = bitmapList.size();
        switch (listSize) {
            case 1:
                Bitmap _p11 = Bitmap.createScaledBitmap(bitmapList.get(0), width, height, false);
                canvas.drawBitmap(_p11, 0, 0, null);
                break;
            case 2:
                // 比例缩放
                Bitmap _p21 = Bitmap.createScaledBitmap(bitmapList.get(0), width, height, false);
                Bitmap _p22 = Bitmap.createScaledBitmap(bitmapList.get(1), width, height, false);
                // 裁取中间部分(从x点裁取置顶距离)
                Bitmap __p21 = Bitmap.createBitmap(_p21, width / 4 + marginWhiteWidth / 2, 0, width / 2 - marginWhiteWidth / 2, height); // 中间留有1px白条
                Bitmap __p22 = Bitmap.createBitmap(_p22, width / 4 + marginWhiteWidth / 2, 0, width / 2 - marginWhiteWidth / 2, height); // 中间留有1px白条
                // 绘图
                canvas.drawBitmap(__p21, 0, 0, null);
                canvas.drawBitmap(__p22, width / 2 + marginWhiteWidth / 2, 0, null);
                break;
            case 3:
                // 1-图1
                Bitmap _p31 = Bitmap.createScaledBitmap(bitmapList.get(0), width, height, false);
                Bitmap __p31 = Bitmap.createBitmap(_p31, width / 4 + marginWhiteWidth / 2, 0, width / 2 - marginWhiteWidth / 2, height);
                // 2-图2,3
                Bitmap _p32 = Bitmap.createScaledBitmap(bitmapList.get(1), width / 2 - marginWhiteWidth / 2, height / 2 - marginWhiteWidth / 2, false);
                Bitmap _p33 = Bitmap.createScaledBitmap(bitmapList.get(2), width / 2 - marginWhiteWidth / 2, height / 2 - marginWhiteWidth / 2, false);
                // 3-拼接
                canvas.drawBitmap(__p31, 0, 0, null);
                canvas.drawBitmap(_p32, width / 2 + marginWhiteWidth / 2, 0, null);
                canvas.drawBitmap(_p33, width / 2 + marginWhiteWidth / 2, height / 2 + marginWhiteWidth / 2, null);
                break;
            case 4:
                // 比例缩放
                Bitmap _p41 = Bitmap.createScaledBitmap(bitmapList.get(0), width / 2 - marginWhiteWidth / 2, height / 2 - marginWhiteWidth / 2, false);
                Bitmap _p42 = Bitmap.createScaledBitmap(bitmapList.get(1), width / 2 - marginWhiteWidth / 2, height / 2 - marginWhiteWidth / 2, false);
                Bitmap _p43 = Bitmap.createScaledBitmap(bitmapList.get(2), width / 2 - marginWhiteWidth / 2, height / 2 - marginWhiteWidth / 2, false);
                Bitmap _p44 = Bitmap.createScaledBitmap(bitmapList.get(3), width / 2 - marginWhiteWidth / 2, height / 2 - marginWhiteWidth / 2, false);
                // 多图拼接
                canvas.drawBitmap(_p41, 0, 0, null);
                canvas.drawBitmap(_p42, width / 2 + marginWhiteWidth / 2, 0, null);
                canvas.drawBitmap(_p43, 0, height / 2 + marginWhiteWidth / 2, null);
                canvas.drawBitmap(_p44, width / 2 + marginWhiteWidth / 2, height / 2 + marginWhiteWidth / 2, null);
                break;
            default:
                break;
        }
        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/u014434239/article/details/84245259
今日推荐