Android GridView 위 챗 모멘트 이미지 모방

최근 프로젝트 는 다 중 그림 을 올 리 고 다 중 그림 을 표시 해 야 하 며,위 챗 모멘트 의 그림 처럼 규칙 적 으로 표시 해 야 합 니 다.
GridView 를 이용 하면 더 이상 어 울 리 지 않 습 니 다.GridView 는 그림 의 수량 을 동적 으로 불 러 올 수 있 고 규칙 적 입 니 다.다음은 자신의 생각 을 말씀 드 리 겠 습 니 다.
  • 1.네트워크 그림 가 져 오기
  • 2.gridview 초기 화,사용자 정의 어댑터
  • 3.그림 수량 에 따라 gridview 의 열 수 를 설정 합 니 다
  • 4.어댑터 업데이트
  • 아래 에 소스 코드 를 붙 여서 해석 을 해 드 리 도록 하 겠 습 니 다.
    첫째,먼저 GridView 의 item
    
    <com.view.SquareLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 
     <ImageView 
      android:id="@+id/item_grida_image" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:layout_margin="@dimen/tinyest_space"> 
     </ImageView> 
    </com.view.SquareLayout> 
    
    이곳 의 Square Layout 레이아웃 은 사용자 정의 입 니 다.자세 한 설명 을 드 리 겠 습 니 다.
    하위 항목 에는 정사각형 레이아웃 에 그림 이 새 겨 져 있 습 니 다.
    다음 사용자 정의 어댑터
    프로젝트 수요 가 다 르 기 때문에 자신 이 정의 하 는 어댑터 는 평소에 사용 하 는 것 과 다 르 기 때문에 원본 코드 를 붙 이지 않 습 니 다.대체로 그림 을 로 컬 에 다운로드 하고 Imageloader 로 불 러 옵 니 다.하지만 업로드 에 실패 한 것 과 새로 만 든 것 이 있어 서 다 릅 니 다.
    3.마지막 으로 사용 하 는 Activity 에서 설정
    
    noScrollgridview = (GridView) findViewById(R.id.noScrollgridview); 
      noScrollgridview.setNumColumns(3); //     3    
      //       adapter 
      picAdapter = new PictureAdapter(this, 1, appItem_file); 
      noScrollgridview.setAdapter(picAdapter); 
    
    
    
    //             
      int size = appItemFile.getFiles().split(",").length; 
      if (size==1){ 
       noScrollgridview.setNumColumns(1); 
      } 
      else if (size==2){ 
       noScrollgridview.setNumColumns(2); 
      } 
      else if (size>2){ 
       noScrollgridview.setNumColumns(3); 
      } 
      picAdapter.notifyDataSetChanged(); 
    
    
    기본적으로 GridView 의 열 수 를 3 으로 설정 하고 그림 의 수량 에 따라 동적 으로 열 수 를 설정 합 니 다.
    마지막 으로 Square Layout 의 소스 코드 를 붙 여 분석 해 보 겠 습 니 다.
    
    /** 
     *      
     */ 
    public class SquareLayout extends RelativeLayout { 
     public SquareLayout(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
     } 
     
     public SquareLayout(Context context, AttributeSet attrs) { 
      super(context, attrs); 
     } 
     
     public SquareLayout(Context context) { 
      super(context); 
     } 
     
     @SuppressWarnings("unused") 
     @Override 
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
      // For simple implementation, or internal size is always 0. 
      // We depend on the container to specify the layout size of 
      // our view. We can't really know what it is since we will be 
      // adding and removing different arbitrary views and do not 
      // want the layout to change as this happens. 
      setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), 
        getDefaultSize(0, heightMeasureSpec)); 
     
      // Children are just made to fill our space. 
      int childWidthSize = getMeasuredWidth(); 
      int childHeightSize = getMeasuredHeight(); 
      //         
      heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec( 
        childWidthSize, MeasureSpec.EXACTLY); 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 
    
    여 기 는 주로 onMeasure()방법 을 다시 썼 습 니 다.높이 와 폭 을 설 치 했 습 니 다.주의해 야 할 것 은 Square Layout 를 사용 할 때 높이 와 폭 을 모두 match 로 설정 해 야 합 니 다.parent。이렇게 하면 GridView 의 모든 항목 을 채 울 수 있 습 니 다.
    다음은 스티커 를 보 여 드 리 겠 습 니 다.

    ImgeView 의 scaleType 속성 은 FitXY 를 설정 하면 사각형 레이아웃 이 가득 하고 center 가 가운데 로 표 시 됩 니 다.
    자세히 말씀 해 주세요.
    1)center:원본 그림 의 크기 를 유지 하고 ImageView 의 중심 에 표시 합 니 다.원본 그림 의 size 가 ImageView 의 size 보다 크 면 부분 재단 처 리 를 초과 합 니 다.
    2)centerCrop:이미지 뷰 전 체 를 채 우 는 것 을 목적 으로 이미지 뷰 의 중심 을 이미지 뷰 의 중심 에 맞 추 는 등 비례 를 확대 하여 이미지 뷰 를 채 울 때 까지(이미지 뷰 의 너비 와 높이 를 모두 채 워 야 한 다 는 뜻)원 도 는 이미지 뷰 를 초과 한 부분 을 재단 처리 합 니 다.
    3)center Inside:원 그림 을 완전히 표시 하 는 것 을 목적 으로 그림 의 내용 을 전체 가운데 로 표시 하고 원 그림 의 size 너비(높이)를 비례 에 따라 축소 하거나 ImageView 의 너비(높이)보다 작 게 표시 합 니 다.원본 그림 의 size 자체 가 ImageView 의 size 보다 작 으 면 원본 그림 의 size 는 아무런 처리 도 하지 않 고 중앙 에 ImageView 에 표 시 됩 니 다.
    4)matrix:원본 그림 의 크기 를 바 꾸 지 않 고 ImageView 의 왼쪽 상단 부터 원본 그림 을 그립 니 다.원본 그림 이 ImageView 를 초과 한 부분 을 재단 처리 합 니 다.
    5)fitCenter:원본 그림 을 비례 에 따라 ImageView 의 ImageView 높이 로 확대 하거나 축소 하여 중간 에 표시 합 니 다.
    6)fitEnd:원본 그림 을 비례 에 따라 ImageView 의 높이 로 확대(축소)하여 ImageView 의 아래 부분 에 표시 합 니 다.
    7)fitStart:원본 그림 을 비례 에 따라 ImageView 의 높이 로 확대(축소)하여 ImageView 의 상단 위치 에 표시 합 니 다.
    8)fitXY:원 도 를 지 정 된 크기 로 View 에 표시 하고 그림 을 늘 려 서 원 비례 를 유지 하지 않 고 ImageView 를 채 웁 니 다.
    본 고 는 이미《Android 위 챗 개발 튜 토리 얼 집계》로 정리 되 었 으 니,여러분 의 학습 과 독 서 를 환영 합 니 다.
    이상 은 본문의 전체 내용 이 므 로 여러분 의 학습 에 도움 이 되 기 를 바 랍 니 다.

    좋은 웹페이지 즐겨찾기