Irregular polygon filling bitmap in Android

Source: Internet
Author: User

I recently studied Android 2D graphics. In Android, there are many ways to draw polygon. Here is a simple method.
Use path.

Path = New Path ();
Path. moveTo (50, 50 );
Path. lineto (100, 50 );
Path. lineto (100, 0 );
Path. lineto (150, 0 );
Path. lineto (150, 50 );
Path. lineto (200, 50 );

Path. lineto (200,100 );
Path. lineto (250,100 );
Path. lineto (250,150 );
Path. lineto (200,150 );
Path. lineto (200,200 );

Path. lineto (150,200 );
Path. lineto (150,250 );
Path. lineto (100,250 );
Path. lineto (100,200 );
Path. lineto (50,200 );

Path. lineto (50,150 );
Path. lineto (0,150 );
Path. lineto (0,100 );
Path. lineto (50,100 );

Path. Close ();

// Generate a polygon
Mdrawable = new shapedrawable (New pathshape (path, 250,250 ));
// Mdrawable. getpaint (). setcolor (color. Red );

Bitmap bitmap = bitmapfactory. decoderesource (this. getresources (), R. drawable. test );
Bitmap b1 = bitmap. createbitmap (bitmap, 0, 0,250,250); // generate a bitmap that matches the polygon size to avoid resolution problems.
Shader ashader = new bitmapshader (B1, shader. tilemode. Repeat, shader. tilemode. Repeat );
Mdrawable. getpaint (). setshader (ashader); // fill the bitmap

Mdrawable. setbounds (250,250,); // you can specify the boundary size.

Final generation

Original address: http://www.xiaojxiao.net /? P = 49

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.