The following is the use of scroller to show you the effect of sliding doors
Ideas:
1. Customize a class Pushpulldoorview, inherit relativelayout,
2. Initialize the Scroller object and have a bouncing effect. The display is smoother when you move the painting according to your gestures.
3. In the Ontouchevent method, use Scrollto to move the view while the gesture is moving. When the finger leaves the screen, the animation that slides in the scroller is called.
4. Rewrite the Computescroll method to determine whether the Scroller object animation is over, and if not, use the Scrollto method to move the view continuously until the end of the animation.
Note the point:
When starting the Scroller object movement, to use invalidate to start the refresh interface, then the code will be executed computescroll, do not forget to write Postinvalidate () to refresh the interface, In this way, the entire view moves the effect of the dripping.
The following is explained by the code:
public class Pushpulldoorview extends Relativelayout {private final String TAG = PushPullDoorView.class.getSimpleName (); Private Context mcontext;private Scroller mscroller;//smooth scroll private int mscreenheigh = 0;private int mlastdowny = 0;privat e int Mcurryy;private int mdely;private Boolean mcloseflag = false;//hidden viewprivate ImageView mimgview;private Interpola Tor polator;//scrolling Effect Public Pushpulldoorview (context context) {super (context); mcontext = Context;setupview ();} Public Pushpulldoorview (context context, AttributeSet Attrs) {Super (context, attrs); mcontext = Context;setupview ();} @SuppressLint ("Newapi") private void Setupview () {Initscroller (); Getwindowinfo (); Initimageview ();} public void Initscroller () {polator = new bounceinterpolator ();//Bounce effect, of course, you can also set other effects, for example: accelerated effect Mscroller = new Scroller ( Mcontext, polator);} public void Getwindowinfo () {//Get screen Resolution WindowManager WM = (WindowManager) (Mcontext.getsystemservice (Context.window_ SERVICE));D isplaymetrics dm = new Displaymetrics (); Wm.getdefaulTdisplay (). Getmetrics (DM); mscreenheigh = Dm.heightpixels;} public void Initimageview () {//Here you must set a transparent background, otherwise it will affect you to see the underlying layout This.setbackgroundcolor (Color.argb (0, 0, 0, 0)); Mimgview = New ImageView (Mcontext); Mimgview.setlayoutparams (New Layoutparams (Layoutparams.match_parent,layoutparams.match_ PARENT); Mimgview.setscaletype (ImageView.ScaleType.FIT_XY);//Fill the entire screen mimgview.setimageresource (r.drawable.bg); Default background AddView (Mimgview);} /** * Set Push door background * * @param id */public void setbgimage (int id) {mimgview.setimageresource (id);} /** * Set Push door background * * @param drawable */public void Setbgimage (drawable drawable) {mimgview.setimagedrawable (drawable);} /** * Push the door to animate * * @param starty * Position starting in y direction * @param dy * y-direction distance moved * @param duration * Time */public void Startbounceanim (int starty, int dy, int duration) {mscroller.startscroll (0, starty, 0, dy, duration); invalid Ate ();//must not forget to write this code} @Overridepublic Boolean ontouchevent (Motionevent event) {int action = event.getaction (); switch ( Action{case motionevent.action_down://records the Y-draw coordinate mlastdowny = (int) event.gety (); return true;case Motionevent.action_move: Mcurryy = (int) event.gety () mdely = mcurryy-mlastdowny;//only valid if (mdely < 0) {scrollTo (0,-mdely);//Follow the trigger of the moving event, constantly Move}break;case MotionEvent.ACTION_UP:mCurryY = (int) event.gety () mdely = mcurryy-mlastdowny;if (mdely < 0) {if (Math. ABS (mdely) > Mscreenheigh/2) {//swipe up more than half the screen high to turn on the Vanishing Animation Startbounceanim (this.getscrolly (), Mscreenheigh, 450); Mcloseflag = true;} else {//swipe up no more than half the screen height to turn on flick animation Startbounceanim (This.getscrolly (),-this.getscrolly (), 1000);}} break;} Return Super.ontouchevent (event);} @Overridepublic void Computescroll () {if (Mscroller.computescrolloffset ()) {ScrollTo (Mscroller.getcurrx ()), Mscroller.getcurry ());//Do not forget to update the interface log.d (TAG, "Computescroll");p ostinvalidate ();} else {if (Mcloseflag) {this.setvisibility (View.gone);}}}}
In the construction method is the operation to be done, initialize the scroller, get the screen resolution, add ImageView as the view background, of course, can also be other view, here with ImageView as the background.
Under Ontouchevent's Motionevent.action_move: action, note that the current Y value-motionevent.action_down y is less than 0 to indicate that it is sliding upward, at which point the Scrollto () can be used To move the view.
ScrollTo (x direction position, Y method position), the initialization state is (0,0), when the distance to slide upward is 10, then actually is 10 is the y direction of the difference in the movement, equivalent to the Y direction 10 moved to 0.
When the screen is taken off, when the distance is less than half the height of the screen, then move down, if the distance is greater than half the screen height, then swipe up and hide the view.
In the Computescroll method, because when the hand leaves the screen will call scroller start scrolling, then this will be based on the end of scrolling, constantly moving the screen, of course, this is the role of invalidate and postinvalidate interaction, To constantly refresh and Scrollto () move the view.
Source: http://download.csdn.net/detail/forwardyzk/8327775
:
Android Sliding Door effect (Scroller)