Public classMainactivityextendsappcompatactivity {PrivateTextView Kuandu; floatCurtranslationx; floatwidth; floatheight; PrivateButton out; PrivateButton in; Privateview view; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Kuandu=(TextView) Findviewbyid (R.id.kuandu); out=(Button) Findviewbyid (r.id.out); Inch=(Button) Findviewbyid (r.id.in); View=Findviewbyid (R.id.view); Out.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {goout (); } }); In.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {goin (); } });WindowManager Manager= This. Getwindowmanager (); Displaymetrics Outmetrics=NewDisplaymetrics (); Manager.getdefaultdisplay (). Getmetrics (Outmetrics); Width=Outmetrics.widthpixels; Height=Outmetrics.heightpixels; Kuandu.settext ("Width is:" +width+ ", height is" +height); } Private voidgoout () {Curtranslationx=View.gettranslationx (); Objectanimator Animator= Objectanimator.offloat (view, "Translationx", Curtranslationx, width); Animator.setduration (1000); Animator.start (); } Private voidgoin () {Curtranslationx=View.gettranslationx (); Objectanimator Animator= Objectanimator.offloat (view, "Translationx", Curtranslationx, 0); Animator.setduration (1000); Animator.start (); }}
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.example.laoyimou.imagetest.MainActivity"> <ButtonAndroid:id= "@+id/out"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Out" /> <ButtonAndroid:id= "@+id/in"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "in" /> <TextViewAndroid:id= "@+id/kuandu"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> <ViewAndroid:id= "@+id/view"Android:layout_width= "Match_parent"Android:layout_height= "80DP"Android:background= "@color/colorprimary" /></LinearLayout>
Get the screen width, move the view out of the screen and move back