0

I want to crop shape in Hexagon,Octagon and in circle shape.I have used Custom Image view class and use in xml for showing image. It works properly for different shapes.

Now i need to crop the image in user selected shape in next activity.i get image in next activity.

I tried this example: Masking(crop) image in frame But the image didn't fit in shape.only part of the image get masked.

How i can achieve this?

Community
  • 1
  • 1

1 Answers1

0

Read this blog post by Romain Guy on how to make images with rounded corners. Using this technique you can create a variety of shapes.

How would this work?

If you have a predefined number of shapes, I'd suggest you create a class with a few Shapes defined by Paths. Now whenever your user asks for a different shape to be used on an ImageView, you create a Bitmap and return a bitmap from that class and put it on your ImageView.

For example a octagon shape would be something like this:

public static Bitmap drawOctagonShapedBitmap(Bitmap src) {

    Bitmap dst =  Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    //Create an output as big as the actual bitmap.
    Path octagon = null; //Create an octagon shape, it should be big enough to crop enough of the bitmap.
    Canvas canvas = new Canvas(dst);
    Paint mPaint = new Paint();

    BitmapShader mBitmapShader = new BitmapShader(src, Shader.TileMode.CLAMP,
            Shader.TileMode.CLAMP);
    mPaint.setAntiAlias(true);
    mPaint.setShader(mBitmapShader);

    canvas.drawPath(octagon, mPaint);

    return dst;
}
Gent Ahmeti
  • 1,559
  • 13
  • 17