Android uses path to implement graffiti functionality _android

Source: Internet
Author: User
Tags gety

To achieve a graffiti effect today, will be a few steps to achieve, here is an important point of knowledge is layer, to understand this, or you see this blog, very confused, confused boundless horizon is my love, first from the simple need to do, draw a line, the code is as follows:

Package Com.tuya;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.graphics.Path;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
 /** * Created by admin on 2016/12/16.
  * * public class Graffitiview extends view{private Paint Paint;
  private path Path;
  private float Downx,downy;
  private float tempx,tempy;
  Public Graffitiview {This (context,null);
  Public Graffitiview (context, AttributeSet attrs) {This (context, attrs,0);
    Public Graffitiview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
  Init ();
    private void init () {paint = new paint ();
    Paint.setantialias (TRUE);
    Paint.setstrokewidth (10);
    Paint.setstyle (Paint.Style.STROKE);
  Path = new Path ();
    } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
     if (path!=null) { Canvas.drawpath (Path,paint); @Override public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motioneven
        T.action_down:downx = Event.getx ();
        DownY = Event.gety ();
        Path.moveto (Downx,downy);
        Invalidate ();
        TEMPX = Downx;
        Tempy = DownY;
      Break
        Case MotionEvent.ACTION_MOVE:float MoveX = Event.getx ();
        float Movey = event.gety ();
        Path.quadto (Tempx,tempy,movex,movey);
        Invalidate ();
        TEMPX = MoveX;
        Tempy = Movey;
    Break
  return true; }
}

Effect:

If you do not use the path Quadto () with the LineTo () method, but do not use queadto () to draw the smooth paths, these 5 paths are how to draw to the screen? The concept of the layer, if not understood, can go to see what I have written about Canvas blog, also can go to the Internet to see other blogs,
We know every time canvas.draw ... () actually produces new layers, only the layers are transparent, and finally insinuate to the canvas (canvas), the canvas is analyzed:

The above is a small set to introduce the Android use of path to achieve graffiti function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.