如何使用帆布绘制路径或路径在位图上绘画?我尝试了几个教程,但失败了
因此,这是我当前使用Canvas drawline 的代码,我正在尝试将其修改为 drawpath 。
我在主要活动中声明的变量:
ImageView imageResult;
final int RQS_IMAGE1 = 1;
Paint paint;
Uri source;
Bitmap bitmap;
Canvas canvas;
Path path;
我的代码在创建方法中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawing_board);
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(10);
//result is the ID of imageview
imageResult = findViewById(R.id.result);
//The user will select Images on gallery
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_IMAGE1);
其中所选映像传递到成像变量中,然后转换为位图,然后将其放置在画布中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap tempBitmap;
if (resultCode == RESULT_OK) {
switch (requestCode) {
case RQS_IMAGE1:
source = data.getData();
try {
//tempBitmap is Immutable bitmap,
//cannot be passed to Canvas constructor
tempBitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(source));
Bitmap.Config config;
if (tempBitmap.getConfig() != null) {
config = tempBitmap.getConfig();
} else {
config = Bitmap.Config.ARGB_8888;
}
//bitmap is Mutable bitmap
bitmap = Bitmap.createBitmap(
tempBitmap.getWidth(),
tempBitmap.getHeight(),
config);
canvas = new Canvas(bitmap);
canvas.drawBitmap(tempBitmap, 0, 0, null);
imageResult.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
现在,这是我获取的方法用户手指并将帆布.DRAWLINE放入运动事件中。因此,他们可以在放置图像视图的画布上绘画:
private float mX,mY;
public void paintOn_Image(){
imageResult.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mX = x;
mY = y;
float ratioWidth = (float)bitmap.getWidth()/(float)v.getWidth();
float ratioHeight = (float)bitmap.getHeight()/(float)v.getHeight();
canvas.drawLine(
mX * ratioWidth,
mY * ratioHeight,
x * ratioWidth,
y * ratioHeight,
paint);
imageResult.invalidate();
break;
case MotionEvent.ACTION_MOVE:
ratioWidth = (float) bitmap.getWidth() / (float) v.getWidth();
ratioHeight = (float)bitmap.getHeight()/(float)v.getHeight();
canvasMaster.drawLine(
mX * ratioWidth,
mY * ratioHeight,
x * ratioWidth,
y * ratioHeight,
paint);
imageResult.invalidate();
mX = x;
mY = y;
break;
case MotionEvent.ACTION_UP:
ratioWidth = (float) bitmapMaster.getWidth() / (float) v.getWidth();
ratioHeight = (float)bitmapMaster.getHeight()/(float)v.getHeight();
canvasMaster.drawLine(
x * ratioWidth,
y * ratioHeight,
x * ratioWidth,
y * ratioHeight,
paint);
imageResult.invalidate();
break;
}
return true;
}
});
这是我到目前为止所做的。 在MotionEvent.action_down中,Action_Move& Action_up,我更改此
canvas.drawLine(
mX * ratioWidth,
mY * ratioHeight,
x * ratioWidth,
y * ratioHeight,
paint);
imageResult.invalidate();
break;
信息:
path.moveTo(mX*ratioWidth,mY*ratioHeight);
path.lineTo(x*ratioWidth,y*ratioHeight);
path.moveTo(mX*ratioWidth,y*ratioHeight);
path.lineTo(x*ratioWidth,mY*ratioHeight);
canvas.drawPath(path,paintDraw);
imageResult.invalidate();
canvas.drawPath(path, paint);
imageResult.invalidate();
break;
但是,当用户触摸画布时,应用程序正在崩溃:(
我如何使用路径上的画布上绘画?计算不正确吗?
我试图将其更改为路径的原因是我们不能 undo 绘画进入画布后的绘图。因此,如果您有解决方案 撤消/clear/删除绘制线,这也对我有用。谢谢。
So this is my current code which uses canvas drawLine, where I'm trying to modify it into drawPath.
My Declared Variables in Main Activity :
ImageView imageResult;
final int RQS_IMAGE1 = 1;
Paint paint;
Uri source;
Bitmap bitmap;
Canvas canvas;
Path path;
My code in On Create method :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawing_board);
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(10);
//result is the ID of imageview
imageResult = findViewById(R.id.result);
//The user will select Images on gallery
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_IMAGE1);
Where selected image pass into imageResult variable and then converted into bitmap, and that bitmap will be place into the canvas :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap tempBitmap;
if (resultCode == RESULT_OK) {
switch (requestCode) {
case RQS_IMAGE1:
source = data.getData();
try {
//tempBitmap is Immutable bitmap,
//cannot be passed to Canvas constructor
tempBitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(source));
Bitmap.Config config;
if (tempBitmap.getConfig() != null) {
config = tempBitmap.getConfig();
} else {
config = Bitmap.Config.ARGB_8888;
}
//bitmap is Mutable bitmap
bitmap = Bitmap.createBitmap(
tempBitmap.getWidth(),
tempBitmap.getHeight(),
config);
canvas = new Canvas(bitmap);
canvas.drawBitmap(tempBitmap, 0, 0, null);
imageResult.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
Now, this is the method where I get the touch points of users finger and put the canvas.drawLine inside of motion events. So then, they can paint on the canvas where the Imageview is placed :
private float mX,mY;
public void paintOn_Image(){
imageResult.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mX = x;
mY = y;
float ratioWidth = (float)bitmap.getWidth()/(float)v.getWidth();
float ratioHeight = (float)bitmap.getHeight()/(float)v.getHeight();
canvas.drawLine(
mX * ratioWidth,
mY * ratioHeight,
x * ratioWidth,
y * ratioHeight,
paint);
imageResult.invalidate();
break;
case MotionEvent.ACTION_MOVE:
ratioWidth = (float) bitmap.getWidth() / (float) v.getWidth();
ratioHeight = (float)bitmap.getHeight()/(float)v.getHeight();
canvasMaster.drawLine(
mX * ratioWidth,
mY * ratioHeight,
x * ratioWidth,
y * ratioHeight,
paint);
imageResult.invalidate();
mX = x;
mY = y;
break;
case MotionEvent.ACTION_UP:
ratioWidth = (float) bitmapMaster.getWidth() / (float) v.getWidth();
ratioHeight = (float)bitmapMaster.getHeight()/(float)v.getHeight();
canvasMaster.drawLine(
x * ratioWidth,
y * ratioHeight,
x * ratioWidth,
y * ratioHeight,
paint);
imageResult.invalidate();
break;
}
return true;
}
});
Here's what I've done so far.
In the motionEvent.ACTION_DOWN,ACTION_MOVE & ACTION_UP, I change this :
canvas.drawLine(
mX * ratioWidth,
mY * ratioHeight,
x * ratioWidth,
y * ratioHeight,
paint);
imageResult.invalidate();
break;
Into this :
path.moveTo(mX*ratioWidth,mY*ratioHeight);
path.lineTo(x*ratioWidth,y*ratioHeight);
path.moveTo(mX*ratioWidth,y*ratioHeight);
path.lineTo(x*ratioWidth,mY*ratioHeight);
canvas.drawPath(path,paintDraw);
imageResult.invalidate();
canvas.drawPath(path, paint);
imageResult.invalidate();
break;
But the application is crashing when the user touch the canvas :(
How can I paint on canvas using Path? Is the calculation incorrect?
The reason why I'm trying to change it into path is we can't undo the
drawLine after painting into the canvas. So if you have solution to
undo/clear/remove the drawLine, that will also work for me. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在阅读了大量文档,教程和信息之后。
只需创建DrawableView和customImageView,而不是在ImageView上创建。
来源
After reading a lot of documentation and tutorials and informations in this forum.
Just Create DrawableView and CustomImageview instead of dwelling on Imageview.
Sources