我正在制作一个绘图应用程序,但我不知道为什么当从图库加载图片时,当进一步绘制时,刚刚绘制的线将出现在Touch上,但当手指离开屏幕时会消失,即绘制的线不能固定在Bitmap上。
有没有人知道如何修改它?非常感谢!!!
public class DrawView extends View // the main screen that is painted
{
private static final float TOUCH_TOLERANCE = 10;
private Bitmap bitmap; // drawing area for display or saving
private Canvas bitmapCanvas; // used to draw on bitmap
private Paint paintScreen; // use to draw bitmap onto screen
private Paint paintLine; // used to draw lines onto bitmap
private HashMap<Integer, Path> pathMap; // current Paths being drawn
private HashMap<Integer, Point> previousPointMap; // current Points
public DrawView(Context context, AttributeSet attrs)
{
super(context, attrs);
paintScreen = new Paint();
// set the default settings
paintLine = new Paint();
paintLine.setColor(Color.BLACK);
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(5);
pathMap = new HashMap<Integer, Path>();
previousPointMap = new HashMap<Integer, Point>();
}
// Method onSizeChanged creates BitMap and Canvas after app displays
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH)
{
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas(bitmap);
bitmap.eraseColor(Color.WHITE); // erase the BitMap with white
}
public void load_pic(String picturePath) // load a picture from gallery
{
pathMap.clear(); // remove all paths
previousPointMap.clear(); // remove all previous points
bitmap = BitmapFactory.decodeFile(picturePath);
invalidate(); // refresh the screen
}
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, 0, 0, paintScreen);
for (Integer key : pathMap.keySet())
canvas.drawPath(pathMap.get(key), paintLine); // draw line
}
// handle touch event
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getActionMasked();
int actionIndex = event.getActionIndex(); // pointer (i.e., finger)
// determine which type of action the given MotionEvent represents, then call the corresponding handling method
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN)
{
touchStarted(event.getX(actionIndex), event.getY(actionIndex), event.getPointerId(actionIndex));
} // end if
else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP)
{
touchEnded(event.getPointerId(actionIndex));
} // end else if
else
{
touchMoved(event);
} // end else
invalidate(); // redraw
return true; // consume the touch event
} // end method onTouchEvent
// called when the user touches the screen
private void touchStarted(float x, float y, int lineID)
{
Path path; // used to store the path for the given touch id
Point point; // used to store the last point in path
// if there is already a path for lineID
if (pathMap.containsKey(lineID))
{
path = pathMap.get(lineID); // get the Path
path.reset(); // reset the Path because a new touch has started
point = previousPointMap.get(lineID); // get Path's last point
} // end if
else
{
path = new Path(); // create a new Path
pathMap.put(lineID, path); // add the Path to Map
point = new Point(); // create a new Point
previousPointMap.put(lineID, point); // add the Point to the Map
} // end else
// move to the coordinates of the touch
path.moveTo(x, y);
point.x = (int) x;
point.y = (int) y;
}
...similar for other on Touch event
答案 0 :(得分:0)
Touch事件结束时是否删除了pathMap(即touchEnded方法)?
这会导致您描述的行为,因为您没有在Bitmap上绘图,而是在Canvas上绘制了直线和位图(并且每次onDraw事件都会重绘Canvas) );也就是说,对于每个onDraw,您将绘制上一个绘图。
答案 1 :(得分:0)
我使用以下代码解决了这个问题。 需要复制位图以进行编辑。
bitmap = (BitmapFactory.decodeFile(picturePath));
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
bitmapCanvas = new Canvas(bitmap);
invalidate();