【发布时间】:2015-02-17 10:36:27
【问题描述】:
我正在尝试使用 Path、Path.quadTo 和 Canvas 创建一个独特的形状,然后将位图传递给这个独特的形状。这个想法是方形位图将转换为画布的独特形状。我不确定这是否可能,但这是我想要完成的,这就是我如何解释将位图设置为画布的文档。将位图设置为画布时,我收到错误“UnsupportedOperationException”。位图在我的资源文件夹中。我错过了什么?
@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);
canvas.setBitmap(mBitmap) 会报错
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)
要添加一些上下文,重点是创建具有独特形状的拼图。如果这对我的最终目标有帮助的话。提前致谢!
【问题讨论】:
-
您是要将画布上的图片保存为位图还是在画布上放置背景图像?
-
我有一些方形图像,我想将它们变成拼图。所以我想在画布上放一张背景图片。
标签: android canvas android-canvas