An Android implementation of a CSS effect

Source: Internet
Author: User

Big New Year's, to look at the effect:


Remember to see a CSS effect, the mouse over the circle, the circle is divided into four sub-circles, the sub-circle and the original circle occupy the same rectangle. This is an Android version of mouse-over events that turn into finger touch events. Nonsense not much to say, look at the code:

Package Com.sovnem.fabulouscircle;import Java.util.arraylist;import Java.util.random;import Android.content.context;import Android.content.res.resources;import Android.graphics.canvas;import Android.graphics.paint;import Android.util.log;import Android.view.motionevent;import Android.view.View;public Class Fabulousview extends View {int[] colors;private Random Rand; Arraylist<circle> Circles;boolean ismeasure; Paint Paint;public Fabulousview (context context) {super (context); init ();} private void Init () {Resources res = GetContext (). getresources (); colors = new int[] {Res.getcolor (Android. R.color.holo_blue_bright), Res.getcolor (Android. R.color.holo_green_light), Res.getcolor (Android. R.color.holo_orange_light), Res.getcolor (Android. R.color.holo_blue_dark), Res.getcolor (Android. R.color.holo_red_light), Res.getcolor (Android. R.color.holo_red_dark), Res.getcolor (Android. R.color.holo_blue_dark)};rand = new Random (), circles = new arraylist<fabulousview.circle> ();p aint = new paint ();} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); int L = Circles.size (); for (int i = 0; i < L; i++) {C Ircle C = circles.get (i); LOG.I ("info", "" + c.tostring ());p Aint.setcolor (C.color); Canvas.drawcircle (c.x, C.y, C.R, Paint);}} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {if (!ismeasure) {int w = getresources (). g Etdisplaymetrics (). Widthpixels;int h = getresources (). Getdisplaymetrics (). Heightpixels;int radius = Math.min (W, h); Circles.add (New Circle (W/2, H/2, RADIUS/2, Getrandomcolor ())); ismeasure = true;} Super.onmeasure (Widthmeasurespec, heightmeasurespec);} Random one color int getrandomcolor () {return colors[rand.nextint (colors.length)];} @Overridepublic boolean ontouchevent (Motionevent event) {//if (event.getaction () = = motionevent.action_up) handleevent (event); return true;} private void Handleevent (Motionevent event) {float x = Event.getx (); Float y = event.gety (); int L = circles.size (); Arraylist<circle> cs = new Arraylist<fabuLousview.circle> (); Boolean isIn = False;int remove = 0;for (int i = 0; i < L; i++) {Circle c = circles.get (i); if (c. Isinself (x, y)) {isIn = True;cs.addall (C.devide (new int[] {getrandomcolor (), Getrandomcolor (), Getrandomcolor (), Getrandomcolor ()})); remove = i;}} if (isIn) {circles.remove (remove); Circles.addall (CS); invalidate ();}} Class Circle {float x, y;float r;int color;public Circle (float x, float y, float r, int color) {super (); this.x = X;this.y = Y;THIS.R = R;this.color = color;} Arraylist<circle> devide (int[] colors) {arraylist<circle> cs = new arraylist<fabulousview.circle> () ; Cs.add (New Circle (X-r/2, Y-r/2, R/2, Colors[0])), Cs.add (New Circle (x + R/2, Y-r/2, R/2, colors[1]), CS. Add (New Circle (X-r/2, y + R/2, R/2, colors[2]); Cs.add (New Circle (x + r/2, y + r/2, R/2, colors[3]); return CS;} Boolean isinself (float px, float py) {return (THIS.X-PX) * (THIS.X-PX) + (this.y-py) * (this.y-py) <= THIS.R * THIS.R;} @OverridepUblic String toString () {return "Circle [x=" + x + ", y=" + y + ", r=" + R + ", color=" + color + "]";}}} 


An Android implementation of a CSS effect

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.