Android custom Control (ii) Bitmapshader, shapedrawable, Shape

Source: Internet
Author: User
Tags float number

In the first blog, I have summarized some common methods, this article mainly introduces Bitmapshader bitmap rendering, Composeshader combined rendering, and then see how xfermode is actually applied. However, this article is only to rewrite OnDraw a method, 工欲善其事 its prerequisite, a method of a method to learn, first understand what each object is doing.
Viewhelper (View processing common method encapsulation)
Android custom Controls (i) Canvas, Paint, Shader
Android custom Control (ii) Bitmapshader, shapedrawable, Shape

Oval ImageView with border

Start by writing a custom control and encourage yourself.

/** * Shapedrawable Object Use * Created by Chenss on 2016/11/24. */ Public  class rectview extends View {    PrivateBitmapshader Mbitmapshader;PrivateShapedrawable mshapedrawable;PrivateBitmap Mbitmap;PrivatePaint Mpaint;Private intMwidth, Mheight; Public Rectview(Context context) {Super(context);//Get imagesMbitmap = Viewhelper.findbitmapbyid (context, r.mipmap.view_shape);//Construct renderer Bitmapshader, modify Tilemode to see effectsMbitmapshader =NewBitmapshader (Mbitmap, Shader.TileMode.MIRROR, Shader.TileMode.REPEAT); Mpaint =NewPaint (); Mpaint.setantialias (true);        Mpaint.setcolor (Color.gray);        Mwidth = Mbitmap.getwidth ();    Mheight = Mbitmap.getheight (); }@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);//Add a strokeCanvas.drawoval (Ten,Ten, Mwidth-290, Mheight- -, Mpaint);//Build Shapedrawable object and define shape as ellipseMshapedrawable =NewShapedrawable (NewOvalShape ());//Get brushes and set rendererMshapedrawable.getpaint (). Setshader (Mbitmapshader);//Set display areaMshapedrawable.setbounds ( -, -, Mwidth- -, Mheight- -);//Draw shapedrawableMshapedrawable.draw (canvas); }}

Use our custom view in activity:

publicclass RectActivity extends AppCompatActivity {    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(new RectView(this));    }}


The effect seems to be good, if you learn to rewrite onmeasure probably can customize the view, the heart has some small excitement.

Summary:

In this demo, we used the Bitmapshader,shapedrawable,shader.tilemode.

    1. Shader.tilemode in the previous blog knowledge to mention, it has three modes: CLAMP (stretching), REPEAT (repetition), MIRROR (mirror);
    2. Bitmapshader in the previous blog also slightly said, is a bitmap rendering object;
    3. Shapedrawable look at the name, it seems to be drawn shape;
    4. We give shapedrawable the argument is ovalshape,ovalshape literal translation is an ellipse, so is to draw the ellipse, look at the API, found that OvalShape has a parent shape, there should be other brothers.

So clear our learning goals: the difference between shader.tilemode tiling patterns, what kind of subclasses (what shapes can we draw), and how do we use them? Can you achieve the above effects without using shapedrawable?

shader.tilemode Tiling Mode

Shape Shapes Object

API Introduction:
Defines a generic graphical "shape." Any Shape can being drawn to a Canvas with it own draw () method, but more graphical control is available if you instead pass It to a shapedrawable.

Looking at API discovery, shape has 2 direct subclasses, 3 indirect subclasses, respectively:
Pathshape, Rectshape,arcshape, OvalShape, Roundrectshape.

Pathshape

Polygon
On a blog I wrote a rendering of the demo, using the path, I believe that the wit of you all of a sudden to understand its role, the demo in accordance with the X, Y values continue to draw lines, and finally draw a polygon, but path literal translation since the path, certainly can draw other lines, we can do their own research. From the role of path we can also see that pathshape is a high degree of freedom, can be set up a variety of shapes, want to design a wonderful shape when considering the use of it (pentagram, diamond, hexagon, etc.).

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

    • Path object to set the graph.
    • Stdwidth: Standard Width
    • Stdheight: Standard Height
Rectshape

Rectangle to create the instance directly.
constructor Function:
Rectshape ()

Arcshape

Sector
constructor Function:
Arcshape (float startangle, float sweepAngle)

    • StartAngle: Starting angle
    • SweepAngle: End Angle
OvalShape

Ellipse, create an instance directly.
constructor Function:
OvalShape ()

Roundrectshape

Rounded rectangle, the three parameters of the constructor are set according to the requirements, do not want to be set to null,outerradii and innerradii need to be at least 8 of the float type array, each two float number determines a radian value, a total of 4 radian values, respectively: upper left, upper right, Bottom right, bottom left 4 positions.
constructor Function:
Roundrectshape (float[] outerradii, RECTF inset, float[] innerradii)

    • OUTERRADII: Outer rectangle top left, top right, bottom right, bottom left corner radius
    • Inset: Embedded RECTF Rectangle, 4 parameters of margin
    • Innerradii: Inner rectangle left upper, upper right, bottom right, bottom left corner radius
Supplement: Roundrectshape Use demo

Because the Roundrectshape parameter does not look good to understand, add a demo, if you do not set inset, innerradii These two parameters, then he is a rounded rectangle, after setting, the equivalent of a hole dug from the middle.

/** * Shapedrawable Object Use * Created by Chenss on 2016/11/24. */ Public  class rectview extends View {    PrivateBitmapshader Mbitmapshader;PrivateShapedrawable mshapedrawable;PrivateShape Mshape;PrivateBitmap Mbitmap; Public Rectview(Context context) {Super(context);//Get a picture in placeMbitmap = Viewhelper.findbitmapbyid (context, r.mipmap.view_shape);//Construct renderer Bitmapshader, modify Tilemode test effectMbitmapshader =NewBitmapshader (Mbitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);//Outer rectangle top left, top right, bottom right, bottom left corner radius        float[] outerradii = { -, -, -, -, -, -, the, $};//inner rectangle and outer rectangle margin, left, top, right, bottomRECTF inset =NewRECTF ( -, -, $, $);//Inner rectangular fillet radius        float[] innerradii = { -, -, -, -, -, -, -, -};//These three parameters are set according to requirementsMshape =NewRoundrectshape (outerradii, inset, innerradii);//Build the Shapedrawable object and define the shape as a rounded rectangleMshapedrawable =NewShapedrawable (Mshape); }@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);//Get brushes and set rendererMshapedrawable.getpaint (). Setshader (Mbitmapshader);//Set display areaMshapedrawable.setbounds ( -, -, +, +);//Draw shapedrawableMshapedrawable.draw (canvas); }

About Xfermode's actual combat, tomorrow to add, sleepy ...

Android custom Control (ii) Bitmapshader, shapedrawable, Shape

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.