Android inherits existing control extensions implement custom controls Textview_android

Source: Internet
Author: User

In general, Android implements custom controls in three different ways.

  Ⅰ, inherits existing controls, and expands the functionality of its controls.

Ⅱ, combine existing controls to achieve more powerful controls.

Ⅲ, overriding view implementation of new controls

This article focuses on inheriting existing controls to extend the implementation of custom controls. This is a very important custom control method that can stand on the shoulders of the native control giant and expand its functionality, and in general, we can draw native controls in the OnDraw method.

This article will expand TextView as an example to see how we inherited the existing controls, from defining a powerful control. This custom control is a border text box, and the border is not the same color as the background. The general Effect chart is this:

Let's analyze how to achieve this effect. We should now stare at the OnDraw method, rewrite the OnDraw method, and in the OnDraw method, draw two separate rectangular border with a colored background. In this way, the text box will have a layered sense of patchwork.

To have this patchwork effect, the first step we have to do is to define the brush with the inner and outer borders of the drawing, defined by the following code:

private void Initview () {
    mPaint1 = new Paint ();
    Mpaint1.setcolor (Color.Blue);
    Mpaint1.setstyle (Style.fill);

    MPaint2 = new Paint ();
    Mpaint2.setcolor (color.yellow);
    Mpaint2.setstyle (Style.fill);
  }

We have defined the first brush color to be blue, and the brush's fill mode to be fully populated. The second brush color is yellow, and the same fill mode is a full fill.

With different color brushes, all we have to do is use the two brushes to draw different rectangles in the OnDraw method, so the code is like this:

Canvas.drawrect (0, 0, getmeasuredwidth (), Getmeasuredheight (), mPaint1);
    Canvas.drawrect (Getmeasuredwidth ()-getmeasuredheight ()-Ten,
        mPaint2);
    Canvas.save ();
    Canvas.translate (0);
    Super.ondraw (canvas);
    Canvas.restore ();

We drew two rectangles with unequal widths, and the canvas translated 10 units. The custom controls that are drawn here are:

This example, very simple, maybe we have to inherit the original control or the meaning of the words, our hundred feet head further. Make a slightly more complex point. Custom textview--with blinking text TextView.

We analyze the realization of the idea:

① to achieve this effect, we can take full advantage of the Paint object's shader (renderer) object.

② the blinking effect by constantly changing the position of the lineargradient.

With this idea in mind, we first initialize the LinearGradient object in the Onsizechanged method, and the Matrix object for the graphics transformation. The source code is as follows:

if (Mwidth = = 0) {
      mwidth = Getmeasuredwidth ();
      if (Mwidth > 0) {
        mpaint = Getpaint ();
        Mlineargradient = new LinearGradient (0, 0, mwidth, 0,
            new int[] {color.gray, Color.green, color.gray}, NULL,
            Sha Der. Tilemode.clamp);
        Mpaint.setshader (mlineargradient);
        Matrix = new Matrix ();
      }
    

We set the LinearGradient object color to a gray-green, linear gradient object, and the tile mode of the color is tiled.

Then, in the OnDraw method, the position of the linear gradient object is constantly transformed, so that there is the effect of flashing around the text. The source code is as follows:

if (matrix!= null) {
      mtranslate = mtranslate + mwidth/5;
      if (Mtranslate > 2 * mwidth) {
        mtranslate =-mwidth;
      }
      Matrix.settranslate (mtranslate, 0);
      Mlineargradient.setlocalmatrix (matrix);
      postinvalidatedelayed (MB);

    

The resulting effect is:

This is one of the custom view Sanbang-Inherit the existing control a little summary, I hope to help you learn.

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.