我正在绘制应用程序,我保存了每个路径并在ArrayList
中绘制,但我不知道我正在做什么工作无法按预期访问结果。
这是我的代码:
// Class extend from SurfaceView
private ArrayList<Drawing> drawingList;
// inner class
public class Drawing {
private Path path;
private Paint paint;
public Drawing(Path path, Paint paint){
this.path = path;
this.paint = paint;
}
public Path getPath() {
return path;
}
public Paint getPaint() {
return paint;
}
}
// inside setup function initialize ArrayList
drawingList = new ArrayList<Drawing>();
// Actual working function
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawCanvas = surfaceHolder.lockCanvas();
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawingList.add(new Drawing(drawPath, drawPaint));
for(Drawing drawing : drawingList) {
if(drawing.getPath() != null) {
drawCanvas.drawPath(drawing.getPath(), drawing.getPaint());
}
}
drawPath.reset();
surfaceHolder.unlockCanvasAndPost(drawCanvas);
break;
default:
return false;
}
invalidate();
return true;
}
问题:如果我绘制路径,前一次只绘制一个路径,当我绘制新路径时会自动删除。如果您从中移除drawPath.reset();
代码绘制路径开始工作正常,您可以根据需要绘制路径。但是,当您将drawPaint
的颜色更改为drawPaint.setColor(paintColor);
颜色设置为所有路径而不是最后一个时,会出现另一个问题
花了很多时间后,我认为问题的原因是删除了drawPath.reset();
代码。因为当我们删除java时,只考虑一条路径而不是多路径,这就是为什么当你改变它设置为整个路径的颜色时。但我不知道我是对的。
尝试克服这种情况为了克服这种情况,我创建了ArrayList
来存储每个路径并在其中绘制,每当用户绘制新路径时,它都会重绘所有路径。但它不是工作问题是一样的,你一次只能绘制一条路径,前一条路径被删除。
你能否让我知道我错过了什么。
更新
每次创建新的Path
和Paint
对象并将其保存在drawingList
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
Path path = new Path();
Paint paint = new Paint();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawCanvas = surfaceHolder.lockCanvas();
paint.setColor(paintColor);
paint.setAntiAlias(true);
paint.setStrokeWidth(20);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(brushSize);
path.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawingList.add(new Drawing(path, paint));
for(Drawing drawing : drawingList) {
if(drawing.getPath() != null) {
drawCanvas.drawPath(drawing.getPath(), drawing.getPaint());
}
}
surfaceHolder.unlockCanvasAndPost(drawCanvas);
break;
default:
return false;
}
invalidate();
return true;
}
问题没有任何绘制
原因我认为原因是drawingList
保存path
和paint
的引用每个用户触摸path
和{{1}初始化。这就是paint
松开引用而无法绘制任何内容的原因。
克服情况的想法如果我们将原始值保存在drawingList
而不是参考中,我认为我们可以克服这种情况。但我们怎么做呢?
答案 0 :(得分:1)
由于您在path
paint
和drawingList
,因此无效
如果您仔细查看代码,那么您正在path
内初始化paint
和onTouchEvent
,那么您可以在ACTION_DOWN
中分配这些变量,如您所知ACTION_DOWN
当用户保持触摸意味着它只能呼叫一次来绘制一个路径但onTouchEvent
用户连续呼叫时移动。 问题第二次onTouchEvent
致电path
和paint
进行初始化,但未指定为什么ACTION_UP
被称为drawingList
时只有空变量而你将空变量放在onTouchEvent
内。
要克服这种情况,只需在 Path path;
Paint paint;
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawCanvas = surfaceHolder.lockCanvas();
path = new Path();
paint = new Paint();
paint.setColor(paintColor);
paint.setAntiAlias(true);
paint.setStrokeWidth(20);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(brushSize);
path.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawingList.add(new Drawing(path, paint));
for(Drawing drawing : drawingList) {
if(drawing.getPath() != null) {
drawCanvas.drawPath(drawing.getPath(), drawing.getPaint());
}
}
surfaceHolder.unlockCanvasAndPost(drawCanvas);
break;
default:
return false;
}
invalidate();
return true;
}
喜欢这个。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MbnApi;
namespace TestMbnApi
{
class Program
{
static void Main(string[] args)
{
MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager();
IMbnInterfaceManager infMgr = mbnInfMgr as IMbnInterfaceManager;
IMbnConnectionManager conManager = (IMbnConnectionManager)new MbnConnectionManager();
// Connection
IMbnConnection[] connections = conManager.GetConnections();
IMbnConnection con = connections.First();
// Get interfaceID
String interfaceID = con.InterfaceID;
Console.WriteLine("InterfaceID : {0}", interfaceID);
// Get interface
IMbnInterface interfa = infMgr.GetInterface(interfaceID) as IMbnInterface;
// GetInterfaceCapability
MBN_INTERFACE_CAPS caps = interfa.GetInterfaceCapability();
Console.WriteLine("Caps :{0}", caps);
Console.ReadLine();
}
}
}
答案 1 :(得分:0)
drawCanvas.drawPath(drawing.getPath(),drawing.getPaint())
而不是使用drawing.getPath()使用drawing.get(position).getPath()和drawing.get(position).getPaint()