Android Bitmap裁剪椭圆形状

时间:2017-02-16 16:31:43

标签: android bitmap crop bitmapfactory

假设我有一个我用前置摄像头拍摄的人像位图。 在拍完照片后,如何将位图裁剪为椭圆形叠加的形状?椭圆形状以屏幕为中心。椭圆形具有固定的高度和宽度

sys.stderr

截图: enter image description here

1 个答案:

答案 0 :(得分:0)

您可以根据如何从位图裁剪圆圈来确定解决方案:

    @Override
    public Bitmap crop(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap;
    }

现在只需将canvas.drawCircle更改为canvas.drawOval并为其指定适当的值。