Implement a simple Android switch

Source: Internet
Author: User

Learn about drawing series in the graphics in Android recently. According to the Great God idea. Find gourd painting scoop realized a switch. Under

Record the implementation method:

1. Painting background


Shape. Divided into two semicircle and a rectangle, then the code can be written as:

  Private voidDrawbackground (canvas canvas) {Mbackgroundpaint.SetColor (Mcurrentbackgroundcolor);//Draw the right half circleRECTF RECF= NewRECTF (mtogglewidth/ 2 -Mradius,-mradius, Mtogglewidth/ 2 +Mradius, Mradius); Canvas.DrawArc (RECF,- -, the,true, Mbackgroundpaint);//Draw RectangleRECTF Tangle= NewRECTF (-mtogglewidth / 2,-mradius, Mtogglewidth/ 2, Mradius); Canvas.DrawRect (Tangle, mbackgroundpaint);//Draw the left half circleRECTF RECTF= NewRECTF (-mtogglewidth / 2 -Mradius,-mradius,-mtogglewidth / 2 +Mradius, Mradius); Canvas.DrawArc (RECTF, the,- the,true, Mbackgroundpaint); Canvas.Restore (); }
3. Draw Dots:

Here I am using a ring [purple]+ inner cut ring of solid circle [blue] composition. Perhaps you will say, directly set Panint.setstrokewidth not be able to. This idiot, hehe. Actually, I also want to. But found not ah "more ugly, and my color is not good set, hehe, you can try to know."
The Purple ring is responsible for displaying the inner solid circle, forming a boundary effect. This is very good for white solid circles.

4. Setting the Switch
publicvoidsetOpen(boolean isOpen) {        if (mIsOpen != isOpen) {            mIsOpen = isOpen;            showAnimation(mIsOpen);        }    }

Can see. Here I used the animated display effect, in the Android rampage era, without adding animation will show blunt. For a pear, I was the first one to meet my buddies. When doing similar switches. Directly called the visual Project teacher [is the art] cut two pictures, have to say, that dude very smart very good, I naturally also learned, and later found a good blunt ah, no sense of beauty. So I wrote it myself.

A. The center coordinate of a solid circle:

Can be seen very clearly in the picture. Then I can write the code directly:

 / /mtogglewidth is exactly the length of the rectangular rectangle:  final  float  start = IsOpen? -mtogglewidth/2 : mtogglewidth/2 ; float  end = IsOpen? 

Mtogglewidth/2 :-mtogglewidth/2 ; Valueanimator animator = Valueanimator.offloat (start, end); Animator.setduration (Manimationtime); Animator.setinterpolator (new linearinterpolator ()); Animator.addupdatelistener (new Valueanimator.animatorupdatelistener () { @Override public v OID onanimationupdate (Valueanimator valueanimator) {Mcurrentxpositi On = (float ) Valueanimator.getanimatedvalue (); Setcurrentcolor (); Invalidate (); } }); Animator.start (); }

B. Discoloration of the background:
From the turn off color to the open color, there are too many colors. I'm not sure, okay, you look at this:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmzc2mju3mg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "alt=" here to write a picture description of the narrative "title=" > Clear it, well, this color how to play it????

There is an introduction to a color estimator in a very large number of God's articles, namely:

 ValueAnimator.ofArgb(...)

Originally with this very easy implementation, can I practice the SDK min version number is 14, also really lazy to change, OK, too lazy to change I think of ways, method is this:

I am in the coordinates of the S point I know, then I can calculate the percentage of my animation running, yes, what do you mean? :

  floatpercent21.0f / mToggleWidth;

This view can be intuitive. I also do not speak more, in fact, I am very poor in mathematics, but also special just learn paint, also engaged for a long time, more engaged may be good. Knowing the percentage of animations, we know the animations of random moments:

 float  percent  = (mcurrentxposition + mtogglewidth/2 ) * 1.0  f/mtogglewidth; int  currentr = (int ) ((Mendbgcolorr-        Mstartbgcolorr) * percent  + Mstartbgcolorr); int  currentg = (int ) ((Mendbgcolorg-        mstartbgcolorg) * percent  + mstartbgcolorg); int  currentb = (int ) ((Mendbgcolorb-        Mstartbgcolorb) * percent  + mstartbgcolorb); Mcurrentbackgroundcolor = Color.rgb (Currentr, CURRENTG, CURRENTB);  

The endbgcolor here refers to the background color at the end. And Startbgcolor refers to the background color at the beginning.

Use:

In the XML file, configure:

<micro.com.testpaint.view.ToggleButton        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        app:circle_ring_width="4dp"/>

Call in activity:

ToggleButton button = (ToggleButton) findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            @Override            publicvoidonClick(View view) {                button.setOpen(!button.ToggleIsOpen());            }        });

Then there was the way we clicked.

。。。

Well, we can look at it and expand it on my basis. Just learning something about it. The great God do not spray, needs bark.
Finally, the code.

Implement a simple Android switch

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.