2011-04-18 30 views
4

我希望有一個android圖庫,將託管不同長寬比的圖像。我想要的是類似於圖庫中圖像的CENTER_CROP。但是,當我將圖像縮放類型設置爲此時,圖像溢出圖庫圖像邊框。android畫廊與任意長寬比

當然,FIT_XY會導致壓扁/變平的圖像。 CENTER導致畫廊圖像邊框內的水平或垂直黑色空間。

任何想法如何做到這一點?我可以找到的每個示例都使用FIT_XY和固定大小的圖像。我想我可以自己裁剪圖像,但我寧願不要。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView iv = (ImageView) convertView; 
    if (iv == null) { 
     iv = new ImageView(context); 
     iv.setScaleType(ImageView.ScaleType.FIT_XY); 
     iv.setBackgroundResource(galleryItemBackground); 
     iv.setLayoutParams(new Gallery.LayoutParams(200, 200)); 
    } 

    InputStream is; 
    try { 
     is = getInputStream(position); 
    } catch (IOException ioe) { 
     // TODO? 
     throw new RuntimeException(ioe); 
    } 
    Bitmap bm = BitmapFactory.decodeStream(is); 
    iv.setImageBitmap(bm); 



    /* 
    * if (bitmaps[position] != null) { bitmaps[position].recycle(); 
    * bitmaps[position] = null; } bitmaps[position] = bm; 
    */ 

    return iv; 
} 

回答

0

,因爲沒有人回答,我結束了剛剛修剪位爲方自己,因爲,

Bitmap bm = BitmapFactory.decodeStream(is); 

// make it square 

int w = bm.getWidth(); 
int h = bm.getHeight(); 
if (w > h) { 
    // trim width 
    bm = Bitmap.createBitmap(bm, (w - h)/2, 0, h, h); 
} else { 
    bm = Bitmap.createBitmap(bm, 0, (h - w)/2, w, w); 
} 
+0

此解決方案對我無效,因爲我需要整個圖像以便能夠在容器ImageView中滾動它。 – 2011-11-03 15:32:04

0

我有同樣的問題,因爲你看着ScaleType文檔,我發現它可以例如:使用ScaleType.MATRIX,例如:

int w = 1000; 
    int h = 1000; 

    Paint p = new Paint(); 
    p.setShader(new LinearGradient(0, 0, w, h, Color.BLACK, Color.RED, Shader.TileMode.CLAMP)); 

    Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
    Canvas g = new Canvas(bmp);  
    g.drawRect(new Rect(0, 0, w, h), p); 

    ImageView i3 = new ImageView(context); 
    i3.setImageBitmap(bmp); 
    i3.setScaleType(ScaleType.MATRIX); 

    int viewWidth = 300; 
    int viewHeight = 300; 

    Matrix matrix = new Matrix(); 
    matrix.postScale(bmp.getWidth()/viewWidth, bmp.getHeight()/viewHeight, bmp.getWidth()/2, bmp.getHeight()/2); 
    i3.setImageMatrix(matrix); 

    LayoutParams lp2 = new LayoutParams(viewWidth, viewHeight); 
    lp2.leftMargin = 100; 
    lp2.topMargin = 400; 
    lp2.gravity = 0; 

    this.addView(i3, lp2); 

雖然這個解決方案使事情變得複雜一點。如果你想滾動和縮放ImageView,你也需要使用矩陣縮放。所以我很想知道任何可能的選擇。

0

對於這種類型的任務,我使用這個簡單的類。它適合高度或寬度適當縮放圖像(這取決於哪個是較小的尺寸)。在此操作之後,它將圖像置於ImageView邊界中心。

public class FixedCenterCrop extends ImageView { 


    public FixedCenterCrop(Context context) { 
     super(context); 
     setScaleType(ScaleType.MATRIX); 
    } 

    public FixedCenterCrop(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setScaleType(ScaleType.MATRIX); 
    } 

    public FixedCenterCrop(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     setScaleType(ScaleType.MATRIX); 
    } 


    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 
     recomputeImgMatrix(); 
    } 

    @Override 
    protected boolean setFrame(int l, int t, int r, int b) { 
     recomputeImgMatrix(); 
     return super.setFrame(l, t, r, b); 
    } 

    private void recomputeImgMatrix() { 
     final Matrix matrix = getImageMatrix(); 

     float scale; 
     final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight(); 
     final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom(); 
     final int drawableWidth = getDrawable().getIntrinsicWidth(); 
     final int drawableHeight = getDrawable().getIntrinsicHeight(); 

     if (drawableWidth * viewHeight > drawableHeight * viewWidth) { 
      scale = (float) viewHeight/(float) drawableHeight; 
     } else { 
      scale = (float) viewWidth/(float) drawableWidth; 
     } 

     matrix.setScale(scale, scale); 
     matrix.postTranslate((viewWidth - drawableWidth * scale)/2, (viewHeight - drawableHeight*scale)/2); 
     setImageMatrix(matrix); 
    } 
}