[Android custom control] Android custom dotted line

Source: Internet
Author: User

Most of the time, the dotted line is painted using a chopped graph (a real point, a virtual point), and then using the repeat attribute of Bitmap

 

 
 

 

Next we will draw a dotted line, first define a dashedline class, inherit from the View, override the Ondraw () method

 

Public class DashedLine extends View {private final String namespace = "http://www.android-study.com/"; private float startX; private float startY; private float endX; private float endY; private Rect mRect; public DashedLine (Context context, AttributeSet attrs) {super (context, attrs) ;}@ Overrideprotected void onDraw (Canvas canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); Paint paint = new Paint (); paint. setStyle (Paint. style. STROKE); // hollow paint. setColor (Color. DKGRAY); Path path = new Path (); // use the x and y coordinates of moveto and lineto to determine whether the dotted line is horizontal, vertical, or skewed path. moveTo (0, 10); // Set the beginning of the next contour to the point (x, y) path. lineTo (480, 10); // Add a line from the last point to the specified point (x, y ). // DashPathEffect can be used to create a dotted line outline (short horizontal line/small dot), instead of using solid line // float [] {5, 5, 5, 5} value control dotted line spacing, density PathEffect effects = new DashPathEffect (new float [] {5, 5, 5}, 1); paint. setPathEffect (effects); canvas. drawPath (path, paint );}}


 

 

After defining the View, you can use it in XML.

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.