Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple color/stroke gesture and persist drawing in same GestureOverlayView [duplicate]

I have seen many examples of drawing using gesture. My requirement is that, I want to draw multiple colored and multiple stroked gesture in same GestureOverlayView. To be more clear, please have a look at below image.

enter image description here

If user select different colors and different strokes, drawing should be of respective color/stroke. Now, its drawing different colored and different stroke gestures but if I select different color or different stroke, previously drawn gesture also takes new color or new stroke.

One similar question is answered here but has same issue.

Kindly suggest.

<android.gesture.GestureOverlayView
       android:id="@+id/signaturePad"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@android:color/white"
       android:eventsInterceptionEnabled="false"
       android:fadeEnabled="false"
       android:fadeOffset="122000"
       android:gestureStrokeLengthThreshold="0.1"
       android:gestureStrokeWidth="6"
       android:gestureStrokeType="multiple"
       android:orientation="vertical" >

   </android.gesture.GestureOverlayView>

In java code, changing color code

mGestureOverlayView.setGestureColor(Color.BLUE);

Thanks.

Edit : Thought to implement same with Canvas and got code from here. Its drawing multiple lines but again problem is when I change color/stroke, recently drawn lines also taking new color/stroke. Please suggest whats going wrong here.

Edit (Answer) : Finally got solution. Have a look at this link.


1 Answers

I'm afraid this wouldn't be possible without rolling your own version of GestureOverlayView with considerable modification. Here's a run through of what you'd need to change.

Consider what would be needed for this to work:

  1. You would need an iterable (or, in fact, List of some kind) of the past color settings- or in Android a stack of the past Paints.

  2. The Gestures would need to be stored in a separable way. Inside GestureOverlayView, these are stored using Android's Path, so these would need to be seperable- given path's api, the only way to do this would be as different path instances that could be overlaid seperately.

  3. Finally, there would have to be an ordering on the paths, so that the association between colours and paths could be made.

The above would, in very rough terms, enable you to iterate through to the latest path and colour and use the instance of View's draw(Canvas canvas) method to thisViewsCanvas.drawPath(mLatestAddedPaths.next(), mLatestPaintColor.next()) foreach path and paint.

*Finally, a little optimization would be to create each available paint colour when the view is created, and to hash it against its color, to save on paint creation. Your mLatestPaintColor list would then contain the hashes for the paints.

To see what's missing if you view the source, it's pretty clear that GestureOverlayView is along way from providing this:

  1. Look at the setGesutreColor(color), which you reference in your question. All this does is set a field mCertainGestureColor which is then used to set the colour for all draws.

  2. Rather wisely, when you call setGesture(Gesture gesture) (that's every time you make a gesture) the developers of this class extract the path on line 286 of this particular version:

    final Path path = mCurrentGesture.toPath();

And then instead of storing every path that was ever extracted, they combine that new path ("path") with the master path currently existing ("mPath"), on line 292:

mPath.addPath(path, -bounds.left + (getWidth() - bounds.width()) / 2.0f, -bounds.top + (getHeight() - bounds.height()) / 2.0f);

This means that when it comes to draw(Canvas), we have no idea which paths came in which order.

3 . Which finally takes me to why 3 doesn't work- the information available to the draw method on line 394 is only enough to draw each path the same colour:

canvas.drawPath(mPath, mGesturePaint);

So, it should be pretty possible to just reimplement this class yourself to achieve your goals, but it is pretty much impossible to try and achieve what you want to with the current class.

like image 89
Tom Avatar answered Jan 24 '26 01:01

Tom