2014-06-18 33 views
1

我想從原始圖像裁剪一個圓形圖像。我正在使用Picasso庫進行圖像顯示。試過http://yasiradnan.com/circle-transformation-with-android-image-downloading-and-caching-library-picasso/,但它只是將整個圖像轉換成一個圓形,所以圖像變形了。我不想轉換圖像,我只想用圓形裁剪圖像。將圖像裁剪成圓形

+0

我不知道如何使用畢加索,但是這可能會幫助您: http://stackoverflow.com/questions/12944275/crop-image-as-circle-in-android – raybaybay

回答

4

要完成您想要做的事情,您可以繼承ImageView並使其實現PicassoTarget接口。加載位圖時,只需使用將位圖居中放置爲正方形的方法,然後將圖像着色爲圓形。例如:

public class ImageViewTarget extends ImageView implements Target { 

    //constructors 

@Override 
public void onBitmapFailed(Drawable drawable) { 
     //TODO 
} 

@Override 
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom loadFrom) { 
     bitmap = cropCircle(bitmap.isMutable() ? bitmap : bitmap.copy(Config.ARGB_8888, true)); 
      setImageBitmap(bitmap); 
} 

@Override 
public void onPrepareLoad(Drawable arg0) { 
    //TODO 
} 


public Bitmap cropCricle(Bitmap bm){ 

    int width = bm.getWidth(); 
    int height = bm.getHeight(); 

    Bitmap cropped_bitmap; 

    /* Crop the bitmap so it'll display well as a circle. */ 
    if (width > height) { 
     cropped_bitmap = Bitmap.createBitmap(bm, 
       (width/2) - (height/2), 0, height, height); 
    } else { 
     cropped_bitmap = Bitmap.createBitmap(bm, 0, (height/2) 
       - (width/2), width, width); 
    } 

    BitmapShader shader = new BitmapShader(cropped_bitmap, TileMode.CLAMP, TileMode.CLAMP); 

    Paint paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setShader(shader); 

    height = cropped_bitmap.getHeight(); 
    width = cropped_bitmap.getWidth(); 

    Bitmap mCanvasBitmap = Bitmap.createBitmap(width, height, 
      Bitmap.Config.ARGB_8888); 

    Canvas canvas = new Canvas(mCanvasBitmap); 
    canvas.drawCircle(width/2, height/2, width/2, paint); 

    return mCanvasBitmap; 
} 

} 

有可能是一個更好的,爲什麼來處理cropCircle(Bitmap bitmap);方法,但上述作品有時優化/蓄客的。

2

您可以使用下面的代碼獲得圓角位圖...這可能對你有幫助....

private Bitmap getRoundedCroppedImage(Bitmap bmp) { 
     int widthLight = bmp.getWidth(); 
     int heightLight = bmp.getHeight(); 

     Bitmap output = Bitmap.createBitmap(widthLight, heightLight,Config.ARGB_8888); 

     Canvas canvas = new Canvas(output); 
     Paint paint = new Paint(); 
     paint.setFlags(Paint.ANTI_ALIAS_FLAG); 

     RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight)); 

     canvas.drawRoundRect(rectF, widthLight/2 ,heightLight/2,paint); 

     Paint paintImage = new Paint(); 
     paintImage.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP)); 
     canvas.drawBitmap(bmp, 0, 0, paintImage); 

     return output; 
    } 

謝謝...

+0

謝謝...親愛的...這就是減少我的努力... –

0

有一些決定,我做了基於在that answer 您可以自定義的ImageView沒有庫

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.PorterDuff.Mode; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.Rect; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

public class CircleImageView extends ImageView { 

    public CircleImageView(Context ctx, AttributeSet attrs) { 
     super(ctx, attrs); 
    } 

    @Override 
    public void setImageDrawable(Drawable aDrawable) { 
     Bitmap bitmap=getCircleCroppedBitmap(((BitmapDrawable) aDrawable).getBitmap()); 
     super.setImageDrawable(new BitmapDrawable(getResources(),bitmap)); 
    } 

    private static Bitmap getCircleCroppedBitmap(Bitmap bitmap) { 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
       bitmap.getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
     canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, 
       bitmap.getWidth()/2, paint); 
     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 
     //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false); 
     //return _bmp; 
     return output; 
    } 

}