2014-02-11 58 views
3

我試圖將圖像設置爲圖像imageview從外部存儲器的視圖,我完成了,但事情是,它將整個圖像設置爲imageview,我只想設置從該圖像中選擇正方形區域。只是生活Facebook提供功能設置配置文件圖片.. 任何人都可以幫助我做到這一點? 下面就是我想要做的樣品..如何將圖像的選定區域設置爲ImageView

enter image description here

+0

你的意思是你要裁剪圖像? –

+0

將此用於您的相機意圖 - photoPickerIntent.putExtra(「crop」,「true」); –

+1

我不想修改原始圖像,我只是想將選定區域設置爲imageview – Akshay

回答

2

事情是這樣的:

public static Bitmap cropBitmapToSquare(Bitmap bmp) { 

    System.gc(); 
    Bitmap result = null; 
    int height = bmp.getHeight(); 
    int width = bmp.getWidth(); 
    if (height <= width) { 
     result = Bitmap.createBitmap(bmp, (width - height)/2, 0, height, 
       height); 
    } else { 
     result = Bitmap.createBitmap(bmp, 0, (height - width)/2, width, 
       width); 
    } 
    return result; 
} 

這裏是作物活性的樣品:

http://khurramitdeveloper.blogspot.ru/2013/07/capture-or-select-from-gallery-and-crop.html

+0

感謝您的鏈接,,希望它能爲我工作.. – Akshay

+0

非常感謝你的鏈接,它爲我工作.. – Akshay

0

實際我上面關於setImageMatrix的評論不是最好的解決方案,試試這個自定義Drawable(不需要同意美食任何臨時位圖):

class CropDrawable extends BitmapDrawable { 

    private Rect mSrc; 
    private RectF mDst; 

    public CropDrawable(Bitmap b, int left, int top, int right, int bottom) { 
     super(b); 
     mSrc = new Rect(left, top, right, bottom); 
     mDst = new RectF(0, 0, right - left, bottom - top); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     canvas.drawBitmap(getBitmap(), mSrc, mDst, null); 
    } 

    @Override 
    public int getIntrinsicWidth() { 
     return mSrc.width(); 
    } 

    @Override 
    public int getIntrinsicHeight() { 
     return mSrc.height(); 
    } 
} 

和測試代碼:

ImageView iv = new ImageView(this); 
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.test); 
    Drawable d = new CropDrawable(b, 150, 100, 180, 130); 
    iv.setImageDrawable(d); 
    setContentView(iv); 
相關問題