Android faux curtain effect and login interface drag effect (application of Scroller Class) with 2 demo and source code

Source: Internet
Author: User

in Android Learning, action interaction is an important part of the software. Among the scroller is the class that provides the drag effect on the web. For example, some launcher implementation of the sliding screen can be achieved through this class. the following is to say the last scroller class learning after the practice.

If you do not know the Scroller class, click:Android Interface swipe to implement---scroller class from source Code and development documentation (let your layout move up)

Read on to see what follows. You will find that the original implementation is very easy.

I've said it before. Sliding the open Source Library on the widely used side of the--slidinglayer is actually using the implementation of the Scroller class, (slidinglayer: GITHUB), This is the---scroller class used during the implementation of this library. We are able to use this library to achieve the results I want to achieve, but this is used, for the beginning of the study of the people raised little. So I decided to go straight to using the Scroller class to achieve:  1) Curtain unfold and close effect2) Login Interface drag effect (somewhat similar to Popupwindow, but with a drag effect).
with these 2 examples, you probably know the basic usage of the Scroller class and can write some similar effects on your own.
first. On the main code, the last demo source code.
Disclaimer: The resource files in the demo are among the 2 applications downloaded online. Found that the effect is good and can be further intact (for example, the curtain effect, originally without a push-pull effect), extracted the application of resource files to achieve their own. The goal is to better achieve the display effect.

The code has a gaze and description to better understand the implementation process.

There may be some local optimization to do, I hope you forgive me.


:
1) Curtain effectuse: Can be used for advertising walls, bulletin boards and other placesNote: Click Switch can realize the expansion and shutdown function, but also can be pushed through the switch to achieve the effect of unwinding, animation to increase the rebound effect. More real.

2) Login window effectUse : can use the login method at login time selection, menu options, etc., somewhat similar to the Popupwindow with drag effectDescription: Ability to log in button to expand Close Login window. can also be closed by push-pull. Note: The outside of the click window is implemented via callback interface, which is not listed here, can download source code view


learned the Scroller class. Probably you also know what the core code will be, the following list
Core code:
Curtain Effect:

public class Curtainview extends Relativelayout implements ontouchlistener{private static String TAG = "Curtainview";p Riv Ate Context mcontext;/** Scroller drag class */private Scroller mscroller;/** screen height */private int mscreenheigh = 0;/** screen width */pr  ivate int mscreenwidth = 0;/** The coordinate of y when clicked */private int downY = 0;/** drag when y coordinates */private int movey = 0;/** drag when y direction distance */private int scrolly = 0;/** release y coordinates */private int upy = 0;/** ad curtain height */private int curtainheigh = 0;/** Open */private Boolean is Open = false;/** in animated */private boolean ismove = false;/** rope picture */private ImageView img_curtain_rope;/** ad picture */private I Mageview img_curtain_ad;/** rise animation time */private int upduration = 1000;/** drop animation time */private int downduration = 500;public Curt Ainview (Context context) {super (context); init (context);} Public Curtainview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); init (context);} Public Curtainview (context context, AttributeSet Attrs) {Super (context, attrs); init (context);} /* * Initialize */private void init (context context) {This.mcontext = Context;//interpolator set to bounce effect (Bounce: bounce) Interpolator int Erpolator = new Bounceinterpolator (); mscroller = new Scroller (context, interpolator); Mscreenheigh = Basetools.getwindowheigh (context); mscreenwidth = basetools.getwindowwidth (context);// The background is set to Transparent This.setbackgroundcolor (COLOR.ARGB (0, 0, 0, 0)), final view view = Layoutinflater.from (Mcontext). Inflate ( R.layout.curtain, null); Img_curtain_ad = (ImageView) View.findviewbyid (r.id.img_curtain_ad); img_curtain_rope = ( ImageView) View.findviewbyid (r.id.img_curtain_rope); AddView (view); Img_curtain_ad.post (new Runnable () {@ overridepublic void Run () {//TODO auto-generated Method Stubcurtainheigh = Img_curtain_ad.getheight (); LOG.D (TAG, "curtainheigh=" + Curtainheigh); CurtainView.this.scrollTo (0, Curtainheigh);//Note the difference between Scrollby and scrollTo}); Img_curtain_rope.setontouchlistener (this );} /** * Drag animation * @param starty * @param dy vertical distance, rolling y distance * @param duration time */public void Startmoveanim (inT starty, int dy, int duration) {ismove = True;mscroller.startscroll (0, starty, 0, dy, duration); invalidate ();//Notify UI thread of updates} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {//TODO auto-generated method stubsuper.onl Ayout (changed, L, T, R, b);} @Overridepublic void Computescroll () {//infers whether it is still scrolling, and is also scrolling to Trueif (Mscroller.computescrolloffset ()) {ScrollTo ( Mscroller.getcurrx (), Mscroller.getcurry ());//Update interface postinvalidate (); ismove = true;} else {ismove = false;} Super.computescroll ();}  @Overridepublic boolean OnTouch (View V, motionevent event) {//TODO auto-generated method stubif (!ismove) {int offviewy = 0;//the distance between the top of the screen and the top of the layout switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:downY = (int) event.getrawy (); offviewy = DownY-(int) event.getx (); return true;case MotionEvent.ACTION_MOVE:moveY = (int) event.getrawy (); scrolly = Movey-downy; if (scrolly < 0) {//swipe up if (IsOpen) {if (Math.Abs (scrolly) <= img_curtain_ad.getbottom ()-Offviewy) {scrollTo (0,- scrolly);}}} Else{//Swipe down if (!isopen) {if (scrolly <= curtainheigh) {scrollTo (0, curtainheigh-scrolly);}}} Break;case MotionEvent.ACTION_UP:upY = (int) Event.getrawy (), if (Math.Abs (Upy-downy) <) {Onropeclick (); break;} if (DownY > Upy) {//swipe up if (IsOpen) {if (Math.Abs (scrolly) > Curtainheigh/2) {//swipe up more than half screen high to turn on vanishing animation Startmovean Im (This.getscrolly (), (curtainheigh-this.getscrolly ()), upduration); isOpen = false;} else {Startmoveanim (this.getscrolly (),-this.getscrolly (), upduration); isOpen = True;}}} else {//Swipe down if (scrolly > CURTAINHEIGH/2) {//swipe up more than half screen high to turn on vanishing animation Startmoveanim (this.getscrolly (),-this.getscro Lly (), upduration); isOpen = true;} else {Startmoveanim (this.getscrolly (), (curtainheigh-this.getscrolly ()), upduration); isOpen = false;}} Break;default:break;}} return false;} /** * Click on the rope switch, will expand close * Use this method in Ontoch when clicking the event, avoid the click Time Response Ontouch the cohesion of the imperfect influence */public void Onropeclick () {if (IsOpen) { CurtainView.this.startMoveAnim (0, Curtainheigh, upduration);} Else{curtainview.this.startmoveaNim (Curtainheigh,-curtainheigh, downduration);} IsOpen =!isopen;}}

Login Interface:
public class LoginView extends Relativelayout {/** Scroller drag class */private Scroller mscroller;/** screen height */private int mscre  Enheigh = 0;/** screen width */private int mscreenwidth = 0;/** Click when y coordinates */private int downY = 0;/** drag when y coordinates */private int movey = 0;/** drag when y direction distance */private int scrolly = 0;/** release when y coordinates */private int upy = 0;/** is moving */private Boolean ismoving = false;/  * * Layout height */private int viewheight = 0;/** Open */public Boolean isshow = false;/** can be enough to drag */public boolean menabled = true;/**  Click outside to close the interface */public Boolean moutsidetouchable = true;/** rise animation time */private int mduration = 800;private final static String TAG = "LoginView";p ublic LoginView (context context) {super (context); init (context);} Public LoginView (context context, AttributeSet Attrs) {Super (context, attrs); init (context);} Public LoginView (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); init (context);} private void init (context context) {setdescendantfocusability (focus_after_descendants); setfocusablE (true); Mscroller = new Scroller (context); Mscreenheigh = Basetools.getwindowheigh (context); mscreenwidth = Basetools.getwindowwidth (context);//background set to Transparent This.setbackgroundcolor (COLOR.ARGB (0, 0, 0, 0)); final view view = Layoutinflater.from (context). Inflate (R.layout.view_login,null); Layoutparams params = new Layoutparams (layoutparams.match_parent,layoutparams.wrap_content);//Suppose not to give him this, it's the layout of the Match_ The parent does not know how many addview (view, params);//ViewGroup the size,//background set to Transparent This.setbackgroundcolor (COLOR.ARGB (0, 0, 0, 0)); View.post (New Runnable () {@Overridepublic void Run () {//TODO auto-generated Method stubviewheight = View.getheight ();}) ; LoginView.this.scrollTo (0, Mscreenheigh); ImageView btn_close = (ImageView) View.findviewbyid (r.id.btn_close); btn_ Close.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubdismiss ();}});} @Overridepublic boolean onintercepttouchevent (motionevent ev) {if (!menabled) {return false;} Return Super.onintercepttouchevent (Ev);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubswitch (Event.getaction ()) { Case MotionEvent.ACTION_DOWN:downY = (int) event.gety (); LOG.D (TAG, "DownY =" + DownY);//Assuming it is fully displayed. Let the layout get touch listening, assuming not show, touch events do not intercept. Pass down if (isshow) {return true;} Break;case MotionEvent.ACTION_MOVE:moveY = (int) event.gety () scrolly = movey-downy;//swipe down if (scrolly > 0) {if (isshow ) {scrollTo (0,-math.abs (scrolly));}} Else{if (Mscreenheigh-this.gettop () <= viewheight &&!isshow) {scrollTo (0, Math.Abs (viewheight-scrolly));}} Break;case MotionEvent.ACTION_UP:upY = (int) event.gety (), if (isshow) {if (this.getscrolly () <=-(VIEWHEIGHT/2)) { Startmoveanim (This.getscrolly (),-(viewheight-this.getscrolly ()), mduration); isshow = false; LOG.D ("Isshow", "false");} else {Startmoveanim (this.getscrolly (),-this.getscrolly (), mduration); isshow = true; LOG.D ("Isshow", "true");}} LOG.D ("this.getscrolly ()", "" "+this.getscrolly ()); changed (); Break;case MOTIONEVENT.ACTION_OUTSIDE:LOG.D (TAG, "Action_outside"); break;default:break;} Return Super.ontouchevent (event);}  /** * Drag animation * @param starty * @param dy move to a point y coordinate distance * @param duration time */public void Startmoveanim (int starty, int dy, int duration) {ismoving = True;mscroller.startscroll (0, starty, 0, dy, duration); invalidate ();//Notify UI thread of update}@ overridepublic void Computescroll () {if (Mscroller.computescrolloffset ()) {ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ());//Update interface postinvalidate (); ismoving = true;} else {ismoving = false;} Super.computescroll ();} /** interface */public void Show () {if (!isshow &&!ismoving) {LoginView.this.startMoveAnim (-viewheight, Viewheight, mduration); isshow = true; LOG.D ("Isshow", "true"); changed ();}} /** Close interface */public void Dismiss () {if (isshow &&!ismoving) {LoginView.this.startMoveAnim (0,-viewheight, mduration); isshow = false; LOG.D ("Isshow", "false"); changed ();}} /** whether to open */public Boolean isshow () {return isshow;} /** get enough to drag */public boolean isslidingenabled () {return menabled;} /** settings canNo enough drag */public void Setslidingenabled (Boolean enabled) {menabled = enabled;}    /** * Set the Listener interface to achieve matte layer effect */public void Setonstatuslistener (Onstatuslistener listener) {This.statuslistener = listener;}    public void Setoutsidetouchable (Boolean touchable) {moutsidetouchable = touchable; }/** * Show state changes when running callback pretext */public void changed () {if (Statuslistener! = null) {if (isshow) {statuslistener.onshow ();} Else{statuslistener.ondismiss ();}}} /** monitoring Interface */public onstatuslistener statuslistener;/** * Listening interface, to monitor the interface change status in the main interface */public interface onstatuslistener{/** Open state * /public void OnShow ();/** off state */public void Ondismiss ();} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {//TODO auto-generated method stubsuper.onl Ayout (changed, L, T, R, b);}}

in fact, the code is similar. Once you know, you can extrapolate your own view to achieve your desired results.

Finally, the source code:

Android faux curtain effect and login interface drag effect (application of Scroller Class) with 2 demo and source code

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.