Android shape shapes

Source: Internet
Author: User


shape Inheritance System:

Shape (Android.graphics.drawable.shapes)
----Pathshape (android.graphics.drawable.shapes)
----Rectshape (android.graphics.drawable.shapes)
--------Arcshape (android.graphics.drawable.shapes)
--------OvalShape (android.graphics.drawable.shapes)
--------Roundrectshape (android.graphics.drawable.shapes)


Rectshape

Rectshape rectshape = new Rectshape (); Shapedrawable drawable = new Shapedrawable (rectshape);d rawable.getpaint (). SetColor (color.red);d rawable.getpaint (). SetStyle (Paint.Style.FILL); Filling view.setbackgrounddrawable (drawable);

Rectangle


Roundrectshape

Float[] outerradii = {20, 20, 40, 40, 60, 60, 80, 80};//outer rectangle left, top right, bottom right, left corner radius//float[] outerradii = {20, 20, 20, 20, 20, 20  , 20, 20};//outer rectangle left upper, upper right, lower right, lower left corner radius RECTF inset = new RECTF (100, 100, 200, 200);//inner rectangle outside rectangle, upper left corner x, y distance, lower right corner x, y distance float[] innerradii = {20, 20, 20, 20, 20, 20, 20, 20};//rectangular fillet radius//roundrectshape roundrectshape = new Roundrectshape (outerradii, inset, Inne RRADII); Roundrectshape roundrectshape = new Roundrectshape (outerradii, NULL, innerradii); No inner rectangle shapedrawable drawable = new Shapedrawable (roundrectshape);d rawable.getpaint (). SetColor (Color.magenta); Drawable.getpaint (). Setantialias (True);d rawable.getpaint (). SetStyle (Paint.Style.STROKE);//Stroke View.setbackground (drawable);

Rounded rectangles with rounded rectangles without inner rectangles


OvalShape

OvalShape OvalShape = new OvalShape (); Shapedrawable drawable = new Shapedrawable (ovalshape);d rawable.getpaint (). SetColor (color.red);d rawable.getpaint (). SetStyle (Paint.Style.FILL_AND_STROKE); view.setbackgrounddrawable (drawable);

ellipse. When the width and height of the view are equal, the circle is drawn


Arcshape

Arcshape arcshape = new Arcshape (45, 270); Clockwise  start angle 45, scan the angle of the   fan shapedrawable drawable = new Shapedrawable (arcshape);d rawable.getpaint (). SetColor ( color.red);d rawable.getpaint (). SetStyle (Paint.Style.FILL);//Bitmap Bitmap =  ((bitmapdrawable) getresources (). Getdrawable (R.DRAWABLE.AA)). Getbitmap ();//Bitmapshader Bitmapshader = new Bitmapshader (bitmap, Shader.TileMode.MIRROR, shader//                . Tilemode.repeat);//Matrix Matrix = new Matrix ();//Matrix.prescale (600.00f/bitmap.getwidth (), 600.00f/bitmap.getheig HT ());//view:w=600,h=600//Bitmapshader.setlocalmatrix (matrix);//Drawable.getpaint (). Setshader (Bitmapshader); View.setbackgrounddrawable (drawable);

Fan Chart

Combined with Bitmapshader


Pathshape

Path PATH = new Path ();p ath.moveto (0);p ath.lineto (0, 50);p Ath.lineto (+),;p Ath.lineto (+),;p Ath.lineto, 0); Pathshape pathshape = new Pathshape (path, 200, 100); Shapedrawable drawable = new Shapedrawable (pathshape);d rawable.getpaint (). SetColor (color.red);d rawable.getpaint (). SetStyle (Paint.Style.FILL); imageview.setbackgrounddrawable (drawable);

Sets the shape to the Path object.

Pathshape constructor: Pathshape (Path, stdwidth, stdheight);

Stdwidth: Standard Width

Stdheight: Standard Height

When constructing a Pathshape object, a standard with a wide height is set. Intrinsic functions

protected void OnResize (float width, float height) {    Mscalex = width/mstdwidth;    Mscaley = Height/mstdheight;} public void Draw (canvas canvas, paint paint) {    canvas.save ();    Canvas.scale (Mscalex, Mscaley);    Canvas.drawpath (MPath, paint);    Canvas.restore ();}
There is a function resize () in the shape base class, where onresize () is called and resize () is called in shapedrawable.

With the standard width set, the ratio of the actual width to the standard width is calculated, and the canvas is scaled at the end of the drawing.

Effect: The (x, y) coordinate value in path is multiplied by the ratio value, which is the final rendered coordinate value (the actual interior is a scaled canvas)

For example, the w=400 of the view here, h=400

If the standard width is equal to 400, then the canvas is eventually not scaled, or 1:1.

Pathshape pathshape = new Pathshape (path, 400, 400);

stdx=400, stdy=400


Pathshape pathshape = new Pathshape (path, 100, 100);

stdx=100, stdy=100


Pathshape pathshape = new Pathshape (path, 200, 100);

stdx=200, stdy=100




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android shape shapes

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.