2014-06-18 125 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; 
    } 

}