Public classMainactivityextendsrelativelayout{PrivateScroller Scroller; Privatecontext Context; Private intwidth; Private intheight; PrivateImageView ImageView; Private intMlastdowny = 0; Private intMcurryy; Private intmdely; Private BooleanMcloseflag =false; PublicMain_skip (Context context, AttributeSet attrs) {Super(context, attrs); This. Context =context; Initview (); } Private voidInitview () {//This interpolator you can set something else I chose here is a bounce effect interpolatorBounceinterpolator interpolator =NewBounceinterpolator (); Scroller=NewScroller (Context,interpolator); WindowManager Manager=(WindowManager) context. Getsystemservice (Context.window_service); Displaymetrics Metrics=NewDisplaymetrics (); Manager.getdefaultdisplay (). Getmetrics (metrics); Width=Metrics.widthpixels; Height=Metrics.heightpixels; //Here you have to set a transparent background, otherwise it will affect you to see the underlying layout This. SetBackgroundColor (Color.argb (0, 0, 0, 0)); ImageView=NewImageView (context); Layoutparams params=Newlayoutparams (layoutparams.match_parent, layoutparams.match_parent); Imageview.setlayoutparams (params); Imageview.setscaletype (SCALETYPE.FIT_XY); Imageview.setimageresource (R.DRAWABLE.A); AddView (ImageView); } Public voidSetbgimg (intResID) {Imageview.setimageresource (ResID); } Public voidsetbgimg (drawable drawable) {imageview.setimagedrawable (drawable); } //push the door to the animation Public voidSetanimation (intStarty,intDyintduration) {Scroller.startscroll (0, Starty, 0, dy, duration); Invalidate (); } @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:mLastDownY= (int) event.gety (); System.err.println ("Action_down=" +mlastdowny); return true; CaseMotionEvent.ACTION_MOVE:mCurryY= (int) event.gety (); System.err.println ("Action_move=" +mcurryy); Mdely= Mcurryy-Mlastdowny; if(Mdely < 0) {ScrollTo (0,-mdely); System.err.println ("------------- " +mdely); } Break; CaseMotionEvent.ACTION_UP:mCurryY= (int) event.gety (); Mdely= Mcurryy-Mlastdowny; if(Mdely < 0){ if(Math.Abs (mdely) >HEIGHT/2) {setanimation ( This. getscrolly (), height, 450); Mcloseflag=true; }Else{setanimation ( This. getscrolly (),- This. getscrolly (), 1000); } } Break; default: Break; } return Super. Ontouchevent (event); } @Override Public voidComputescroll () {if(Scroller.computescrolloffset ()) {ScrollTo (Scroller.getcurrx (), Scroller.getcurry ()); //don't forget to update the interfacepostinvalidate (); }Else { if(mcloseflag) { This. setvisibility (View.gone); } } Super. Computescroll (); }}
Android Push Door effect