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!