Android Development's Custom Checkbox_android

Source: Internet
Author: User

The effect to implement is as follows

Consider that the key is animation effect, so direct inheritance View . However, CheckBox the Super class CompoundButton implements the Checkable interface, which is worth learning from.

The following record of the problems encountered, and from the point of view of the source solution.

Question one: Support wrap_content

Because it is directly inherited from View , wrap_content special processing is required.

View measure Process Measurespec:

 /** * A Measurespec encapsulates the layout requirements passed from parent to child.
  * Each MEASURESPEC represents a requirement for either the width or the height. 
  * A MEASURESPEC is comprised of a size and a mode. * Measurespecs are implemented as ints to reduce object allocation.
  This class * are provided to packs and unpack the <size, mode> tuple into the Int.
  * * public static class Measurespec {private static final int mode_shift = 30;

  private static final int mode_mask = 0x3 << mode_shift; /** * Measure Specification Mode:the parent has not imposed any constraint * to the child.
   It can be whatever size it wants.

  * * public static final int UNSPECIFIED = 0 << mode_shift; /** * Measure Specification Mode:the parent has determined a exact size * for the child.
   The "child is" going to be given those bounds regardless * of how big it wants to be.

  * * public static final int exactly = 1 << mode_shift;
   /*** Measure Specification mode:the can be as large as it wants up * to the specified size.

  * * public static final int at_most = 2 << mode_shift;
   /** * Extracts the mode from the supplied measure specification. * * @param measurespec The measure specification to extract the "mode from * @return {@link android.view.View.Measure Spec#unspecified}, * {@link android.view.view.measurespec#at_most} or * {@link android.view.view.measurespec#exa
  ctly} */public static int getmode (int measurespec) {return (Measurespec & Mode_mask);
   }/** * Extracts the size from the supplied measure specification. * * @param measurespec The measure specification to extract the size from * @return the size in pixels defined in th E supplied measure specification/public static int getsize (int measurespec) {return (Measurespec & ~mode_m
  ASK); }
 }

Knowing from the documentation that Android is designed to save memory, MeasureSpec It's made up of mode size two parts, and so much is done to convey the requirements of the long breadth from the parent container to the child view.

Mode has three different modes:

1,UNSPECIFIED: The parent container does not have any restrictions on the width of the view

2,exactly: The parent container has specified the exact height for the child view

3,At_most: The parent container specifies the largest width, the child view can not exceed

Wrap_content belongs to the at_most mode.

Take a look at the approximate measure process:

Call measure () first in view and finally call Onmeasure ()

protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
  setmeasureddimension (getdefaultsize ( Getsuggestedminimumwidth (), Widthmeasurespec),
    getdefaultsize (Getsuggestedminimumheight (), Heightmeasurespec));
 }

setMeasuredDimensionSet view the width of the height. Take another look.getDefaultSize()

public static int getdefaultsize (int size, int measurespec) {
  int = size;
  int specmode = Measurespec.getmode (measurespec);
  int specsize = measurespec.getsize (measurespec);

  Switch (specmode) {case
  measurespec.unspecified: result
   = size;
   break;
  Case Measurespec.at_most: Case
  measurespec.exactly: result
   = specsize;
   break;
  return result;
 }

Because wrap_content it belongs to the pattern AT_MOST , so the width is high, that is, the specSize parent container size , and this is match_parent the same. The wrap_content general idea of the support is to rewrite the onMeasure() specific point, which mimics the getDefaultSize() gain in width and height.

 @Override
 protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
  int widthmode = Measurespec.getmode (WIDTHMEASURESPEC);
  int widthsize = measurespec.getsize (widthmeasurespec);
  int heightmode = Measurespec.getmode (heightmeasurespec);
  int heightsize = measurespec.getsize (heightmeasurespec);

  int width = widthsize, height = heightsize;

  if (Widthmode = = measurespec.at_most) {
   width = dp2px (default_size);
  }

  if (Heightmode = = measurespec.at_most) {
   height = dp2px (default_size);
  }
  Setmeasureddimension (width, height);
 }

Question two: Path.addpath () combined with pathmeasure

Give an example to illustrate the problem:

 Mtickpath.addpath (Entrypath);
 Mtickpath.addpath (Leftpath);
 Mtickpath.addpath (Rightpath);
 Mtickmeasure = new Pathmeasure (Mtickpath, false);
 Mtickmeasure is a pathmeasure

Although it's mTickPath now made up of three, it's the same as the path mTickMeasure length entryPath length, and I'm surprised to be here. Look at getLength() the source code:

 /** * return "total
  length of" The current contour, or 0 if no path are
  * associated with this measure Object.
   */Public
 Float GetLength () {return
  native_getlength (native_instance);
 }

From a comment, you get the current contour total length.

getLengthCalled the native layer of methods, to here have to look at the bottom of the implementation.

By reading the source code found, Path and PathMeasure actual respectively corresponding to the underlying SKPath and SKPathMeasure .

View native The source of the layer getLength() :

 Skscalar skpathmeasure::getlength () {
  if (Fpath = NULL) {return
   0;
  }
  if (Flength < 0) {
   this->buildsegments ();
  }
  Skassert (flength >= 0);
  return flength;
}

Actually called to buildSegments() assign a fLength value, here the underlying design has a very smart place-- SKPathMeasure fLength special handling for initialization:

Skpathmeasure::skpathmeasure (const skpath& PATH, bool forceclosed) {
 Fpath = &path;
 Flength =-1; Signal we need to compute it
 fforceclosed = forceclosed;
 Ffirstptindex =-1;

 Fiter.setpath (path, forceclosed);

When fLength=-1 we need to compute, that is to say, when the method has not been executed getLength() , it fLength is always 1, and once executed fLength>=0 , the next time it is not executed, buildSegments(), this avoids the recurrence of the calculation.

Intercept buildsegments () part of the code:

void Skpathmeasure::buildsegments () {skpoint pts[4];
 int ptindex = Ffirstptindex;
 Skscalar distance = 0;
 bool isclosed = fforceclosed;
 BOOL Firstmoveto = Ptindex < 0;

 segment* seg; /* NOTE: * As we accumulate distance, we have to check this result of = * actually made it larger, since a very SMA
 ll Delta might is > 0, but * still have no effect on distance (if distance >>> delta).
 * We do the check below, and in Compute_quad_segs and Compute_cubic_segs * Fsegments.reset ();
 bool done = false;
    Do {switch (Fiter.next (pts)) {case Skpath::kmove_verb:ptindex + 1;
    Fpts.append (1, pts);
     if (!firstmoveto) {done = true;
    Break
    } Firstmoveto = false;

   Break
    Case Skpath::kline_verb: {skscalar d = skpoint::D istance (Pts[0], pts[1]);
    Skassert (d >= 0);
    Skscalar prevd = distance;
    Distance = D;
     if (Distance > prevd) {seg = Fsegments.append (); Seg->fdistance = Distance;
     Seg->fptindex = Ptindex;
     Seg->ftype = Kline_segtype;
     Seg->ftvalue = Kmaxtvalue;
     Fpts.append (1, pts + 1);
    ptindex++;

   }} break;
    Case Skpath::kquad_verb: {skscalar prevd = distance;
    Distance = This->compute_quad_segs (pts, distance, 0, Kmaxtvalue, ptindex);
     if (Distance > Prevd) {fpts.append (2, pts + 1);
    Ptindex + 2;

   }} break;
    Case Skpath::kconic_verb: {const skconic conic (pts, Fiter.conicweight ());
    Skscalar prevd = distance;
    Distance = This->compute_conic_segs (conic, distance, 0, Kmaxtvalue, ptindex);  if (Distance > PREVD) {//We store the conic weight in our next point, followed by the last 2 pts//thus to Reconstitue a conic, you ' d need to say/Skconic (Pts[0], pts[2], pts[3], weight = pts[1].fx) Fpts.append ()
     ; set (CONIC.FW, 0);
     Fpts.append (2, pts + 1);
    Ptindex + 3;

   }} break; Case Skpath::kcubic_verb: {skscalar PREVD= distance;
    Distance = This->compute_cubic_segs (pts, distance, 0, Kmaxtvalue, ptindex);
     if (Distance > Prevd) {fpts.append (3, pts + 1);
    Ptindex + 3;

   }} break;
    Case skpath::kclose_verb:isclosed = true;

   Break
    Case Skpath::kdone_verb:done = true;
  Break

 } while (!done);
 Flength = distance;
 fisclosed = isclosed; Ffirstptindex = Ptindex;

The code is longer and needs to be considered slowly. fIteris a Iter type, in SKPath.h the declaration:

/* Iterate through all of the segments (lines, Quadratics, cubics) of each
contours in a path.
The iterator cleans up the segments along the way, removing degenerate, segments and
adding close verbs where necessary . When the Forceclose
argument are provided, each contour (as defined by a new starting move
command) would be complet Ed with a close verb regardless of the
contour ' s contents./

It is clear from this statement that ITER's role is to traverse path every one in the contour . Look at the Iter.next() method:

 Verb next (Skpoint pts[4], bool Doconsumedegerates = true) {
   if (doconsumedegerates) {
    this-> Consumedegeneratesegments ();
   }
   Return This->donext (pts);
 

The return value is a Verb type:

Enum Verb {
 kmove_verb,  //!< iter.next returns 1 point
 Kline_verb,  //!< Iter.next returns 2 points
 Kquad_verb,//!< Iter.next returns 3 points
 Kconic_verb,//!< Iter.next returns 3 points + Iter.conicweight ()
 Kcubic_verb,//!< Iter.next returns 4 points
 Kclose_verb,//!< iter.next returns 1 point (contour ' s Movet o PT)
 Kdone_verb,  //!< iter.next returns 0 points
}

No matter what type it is, Path it must be composed of dots, and if it is a line, then two points, and the Bezier curve is three points, and so on.

doNext()The code of the method is not posted, the function is the type of judgment and the coordinates of the contour corresponding points are taken out and passed topts[4]

When fIter.next() returned kDone_Verb , one traversal ends.

buildSegmentsThe loop is doing it, and from the pattern, it case kLine_Verb distance += d 's not hard to see that it length 's cumulative. In the example mTickPath given, there are three contour ( mEntryPath , mLeftPath , mRightPath ), and when we call, we mTickMeasure.getLength() first accumulate the mEntryPath contour length of this.

It is not difficult to explain why the length of the acquisition is the mTickMeasure mEntryPath same. So think about it, how do you make buildSegments() the next contour operation? The key is to put the fLength 1.

/** move to the next contour in the path. Return true if one exists, or false if we do with the
 path.
* *
bool Skpathmeasure::nextcontour () {
 flength =-1;
 return this->getlength () > 0;
}

nativethe API corresponding to the layer isPathMeasure.nextContour()

Summarize

These are the full contents of the Custom checkbox for Android development, and I hope this article will help you develop Android.

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.