안 드 로 이 드 모방 위 챗 단체 채 팅 프로필 사진

작업 중 에 모조 못 군 프로필 사진 을 실현 해 야 하 는 기능 중 하 나 는 바로 개인의 프로필 사진 을 조합 해서 보 여 주 는 것 이다.시장 에 있 는 앱 을 보 니 위 챗 의 단체 채 팅 프로필 사진 이 조 합 된 것 같다.QQ 의 프로필 사진 이 아니 라 다른 것 도 없 는 것 같다.
어떻게 이 루어 졌 는 지 공유 해 주세요.우선 효과 도 를 살 펴 보 겠 습 니 다.

자,구체 적 으로 어떻게 이 루어 졌 는 지 말씀 드 리 겠 습 니 다.
사고의 방향 을 실현 하 다.
먼저 Bitmap 그림(로 컬,네트워크)을 가 져 옵 니 다4.567917.2.지정 한 크기 의 미리 보기 그림 을 만 듭 니 다.4.567918.
조합 Bitmap 사진간단 합 니 다.로 컬 그림 은 로 컬 에서 읽 어야 합 니 다.네트워크 그림 이 라면 URL 에 따라 bitmap 를 조합 할 수 있 습 니 다.
구체 적 실현 과정
1.레이아웃 파일:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:gravity="center"
  android:orientation="vertical"
  android:background="#987"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
  <TextView
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="1dp"/>
  <ImageView
    android:src="@drawable/j"
    android:id="@+id/iv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <TextView
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="1dp"/>
  <ImageView
    android:src="@drawable/j"
    android:id="@+id/iv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <TextView
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="1dp"/>
  <ImageView
    android:src="@drawable/j"
    android:id="@+id/iv3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <TextView
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="1dp"/>
  <ImageView
    android:src="@drawable/j"
    android:id="@+id/iv4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <TextView
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="1dp"/>

</LinearLayout>

그림 을 표시 하기 위해 이미지 뷰 컨트롤 네 개
2.Bitmap 가 져 오기,그림 속성 설정

/**
   *         
   * @param count
   * @return
   */
  private List<BitmapBean> getBitmapEntitys(int count) {
    List<BitmapBean> mList = new ArrayList<>();
    String value = PropertiesUtil.readData(this, String.valueOf(count),
        R.raw.data);
    String[] arr1 = value.split(";");
    int length = arr1.length;
    for (int i = 0; i < length; i++) {
      String content = arr1[i];
      String[] arr2 = content.split(",");
      BitmapBean entity = null;
      for (int j = 0; j < arr2.length; j++) {
        entity = new BitmapBean();
        entity.setX(Float.valueOf(arr2[0]));
        entity.setY(Float.valueOf(arr2[1]));
        entity.setWidth(Float.valueOf(arr2[2]));
        entity.setHeight(Float.valueOf(arr2[3]));
      }
      mList.add(entity);
    }
    return mList;
  }
3.압축 그림 을 만 듭 니 다.여 기 는 Thumbnail Utils 의 extractThumbnail()방법 을 사 용 했 습 니 다.매개 변 수 는 bitmap,width,height 입 니 다.

/**
   *      
   */
  private void initData(){
    /*        */
    bitmapBeans1 = getBitmapEntitys(1);
    bitmapBeans2 = getBitmapEntitys(2);
    bitmapBeans3 = getBitmapEntitys(3);
    bitmapBeans4 = getBitmapEntitys(4);
    /*bitmap   */
    Bitmap[] bitmaps1 = {
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
        getResources(), R.drawable.j), (int) bitmapBeans1
        .get(0).getWidth(), (int) bitmapBeans1.get(0).getWidth())};
    Bitmap[] bitmaps2 = {
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
        getResources(), R.drawable.j), (int) bitmapBeans2
        .get(0).getWidth(), (int) bitmapBeans2.get(0).getWidth()),
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
        getResources(), R.drawable.j), (int) bitmapBeans2
        .get(0).getWidth(), (int) bitmapBeans2.get(0).getWidth())};
    Bitmap[] bitmaps3 = {
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
        getResources(), R.drawable.j), (int) bitmapBeans3
        .get(0).getWidth(), (int) bitmapBeans3.get(0).getWidth()),
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
        getResources(), R.drawable.j), (int) bitmapBeans3
        .get(0).getWidth(), (int) bitmapBeans3.get(0).getWidth()),
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
            getResources(), R.drawable.j), (int) bitmapBeans3
            .get(0).getWidth(), (int) bitmapBeans3.get(0).getWidth())};
    Bitmap[] bitmaps4 = {
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
            getResources(), R.drawable.j), (int) bitmapBeans4
            .get(0).getWidth(), (int) bitmapBeans4.get(0).getWidth()),
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
            getResources(), R.drawable.j), (int) bitmapBeans4
            .get(0).getWidth(), (int) bitmapBeans4.get(0).getWidth()),
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
            getResources(), R.drawable.j), (int) bitmapBeans4
            .get(0).getWidth(), (int) bitmapBeans4.get(0).getWidth()),
        ThumbnailUtils.extractThumbnail(BitmapUtils.getScaleBitmap(
            getResources(), R.drawable.j), (int) bitmapBeans4
            .get(0).getWidth(), (int) bitmapBeans4.get(0).getWidth())};
 }
4.비트 맵 그림 조합(즉,우리 그림 을 캔버스 로 그 리 는 것)

/**
   *        bitmap
   * @param mEntityList
   * @param bitmaps
   * @return
   */
  public static Bitmap getCombineBitmaps(List<BitmapBean> mEntityList,
                      Bitmap... bitmaps) {
    Bitmap newBitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
    for (int i = 0; i < mEntityList.size(); i++) {
      bitmaps[i] = GetRoundedCornerBitmap(bitmaps[i]);
      newBitmap = mixtureBitmap(newBitmap, bitmaps[i], new PointF(
          mEntityList.get(i).getX(), mEntityList.get(i).getY()));
    }
    return newBitmap;
  }
여기 제 가 그림 을 보기 위해 서 동 그 랗 게 만 들 었 어 요.

/**
   *      bitmap
   * @param bitmap
   * @return
   */
  public 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()));
      final float roundPx = 50;
      paint.setAntiAlias(true);
      canvas.drawARGB(0, 0, 0, 0);
      paint.setColor(Color.BLACK);
      canvas.drawRoundRect(rectF, roundPx, roundPx, 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;
    }
  }

마지막 그림

 /**
   *  bitmap
   * @param first
   * @param second
   * @param fromPoint
   * @return
   */
  public static Bitmap mixtureBitmap(Bitmap first, Bitmap second,
                    PointF fromPoint) {
    if (first == null || second == null || fromPoint == null) {
      return null;
    }
    Bitmap newBitmap = Bitmap.createBitmap(first.getWidth(),
        first.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas cv = new Canvas(newBitmap);
    cv.drawBitmap(first, 0, 0, null);
    cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);
    cv.save(Canvas.ALL_SAVE_FLAG); //      
    cv.restore();
    return newBitmap;
  }
이렇게 하면 위 챗 그룹 채 팅 이미지 의 효 과 를 간단하게 실현 할 수 있 습 니 다.물론 그림 을 처리 해 야 합 니 다.OOM 을 방지 하고 View 구성 요소 로 사용자 정의 할 수 있 습 니 다.시간 이 있 으 면 이 를 실현 할 수 있 습 니 다.
마지막 으로 작은 프로젝트 에서 이 루어 진 효 과 를 보 여 드 리 겠 습 니 다.다 를 것 이 없습니다.데이터 소스 가 다 르 지 않 고 인터넷 에서 얻 은 것 입 니 다.
여기에 그림 설명 을 쓰 십시오:

이상 은 본문의 전체 내용 이 므 로 여러분 의 학습 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기