Android Curious baby _ watch face World _03

Source: Internet
Author: User

Tag: Android Switch Custom Animation

Bored brush posts see a help, try to write a bit.

A custom switch control that comes with an animated effect.

Said to be a control, is actually a layout container, first on:


First, the principle, and then see HD feed code.

Principle:


There seems to be no principle, sweat ...

As with other custom container controls, it is generally important to note that:

(1) Calculate the size, width and height.

(2) Calculate the layout position of the child view

It's not generally important to note:

(3) animation is used Nineoldandroids

(4) Occlusion effect is by controlling the drawing order of the Sub view


HD Feed Code:

(1) Calculate size:

protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Initialize, call only once if (!isinit) {childleft = Getchildat (0 ); childright = Getchildat (1); Childleft.setonclicklistener (Leftclicklistener); Childright.setonclicklistener ( Rightclicklistener); Measurechild (Childleft, Widthmeasurespec, Heightmeasurespec); Measurechild (ChildRight, Widthmeasurespec, Heightmeasurespec);//Record the width of childleft and childright LEFTCHILDW = Childleft.getmeasuredwidth (); LEFTCHILDH = Childleft.getmeasuredheight (); RIGHTCHILDW = Childright.getmeasuredwidth (); RightChildH = Childright.getmeasuredheight ();//Initialize animation smalltobig = Objectanimator.offloat (null, "ScaleY", new float[] {0.8f, 1}); Bigtosmall = Objectanimator.offloat (null, "ScaleY", new float[] {1, 0.8f}); Lefttorightl = Objectanimator.offloat (childl EFT, "Translationx", new float[] {0, LEFTCHILDW/2}); RIGHTTOLEFTL = Objectanimator.offloat (Childleft, "Translationx", n EW float[] {0}); lefttorightr = Objectanimator.offloat (Childright, "Translationx", new float[] {0, RIGHTCHILDW/2}); Righttoleftr = Objectanimator.offloat (Childright, "Translationx", new float[] {0}); animatorSet = NE W Animatorset (); Animatorset.addlistener (manimatorlistener); isinit = true;} Width is two child width added widthmeasurespec = measurespec.makemeasurespec (Leftchildw + RIGHTCHILDW, measurespec.exactly);// The height is two child the higher of that Heightmeasurespec = Measurespec.makemeasurespec (Leftchildh > RIGHTCHILDH? leftchildh:rightchildh , measurespec.exactly); Setmeasureddimension (Widthmeasurespec, heightmeasurespec);}

look a bit long, actually calculate the size of a few words, other things when the initialization, because some things (such as animation) initialization needs a sub-view of the wide height, so also put in here, I do not know where to put good, Adding a Boolean variable isinit to control things like animations will only be initialized once, onmeasure and onlayout are very high frequency, should avoid the two methods to do a lot of repetition of new operations.

Calculate size Nothing to say, focus on a method that is often used when customizing view:

Measurechild (Childleft, Widthmeasurespec, Heightmeasurespec); Measurechild (Childright, WidthMeasureSpec, HEIGHTMEASURESPEC);
The Measurechild method allows you to measure the child manually to get the child's measurement width.

Note that the measurement is wide, not the final true width, getmeasuredwidth () and getwidth () are different.


(2) Layout sub View

protected void OnLayout (Boolean changed, int l, int t, int r, int b) {switch (currentstate) {///two child movement is through animation, do not need to be here for special Don't deal with it, notice the parameters of the animation. Case State_left_on_top:case STATE_RIGHT_ON_TOP:childLeft.layout (0, 0, LEFTCHILDW, LEFTCHILDH); Childright.layout (LEFTCHILDW-RIGHTCHILDW/2, 0, LEFTCHILDW + RIGHTCHILDW/2, RIGHTCHILDH); Here is the initial state of the judgment and initializes the display if (!isinit2) {if (CurrentState = = state_left_on_top) {////Just childright to be smaller bigtosmall.settarget ( Childright); Bigtosmall.start ();} else {//childleft is smaller and two are shifted to the right bigtosmall.settarget (childleft); animatorset = Null;animatorset = new Animatorset (); Animatorset.addlistener (Manimatorlistener); Animatorset.playtogether (Bigtosmall, Lefttorightl, LeftToRightR); Animatorset.start ();} IsInit2 = True;}}

the layout is nothing to say, and the notes are clearly written.

As the comments say, the two states do not use sub-situation layout, always left to the layout on the line, moving is the animation do things, more than a few times the parameters of the animation, see that row on OK.

Here IsInit2 the same isinit to prevent repetition.

The content in the IF (ISINIT2) is the initial display of the modification, if not, no matter what the state, the initial time is the same:


(3) Animation implementation

public void Changestate () {if (!isaniming) {if (CurrentState = = state_right_on_top) {currentstate = State_left_on_top;if ( Mstatechangelistener! = null) {Mstatechangelistener.onstatechange (currentstate);} Smalltobig.settarget (Childleft); Bigtosmall.settarget (childright); Animatorset.playtogether (SmallToBig, Bigtosmall, RIGHTTOLEFTL, RIGHTTOLEFTR); Animatorset.start ();} else {currentstate = state_right_on_top;if (Mstatechangelistener! = null) {Mstatechangelistener.onstatechange ( CurrentState);} Smalltobig.settarget (childright); Bigtosmall.settarget (Childleft); Animatorset.playtogether (SmallToBig, Bigtosmall, Lefttorightl, lefttorightr); Animatorset.start ();}}

Here the animation is implemented with Nineoldandroids.

The animation has already been initialized in the Onmeasure method, so just make a decision about which animation to perform and start ().

The more difficult is the animation initialization parameters, my experience is "try", try a few more times on the line.

(4) Occlusion effect

@Overrideprotected int Getchilddrawingorder (int childCount, int i) {switch (currentstate) {case state_left_on_top:// Childleft on, need to draw childright then draw childleft//after draw will overwrite the first draw, so that Childleft will be in the upper if (i = = 0) {return 1;} else {return 0;} Default:return i;}}

This method overrides the parent class ViewGroup, overriding this method to control the child's drawing order, remembering that the drawing will obscure the first drawing.
Note that this method takes effect before you call another method:

Setchildrendrawingorderenabled (TRUE);//Open ordered drawing child

The use of ViewGroup This feature can achieve a lot of interesting effects, such as multiple child occlusion, click on the top of the display, you can constantly switch the drawing order to achieve similar effects of the carousel.

This allows a custom switch control to be completed.

As I said earlier, this is actually a layout container. The first is that I put two textview in the formation of the effect, put other view is also possible.

    <view        android:id= "@+id/switchview"        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        class=" Jjj.demo.switchviewdemo.SwitchView ">        <textview            android:layout_width=" 60DP "            android:layout_height=" 40DP "            android:background=" @drawable/shape_corner_red "            android:gravity = "Center"            android:text= "on"            android:textcolor= "#ffffff"            android:textsize= "16sp"/>        < TextView            android:layout_width= "60DP"            android:layout_height= "40DP"            android:background= "@drawable /shape_corner_blue "            android:gravity=" center "            android:text=" OFF "            android:textcolor=" #ffffff "            android:textsize= "16sp"/>    </view>

do not know why the custom control on the lower version of the system in the following way often does not display properly:

<jjj.demo.switchviewdemo.switchview ></jjj.demo.switchviewdemo.SwitchView>

So can only use the above <view ></view> this way, do not know just the problem of the simulator or the real machine will also.


If you don't shift the animation, it would be easier to implement it, but you can try to write it if you're unfamiliar with custom controls.


Feel write a lot of questions, there are good suggestions kneeling for guidance comments AH.


Demo download

Android Curious baby _ watch face World _03

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.