Android Draw dashed Line

Source: Internet
Author: User

Tool classes used:

Dashpatheffect

The description of the class in Android is as follows:

The translation is:

The first parameter intervals array must be an even number of elements (number >=2), even subscript represents a solid line, and an odd subscript represents a blank length

The second parameter phase: Left offset (offset =phase mod (sum of intervals array items), i.e. take-away operation)

Note: The dashed effect can only be drawn when the style of paint is set to stroke or fill_and_stroke, and will not work if the paint style is set to fill.

    /*** The intervals array must contain an even number of entries (>=2), with * The even indices specifying the "On" intervals, and the odd indices * specifying the "off" intervals. Phase is a offset into the intervals * Array (mod the sum of all of the intervals). The intervals array * Controls the length of the dashes.     The paint ' s strokewidth controls the * thickness of the dashes. * Note:this Patheffect only affects drawing with the paint ' s style are set * to STROKE or fill_and_stroke.     It is ignored if the drawing is do with * style = = FILL. * @paramintervals array of on and OFF distances *@paramphase offset into the intervals array*/     PublicDashpatheffect (floatIntervals[],floatphase) {        if(Intervals.length < 2) {            Throw Newarrayindexoutofboundsexception (); } native_instance=nativecreate (intervals, phase); }

Initialize Paint, Path

Private Paint Mdottedlinepaint; Private Path Mdottedpath; // Dashed Paint New Paint (); Mdottedlinepaint.setcolor (Getresources (). GetColor (R.color.horizontal_bar_chart_dot_line_color)); Mdottedlinepaint.setstyle (Style.stroke); Mdottedlinepaint.setstrokewidth (2new Dashpatheffect (newfloat[]{, 5}, 1new Path ();

Draw a dashed line

Canvas.drawpath (Mdottedpath, mdottedlinepaint);

Here need to pay attention to, must use the DrawPath method of canvas , if use drawline words is to draw the effect of, originally in the Internet to find the code is used DrawLine, the results of half a day not to effect

Android Draw dashed Line

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.