2015-10-15 45 views
1

我想創建一個照片庫包含兩列創建Android畫廊,像這裏顯示我想用不同大小的列

enter image description here

圖像中的圖像必須被二次採樣,也讓她的方面比。 這是一個子樣本圖像的自定義轉換:

public ImageCustomTransformation(Context context, int maxWidth, int maxHeight) { 
    super(context); 
    this.maxWidth = maxWidth; 
    this.maxHeight = maxHeight; 
} 

@Override 
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 

    int targetWidth, targetHeight; 
    double aspectRatio; 

    if (toTransform.getWidth() > toTransform.getHeight()) { 
     targetWidth = maxWidth; 
     aspectRatio = (double) toTransform.getHeight()/(double) toTransform.getWidth(); 
     targetHeight = (int) (targetWidth * aspectRatio); 
    } else { 
     targetHeight = maxHeight; 
     aspectRatio = (double) toTransform.getWidth()/(double) toTransform.getHeight(); 
     targetWidth = (int) (targetHeight * aspectRatio); 
    } 

    Bitmap result = Bitmap.createScaledBitmap(toTransform, targetWidth, targetHeight, false); 
    if (result != toTransform) { 
     toTransform.recycle(); 
    } 
    return result; 
} 

這裏是爲GridView的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gallery" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@android:color/darker_gray"> 

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/grid_view_photos" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/set_photo_button" 
    android:layout_below="@+id/relativeLayout2" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" 
    android:numColumns="2" 
    android:horizontalSpacing="10dp" 
    android:verticalSpacing="10dp" /> 

<Button 
    android:id="@+id/set_photo_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="Upload Photo" /> 
</RelativeLayout> 

這裏是爲GridView的項目:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/and`enter code here`roid" 
xmlns:cardview="http://schemas.android.com/apk/res-auto" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margin="5dp" 
android:layout_marginBottom="10dp" 
android:layout_marginLeft="10dp" 
android:layout_marginRight="10dp" 
android:layout_marginTop="10dp" 
android:background="@android:color/white" 
cardview:cardCornerRadius="5dp"> 

<ImageView 
    android:id="@+id/image_view_gallery" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_marginTop="5dp"></ImageView> 
</android.support.v7.widget.CardView> 

要加載我使用Glide Library的圖像,然後將上下文和維度傳遞給二次採樣(變換)圖像。結果不是我想象的那個。 任何人都可以幫助我弄清楚如何從上面的示例圖像完成佈局? 謝謝!

回答

1

尋找AsymmetricGridView或QuiltViewLibrary,這些例子可能會有用。