2016-03-08 49 views
1

的佈局我想建立一個相同的佈局,像Facebook中使用它的Android應用程序的照片。我想實現一個佈局如下圖所示:如何建立像Facebook的照片顯示在Android應用程序

我曾嘗試以下使用recyclerview來顯示照片:

rv = (RecyclerView) findViewById(R.id.rv); 
    final MyAdapter adapter = new MyAdapter(); 
    rv.setAdapter(adapter); 
    GridLayoutManager mLayoutManager = new GridLayoutManager(this, 2); 
    rv.setLayoutManager(mLayoutManager); 

    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
     @Override 
     public int getSpanSize(int position) { 
      if (adapter.getItemCount() == 1) { 
       return 2; 
      } else if (adapter.getItemCount() == 2) { 
       return 1; 
      } else if (adapter.getItemCount() == 3) { 
       if (position == 0) { 
        return 2; 
       } else { 
        return 1; 
       } 
      } else { 

       return 1; 

      } 


     } 
    }); 

現在我沒有寬度和圖像的高度,我有URL僅是圖片。 因此,如果我上傳2張圖像,我想檢查圖像是否爲縱向(高度大於寬度)或風景,並且需要相應地顯示圖像。

我用下面的代碼來獲得圖像的高度和寬度:

new AsyncTask<Void, Void, Void>() { 
    @Override 
    protected Void doInBackground(Void... params) { 
     if (Looper.myLooper()==null) 
      Looper.prepare(); 
     try {              listItemHolder.imageBitmap = Glide.with(mContext). 
                     load(mainImage).                  asBitmap().                 into(-1,-1). 
get(); 
} catch (final ExecutionException e) { 
Log.e(TAG, e.getMessage()); 
} catch (final InterruptedException e) { 
Log.e(TAG, e.getMessage()); 
} 
return null; 
} 
@Override 
protected void onPostExecute(Void dummy) { 
    } 
}.execute(); 

但代碼讓我的應用程序太慢。

任何人都可以在這裏有任何想法,我怎麼能做到這一點。

回答

1

你必須使用StaggeredGridLayoutManager

StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(this, 2); 
    rv.setLayoutManager(mLayoutManager); 

+0

我已經使用這個,我已經管理的照片列。 剩下的唯一東西就是設置照片的寬度和高度,並根據照片的寬度和高度設置跨度數。 –

+0

我正在使用gridlayoutmanager而不是staggeredgridlayoutmanager –

0

要獲得圖像的寬度和高度:

您的代碼使用滑行加載完整的圖片,使慢。

如果你只是想獲得尺寸,你可以使用位圖選項。

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 
int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 
String imageType = options.outMimeType; 

只是從流中解碼它。

檢查此Loading Large Bitmaps Efficiently

+0

我想先計算圖像url的寬度和高度,然後需要在imageview中加載。 我該怎麼做? –

+0

獲取http流,然後使用BtimapFractory.decodeStream(InputStream is)來獲取高度和寬度表單選項。 –

相關問題