Public classMyanimationdemoextendsBaseactivityImplementssensoreventlistener{PrivateView mcirclelight; PrivateObjectanimator Mcirclelightanimator; PrivateCurtainview Mcurtainview; PrivateImageView Mivstar; PrivateObjectanimator Mivstaranimator; Private LongLastshaketime; Private intmanimationcurrentstate; PrivateSensor Macceceratesonsor; PrivateSensormanager Msensormanager; @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy) { }//trigger when accuracy is changed@Override Public voidonsensorchanged (Sensorevent event) {if(Event.sensor.getType () = =type_accelerometer) { floatValue[] =event.values; floatx = value[0]; floaty = value[1]; floatz = value[2]; if(System.currenttimemillis ()-lastshaketime > &&math.abs (x) >17&&math.abs (y) >17& &math.abs (z) >17) {Lastshaketime=System.currenttimemillis (); Startanimation (); } } }//trigger when sensor value changes@Override Public voidonnetworkchanged () {} @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.acrivity_animation_demo); Initview (); Initanimator (); } Private voidInitview () {mcirclelight=(View) Findviewbyid (r.id.animation_light); Mcurtainview=(Curtainview) Findviewbyid (R.id.animation_curtain); Mivstar=(ImageView) Findviewbyid (R.id.animation_star); } Private voidInitanimator () {Mcirclelightanimator=NewObjectanimator (); Mcirclelightanimator.settarget (Mcirclelight); Mcirclelightanimator.setpropertyname ("Rotation"); Mcirclelightanimator.setfloatvalues (0,360); Mcirclelightanimator.setduration (1000); Mcirclelightanimator.setrepeatmode (Valueanimator.restart); Mcirclelightanimator.setinterpolator (Newlinearinterpolator ()); Mcirclelightanimator.addlistener (NewAnimator.animatorlistener () {@Override Public voidOnanimationstart (Animator animation) {} @Override Public voidonanimationend (Animator animation) {Mcurtainview.closewindow (); } @Override Public voidOnanimationcancel (Animator animation) {} @Override Public voidonanimationrepeat (Animator animation) {}}); Mivstaranimator=NewObjectanimator (); //mivstaranimator.offloat (Mivstar, "RotationY", 0,180);Mivstaranimator.settarget (Mivstar); Mivstaranimator.setpropertyname ("Rotatex"); Mivstaranimator.setfloatvalues (0,360); Mivstaranimator.setinterpolator (Newlinearinterpolator ()); Mivstaranimator.setduration (2000); Mivstaranimator.setrepeatmode (Valueanimator.restart); } Private voidstartanimation () {mcurtainview.setvisibility (view.visible); Mcurtainview.openwindow (); Mcirclelightanimator.start (); } @Overrideprotected voidOnresume () {Super. Onresume (); Msensormanager=(Sensormanager) Getapplicationcontext (). Getsystemservice (Sensor_service); if(Msensormanager! =NULL) {Macceceratesonsor=msensormanager.getdefaultsensor (Sensor.type_accelerometer); if(macceceratesonsor!=NULL) {Msensormanager.registerlistener ( This, MACCECERATESONSOR,SENSORMANAGER.SENSOR_DELAY_UI); } }} @Overrideprotected voidOnDestroy () {Super. OnDestroy (); if(Msensormanager! =NULL) {Macceceratesonsor=msensormanager.getdefaultsensor (Sensor.type_accelerometer); if(macceceratesonsor!=NULL) {Msensormanager.unregisterlistener ( This, Macceceratesonsor); } } }}
Remember to write off the sensors when you onpause.
Custom View use Valueanimator to realize curtain opening and closing animation
Public classCurtainviewextendsViewImplementsValueanimator.animatorupdatelistener,animation.animationlistener {intmcurtainstate = 0;//0 Fully open 1 open 2 closed action 3 complete closed PrivatePaint Mpaint; PrivateRECTF MRECTF; PrivateValueanimator Manimator; PublicCurtainview (Context context) { This(Context,NULL); } PublicCurtainview (Context context, @Nullable AttributeSet attrs) { This(Context, attrs,0); } PublicCurtainview (context context, @Nullable AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); Mpaint=NewPaint (); Mpaint.setcolor (Color.White); Manimator=NewValueanimator (); Manimator.setduration (500); Manimator.addupdatelistener ( This); Manimator.setinterpolator (Newdecelerateinterpolator ()); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); Setmeasureddimension (Math.min (Widthmeasurespec,heightmeasurespec), Math.min (Widthmeasurespec,heightmeasurespec) ); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); if(mrectf==NULL) {MRECTF=NewRECTF (0,0, Getmeasuredwidth (), Getmeasuredheight ()); } if(Manimator.getanimatedvalue ()! =NULL){ inti = (int) Manimator.getanimatedvalue (); Canvas.drawarc (MRECTF,90-i,i*2,false, Mpaint);//s Upper Half curtainsCanvas.drawarc (Mrectf,270-i,i*2,false, Mpaint); } } Public voidOpenwindow () {mcurtainstate= 1; Manimator.setintvalues (0,90); Manimator.start (); } Public voidCloseWindow () {mcurtainstate=2; Manimator.setintvalues ((int) Manimator.getanimatedvalue (), 0); Manimator.start (); } @Override Public voidonanimationupdate (valueanimator animation) {invalidate (); } @Override Public voidonanimationrepeat (Animation Animation) {} @Override Public voidOnanimationstart (Animation Animation) {} @Override Public voidonanimationend (Animation Animation) {if(Mcurtainstate ==1) {mcurtainstate= 0; }Else if(mcurtainstate==2) {mcurtainstate=3; } }}
Andro a shake containing objectanimator valueanimator animation effect