2011-10-07 55 views
3

我有一個橫向模式的圖庫,帶有很多帶有圖像適配器的圖像。我想圖像拉伸到整個屏幕水平,並保持縱橫比和拉伸儘可能多的垂直..
我試圖使用FIT_XY,它不適合水平,但它不保持比例垂直(圖像變成粉碎)
我該怎麼做?這是自定義圖像適配器:「ScaleType:FIT_XY」不會拉伸圖庫中的圖像

public class ImageAdapter extends BaseAdapter { 
int mGalleryItemBackground; 
int counter = 0; 

private final Context mContext; 

public String[] mImageIds; 

public ImageAdapter(Context c) { 
    mContext = c; 
    TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1); 
    mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 
    a.recycle(); 
} 

public void insert(String string) { 
    mImageIds[counter] = string; 
    counter++; 
} 

@Override 
public int getCount() { 
    return mImageIds.length; 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView i = new ImageView(mContext); 

    i.setImageBitmap(BitmapFactory.decodeFile(mImageIds[position])); 
    i.setLayoutParams(new Gallery.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT)); 
    i.setScaleType(ImageView.ScaleType.MATRIX); 
    i.setBackgroundResource(mGalleryItemBackground); 

    return i; 
} 

}

回答

3

當我做這在Android中我使用一個合適的比例(fitCenter,fitStart,fitEnd)連同調整視圖界定的XML屬性將如

android:scaleType="fitCenter" android:adjustViewBounds="true" 
+0

現在的形象不舒展,任何其他的想法? – Omar

1

你可以計算寬度和高度的像素,並將它們發送到setLayoutParams

int width = getWindowManager().getDefaultDisplay().getWidth(); 
int height = width * bitmapHeight/bitmapWidth; 
imageView.setLayoutParams(new Gallery.LayoutParams(width, height)); 
imageView.setPadding(6, 0, 6, 0); // set this to what you like 
// remove this line: 
//imageView.setScaleType(ImageView.ScaleType.MATRIX); 

設置規模類型不會做任何事情(除了FitXY將水平延伸到邊界,但離開就像你提到的那樣,身高就像別的東西一樣)。另外FILL_PARENT似乎沒有按預期工作。其他佈局似乎影響位圖大小(或者在我的情況下,可繪製)。

在您的佈局xml文件,你可能需要設置FILL_PARENT:

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

這爲我下載和流式傳輸到一個可繪製,所以我認爲它會爲你的位圖工作。