It's an interesting little thing: using a custom view drawing, drawing lines, and drawing lines that fade until they disappear. The effect is shown in the following illustration:
You can make a stroke of change with a property animation or a gradient fill (Shader), but if you want a gradient (the finger doesn't lift the edge of the drawing edge), you don't find off-the-shelf APIs available in Android. So, I made one.
The basic idea is this:
Record the touch point in the ontouchevent of the view, generate a line lineelement, and place it in a list. Configure a paint instance for each lineelement.
Draws a segment in OnDraw.
Transforms the alpha value of the paint instance of the lineelement.
To reorganize a segment list based on alpha values
Nothing else to say, on the code:
Package com.example.disappearinglines;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.graphics.Path;
Import Android.graphics.RectF;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.os.SystemClock;
Import Android.support.annotation.NonNull;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.View;
Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.ListIterator; public class Disappearingdoodleview extends View {final static String TAG = "Doodleview"; class Lineelement {static FINA
L public int alpha_step = 5;
Static final public int subpath_dimension = 8; Public lineelement () {mpaint = new Paint (); Mpaint.setargb (255, 255, 0, 0); Mpaint.setantialias (true); Mpaint.setstrokewi
DTH (16);
Mpaint.setstrokecap (Paint.Cap.BUTT);
Mpaint.setstyle (Paint.Style.STROKE); } public LineelemeNT (Paint Paint) {mpaint = Paint;} public void Setpaint (Paint Paint) {mpaint = Paint;} public void Setalpha (int alpha) {m
Paint.setalpha (Alpha);
public float mstartx =-1;
public float Mstarty =-1;
public float mendx =-1;
public float Mendy =-1;
Public Paint Mpaint;
Private lineelement mcurrentline = null;
Private list<lineelement> mlines = null;
Private long melapsed = 0; Private Handler Mhandler = new Handler () {@Override public void Handlemessage (msg) {DISAPPEARINGDOODLEVIEW.THIS.I
Nvalidate ();
}
}; Public Disappearingdoodleview {Super (context);} public Disappearingdoodleview AttributeSet attrs) {Super (context, attrs);} @Override protected void OnDraw (Canvas Canvas) {melapsed = Systemclock.elaps
Edrealtime (); if (mlines!= null) {for (lineelement e:mlines) {if (E.mstartx < 0 | | E.mendy < 0) continue; Canvas.drawline (E.mst
Artx, E.mstarty, E.mendx, E.mendy, E.mpaint);
} compactpaths (); }} @Override public boolean OntoucheVent (Motionevent event) {float x = Event.getx (); Float y = event.gety (); int action = Event.getaction (); if (action = = Moti ONEVENT.ACTION_UP) {//end one of the finger release mcurrentline.mendx = x; mcurrentline.mendy = y; mcurrentline = nul
L
Invalidate ();
return true;
} if (action = = motionevent.action_down) {mcurrentline = new lineelement (); addtopaths (Mcurrentline);
Mcurrentline.mstartx = x;
Mcurrentline.mstarty = y;
return true; } if (action = = motionevent.action_move) {mcurrentline.mendx = x; mcurrentline.mendy = y; mcurrentline = new Lineelement ()
;
Addtopaths (Mcurrentline);
Mcurrentline.mstartx = x;
Mcurrentline.mstarty = y;
} if (Mhandler.hasmessages (1)) {mhandler.removemessages (1);}
msg = new Message ();
Msg.what = 1;
mhandler.sendmessagedelayed (msg, 0);
return true; private void Addtopaths (lineelement element) {if (Mlines = null) {Mlines = new arraylist<lineelement> ();} mline
S.add (Element); public void Compactpaths () {int size = mlines.size (); int index= Size-1;
if (size = = 0) return;
int basealpha = 255-lineelement.alpha_step;
int Itselfalpha;
Lineelement Line; for (; index >=0; index--, Basealpha-= lineelement.alpha_step) {line = Mlines.get (index); itselfalpha = line.mPaint.ge
Talpha (); if (Itselfalpha = = 255) {if (basealpha <= 0) {++index; break;} line.setalpha (Basealpha);}
else{Itselfalpha-= lineelement.alpha_step; if (itselfalpha <= 0) {++index; break;} line.setalpha (Itselfalpha);}} if (index >= size) {//All sub-path should disappear mlines = null;} else if (index >= 0) {//log.i (TAG, "compactpaths
From "+ Index +" to "+ (size-1));
Mlines = mlines.sublist (index, size);
}else{//No sub-path should disappear} long interval = 40-systemclock.elapsedrealtime () + melapsed;
if (interval < 0) interval = 0;
msg = new Message ();
Msg.what = 1;
Mhandler.sendmessagedelayed (msg, interval); }
}
The example can also add some effects, such as getting the line to thin as it fades.
The above is a small set of Android to introduce you to the use of custom view to draw an asymptotic animation of all the narration, hope to help everyone, if you have any questions welcome to my message, small series will promptly reply to everyone!