将图像裁剪成圆圈

时间:2014-06-18 21:06:27

标签: android image image-processing picasso

我想从原始图像裁剪圆形图像。我正在使用Picasso库进行图像显示。尝试了http://yasiradnan.com/circle-transformation-with-android-image-downloading-and-caching-library-picasso/,但它只是将完整的图像转换为圆形图像,因此图像变形了。我不想转换图像,我只想裁剪圆形图像。

3 个答案:

答案 0 :(得分:4)

要完成您尝试执行的操作,您可以继承ImageView并使其实现Picasso的{​​{1}}界面。加载位图时,只需使用将位图中心切割为正方形的方法,然后将图像阴影化为圆形。例如:

Target

可能有一个更好的原因来处理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; } } 方法,但上述工作有时可以优化/构建。

答案 1 :(得分: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;
    }

...谢谢

答案 2 :(得分: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;
    }

}