问题描述
我想提出一个绘图应用程序,以下code工作正常,但那个时候我想改变paintline的颜色,绘制将变更为新颜色全部previous行:
I am making a drawing app and the following code works fine, except that when i would like to change the colour of the paintline, all the previous line drawn will be changed to the new color:
private Bitmap bitmap; // drawing area for display or saving
private Canvas bitmapCanvas; // used to draw on bitmap
private Paint paintLine; // used to draw lines onto bitmap
private Path mPath;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
// DoodleView constructor initializes the DoodleView
public DoodleView(Context context, AttributeSet attrs)
{
super(context, attrs); // pass context to View's constructor
this.context_new=context;
setFocusable(true);
setFocusableInTouchMode(true);
// set the initial display settings for the painted line
paintLine = new Paint();
paintLine.setAntiAlias(true); // smooth edges of drawn line
paintLine.setDither(true);
paintLine.setColor(Color.BLACK); // default color is black
paintLine.setStyle(Paint.Style.STROKE); // solid line
paintLine.setStrokeJoin(Paint.Join.ROUND);
paintLine.setStrokeWidth(5); // set the default line width
paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
bitmapCanvas = new Canvas();
mPath = new Path();
} // end DoodleView constructor
// Method onSizeChanged creates BitMap and Canvas after app displays
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH)
{
super.onSizeChanged(w, h, oldW, oldH);
DoodlzViewWidth = w;
DoodlzViewHeight = h;
}
@Override
protected void onDraw(Canvas canvas)
{
for (Path p : paths){canvas.drawPath(p, paintLine);}
canvas.drawPath(mPath, paintLine);
Log.i("OnDRAWING", "REACH ON DRAW");
}
// START TOUCH: handle touch event
@Override
public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
private void touch_start(float x, float y)
{
undonePaths.clear();
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y)
{
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
{
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up()
{
mPath.lineTo(mX, mY);
bitmapCanvas.drawPath(mPath, paintLine);// commit the path to our offscreen
paths.add(mPath);
mPath = new Path();
}
public void onClickUndo()
{
if (paths.size()>0)
{
undonePaths.add(paths.remove(paths.size()-1));
invalidate();
}
else Toast.makeText(getContext(), "nothing more to undo", Toast.LENGTH_SHORT).show();
}
setDrawingColor:
public void setDrawingColor(int color) // set the painted line's color
{
paintLine.setColor(color);
}
如何才能上述code进行修改,使得previous线固定在其原来的颜色?谢谢!!
How could the above code be modified such that the previous line are fixed in their original colours??? Thanks!!
推荐答案
您需要跟踪路径的颜色和的onDraw
恢复是使用油漆的颜色路径开始。
You need to keep track of path color and in the onDraw
restore the color that was use to paint the path initially.
protected void onDraw(Canvas canvas)
{
for (Path p : paths){canvas.drawPath(p, paintLine);}
paintLine.setColor(restoreColorForPath(p));
canvas.drawPath(mPath, paintLine);
Log.i("OnDRAWING", "REACH ON DRAW");
}
您只需要执行 restoreColorForPath
和它的康特一部分 storeColorForPath(路径,颜色)
(可能用一个简单的地图
)
You just have to implement restoreColorForPath
and it's conter part storeColorForPath(path,color)
(probably with a simple Map
)
这类似的问题可以帮助你:How绘制多行用不同的颜色和撤消,重做android系统的路径?
This similar question can help you : How to draw the multiple lines with different color and undo,redo the paths in android?
这篇关于Android的应用程序绘制线条,一旦变色所有的previously绘制的线条涂上新颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!