Android 仿釘釘群組頭像 生成多圖片結合頭像 (圖片個數小於等於4)
阿新 • • 發佈:2018-11-20
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; } }