0

I am trying to create an unique shape using Path, Path.quadTo and Canvas and then pass a bitmap to this unique shape. The idea is that the square bitmap will transform to the unique shape of the canvas. I am not sure if this is possible, but this is what I am trying to accomplish and this is how I have interpreted the documentation for setting a bitmap to canvas. I am receiving an error "UnsupportedOperationException" when I set the bitmap to the canvas. The bitmap is in my resource folder. What am I missing?

    @override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // get bitmap
    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.squareImage);

    // set up Paint attributes
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(5.of);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setAntiAlias(true);

    // set up path
    Path path = new Path();
    path.moveTo(x1, y1)

    // apply curve with quadTo function
    final float x2 = (x1 + x3)/2
    final float y2 = (y1 + y3)/2
    path.quadTo(x2, y2, x, y);

    // shape the canvas according to the path attributes
    canvas.drawPath(path, paint);

    }

    /* Note: at this point I have an unique shape. I now want to apply an image to this shape */

    // reference canvas passed into the onDraw() method and set to bitmap
    // canvas.setBitmap(mBitmap);

The canvas.setBitmap(mBitmap) will cause error

02-17 08:43:27.168: E/AndroidRuntime(3438): java.lang.UnsupportedOperationException
02-17 08:43:27.168: E/AndroidRuntime(3438):     at android.view.HardwareCanvas.setBitmap(HardwareCanvas.java:39)
02-17 08:43:27.168: E/AndroidRuntime(3438):     at com.example.puzzledemo.activity.PathExample$PathView.onDraw(PathExample.java:90)

To add some context, the point of this is to create puzzle pieces with unique shapes. If that helps at all with what my end goal is. Thanks in advance!

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136

2 Answers2

2

I believe you are looking for canvas.drawBitmap

charliebeckwith
  • 1,449
  • 11
  • 29
  • Thank you for your answer. Can you tell me why you believe this function is what I need over what I have been trying to implement? And maybe add some code example on how to use it in my context? I thought canvas.drawBitmap was for drawing rectangle/square shapes, not unique shapes like what jigsaw puzzle pieces take form as. – portfoliobuilder Feb 17 '15 at 02:05
  • 1
    Here is some example code that show help. What i was suggesting is that you draw the Bitmap as a rectangle and apply a mask. http://stackoverflow.com/a/12637039/3508192 This is the most likely solution for your problem. – charliebeckwith Feb 17 '15 at 15:50
  • Thanks. I understand you already have rectangle bitmaps the point is that you can apply a mask and "cut" the rectangle in to the shape of your puzzle piece. If you want to draw the shape directly to a bitmap you need to create a new Canvas with the bitmap (i.e. Canvas c = new Canvas(myBitmap); ) and draw on that. This is why you are getting your "UnsupportedOperationException", you are setting the Bitmap to the canvas that draws to the screen I think. You can't do that. – charliebeckwith Feb 17 '15 at 17:54
  • This is clear now @Solarnum. Thank you, your solution with mask works. I just need some pieces to mask with now :-). Cheers! – portfoliobuilder Feb 17 '15 at 19:25
0

Portfoliobuilder, one of the paint color choices available is transparent. Perhaps if you used a rectangular bitmap to represent each piece, but painted in transparent around the piece borders, you could get the appearance of irregular pieces, but still use the android classes intended for use on rectangular bitmaps? That might save a lot of effort of your part. You will have to create a class to keep track of how the pieces are rotated, their distance apart and which sides match, but you would have to do that anyway.