Android custom View drawing realizes fade animation _android

Source: Internet
Author: User
Tags int size

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 of lineelement, and place it in a list. Configure a paint instance for each lineelement.
• Draw segments in OnDraw.
• Transforms the alpha value of the paint instance of the lineelement.
• Reorganize the segment list by alpha value

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 final 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.setstrokewidth (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) {Mpaint.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) {Disappearingdood
    LeView.this.invalidate ();

  }
  };
  Public Disappearingdoodleview {Super (context);
  Public Disappearingdoodleview (context, AttributeSet attrs) {Super (context, attrs);
    } @Override protected void OnDraw (Canvas Canvas) {melapsed = Systemclock.elapsedrealtime (); if (mlines!= null) {for (LinEelement e:mlines) {if (E.mstartx < 0 | | | E.mendy < 0) continue;
      Canvas.drawline (E.mstartx, 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 = = motionevent.action_up) {//end one line after finger release mcurrentline.mendx = x;
      Mcurrentline.mendy = y;
      Mcurrentline = null;
      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&gt
    ;() ;
  } mlines.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.getAlpha ();
          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.

At present, there are some problems, lines thick, you can obviously see the line segment and line between the gap or rift, who thought how to optimize?

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.