The Andoird uses Android's own components, like Slidingdrawer and drawerlayout, are drawer-effect menus, but many of the items to be implemented have been limited by these self-contained components of Android, which makes it difficult to meet the requirements of the project. Custom components, all aspects under their own control, so as to make adjustments to the requirements. To achieve good results, basically the Android based Ontouch event itself to achieve the function of the response.
First of all, let us first look at the overall effect:
Sliding acceleration effects are some, specific experience, can only be installed to view.
Next, look at the code:
The code extends 2 classes from the mainactivity: Maincontroller and Mainview,maincontroller to handle the control layer, Mainview to operate the presentation layer.
Main code:
code for Mainactivity:
package com.example.wz;
Import Com.example.wz.controller.MainController;
Import Com.example.wz.util.MyLog;
Import Com.example.wz.view.MainView;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.MotionEvent;
public class Mainactivity extends activity {public MyLog log = new MyLog (this, true);
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
LOG.E ("You are welcome to join the test project.");
Link ();
Public Maincontroller Maincontroller;
Public Mainview Mainview;
private void Link () {This.maincontroller = new Maincontroller (this);
This.mainview = new Mainview (this);
This.mainController.thisView = This.mainview;
This.mainView.thisController = This.maincontroller;
This.mainView.initViews ();
@Override public boolean ontouchevent (Motionevent event) {super.ontouchevent (event);
Return Maincontroller.ontouchevent (event); }
}
Code for Maincontroller:
Package Com.example.wz.controller;
Import Android.view.GestureDetector;
Import Android.view.GestureDetector.SimpleOnGestureListener;
Import android.view.MotionEvent;
Import com.example.wz.MainActivity;
Import Com.example.wz.util.MyLog;
Import Com.example.wz.util.OpenLooper;
Import Com.example.wz.util.OpenLooper.LoopCallback;
Import Com.example.wz.view.MainView;
public class Maincontroller {public MyLog log = new MyLog (this, true);
Public mainactivity mainactivity;
Public Maincontroller Thiscontroller;
Public Mainview Thisview;
Public Gesturedetector mgesture;
Public Maincontroller (mainactivity mainactivity) {this.mainactivity = mainactivity;
This.thiscontroller = this;
Mgesture = new Gesturedetector (mainactivity, New Gesturelistener ());
Openlooper = new Openlooper ();
Openlooper.createopenlooper ();
Loopcallback = new Listloopcallback (openlooper);
Openlooper.loopcallback = Loopcallback; public class Touchstatus {public int None = 4, down = 1, HorizoNtal = 2, Vertical = 3, up = 4;//longpress = 5 public int state = None;
Public Touchstatus touchstatus = new Touchstatus ();
public class Bodystatus {public int Fixed = 0, dragging = 1, homing = 2, flinghoming = 3, boundaryhoming = 4;
public int state = Fixed;
Public Bodystatus bodystatus = new Bodystatus ();
public class Drawstatus {public int Closed = 0, Open = 1, goclosing = 2, goopening = 3;
public int state = Closed;
Public Drawstatus drawstatus = new Drawstatus ();
public class Areastatus {public int A = 0, B = 1;
public int state = A;
Public Areastatus areastatus = new Areastatus ();
public float touch_pre_x;
public float touch_pre_y;
public float Currenttranslatex;
public boolean ontouchevent (Motionevent event) {int action = event.getaction ();
float x = Event.getx ();
Float y = event.gety ();
if (action = = motionevent.action_down) {this.touch_pre_x = x;
this.touch_pre_y = y; if (touchstatus.state = = Touchstatus.None) {touchstatus.state = Touchstatus.down;
LOG.E ("Down");
if (x > Thisview.maxtranslatex) {areastatus.state = areastatus.b;
else if (x <= thisview.maxtranslatex) {areastatus.state = AREASTATUS.A;
}} else if (action = = motionevent.action_move) {floatδy = (y-touch_pre_y);
Floatδx = (x-touch_pre_x);
if (touchstatus.state = = Touchstatus.down) {if (Δx *δx +δy *δy >) {if (Δx *δx >δy *δy) {
Touchstatus.state = Touchstatus.horizontal;
else {touchstatus.state = touchstatus.vertical;
} touch_pre_x = x;
touch_pre_y = y;
LOG.E ("Action_move");
} else if (touchstatus.state = touchstatus.horizontal) {Currenttranslatex +=δx;
this.touch_pre_x = x;
this.touch_pre_y = y;
if (Currenttranslatex-thisview.maxtranslatex <= 0 && currenttranslatex >= 0) {setposition ();
} log.e ("Horizontal"); Bodystatus.state = BodystATUs.
dragging;
else if (touchstatus.state = = touchstatus.vertical) {log.e ("Vertical");
Bodystatus.state = bodystatus.dragging;
} else if (action = = motionevent.action_up) {log.e ("action_up"); if (bodystatus.state = = bodystatus.dragging) {if (touchstatus.state = = touchstatus.horizontal) {Bodystatus.stat
e = bodystatus.homing;
Openlooper.start (); else if (touchstatus.state = = touchstatus.vertical) {if drawstatus.state = = Drawstatus.open && areastatus
. state = = areastatus.b) {bodystatus.state = bodystatus.homing;
Drawstatus.state = drawstatus.goclosing;
Openlooper.start (); }} else if (touchstatus.state = touchstatus.down && areastatus.state = = areastatus.b) {bodystatus.
state = bodystatus.homing;
Drawstatus.state = drawstatus.goclosing;
Openlooper.start ();
} touchstatus.state = Touchstatus.up;
} mgesture.ontouchevent (event);
return true; } class GesturelisteNER extends Simpleongesturelistener {@Override public boolean onfling (Motionevent E1, motionevent E2, float velocity X, float velocityy) {if (Velocityx * Velocityx + velocityy * velocityy > 250000) {if (Velocityx * Velocityx &G T
Velocityy * velocityy) {log.e ("velocityx--" + Velocityx); if (drawstatus.state = = drawstatus.closed && Velocityx < 0) {} else if (drawstatus.state = = Drawstatus.op
En && velocityx > 0 {} else {dxspeed = Velocityx;
Bodystatus.state = bodystatus.flinghoming;
Openlooper.start ();
} else {log.e ("velocityy");
} return true;
The public void Onlongpress (Motionevent event) {} public boolean Ondoubletap (Motionevent event) {return false;
public boolean ondoubletapevent (Motionevent event) {return false;
public boolean Onsingletapup (Motionevent event) {return false; @Override public boolean onsingletapconfirmed (MotionevenT event) {return false;
public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float Distancey) {return false;
} public void SetPosition () {ThisView.v1.setTranslationX (Currenttranslatex-thisview.maxtranslatex);
ThisView.v2.setTranslationX (Math.Abs (Currenttranslatex));
float transletespeed = 3f;
Openlooper openlooper = null;
Loopcallback loopcallback = null; public class Listloopcallback extends Loopcallback {public listloopcallback (Openlooper openlooper) {Openlooper.supe
R (); @Override public void loop (double ellapsedmillis) {if (bodystatus.state = = bodystatus.homing) {Hommingview
((float) ellapsedmillis);
else if (bodystatus.state = = bodystatus.flinghoming) {Flinghomingview ((float) ellapsedmillis);
}} public float ratio = 0.0008f;
public void Flinghomingview (float ellapsedmillis) {Float distance = (float) ellapsedmillis * transletespeed;
Boolean isstop = false; if (DRAWSTATUS.STate = = drawstatus.closed) {drawstatus.state = drawstatus.goopening;
else if (drawstatus.state = = Drawstatus.open) {drawstatus.state = drawstatus.goclosing;
} if (drawstatus.state = = drawstatus.goclosing) {This.currenttranslatex = distance;
if (This.currenttranslatex <= 0) {This.currenttranslatex = 0;
Drawstatus.state = drawstatus.closed;
Isstop = true;
LOG.E ("-------------1");
} else if (drawstatus.state = = drawstatus.goopening) {This.currenttranslatex + = distance;
if (This.currenttranslatex >= thisview.maxtranslatex) {This.currenttranslatex = Thisview.maxtranslatex;
Drawstatus.state = Drawstatus.open;
Isstop = true;
LOG.E ("-------------2");
} setposition ();
if (isstop) {openlooper.stop ();
} public float Dxspeed; public void Dampenspeed (long Deltamillis) {if (this.dxspeed!= 0.0f) {this.dxspeed *= (1.0f-0.002f * deltamillis
); if (Math.Abs (this.dxspeed) < 50f) this.Dxspeed = 0.0f;
} public void Hommingview (float ellapsedmillis) {Float distance = (float) ellapsedmillis * transletespeed;
Boolean isstop = false; if (drawstatus.state = = drawstatus.closed && This.currenttranslatex < THISVIEW.MAXTRANSLATEX/5) {This.cur
Renttranslatex-= distance;
if (This.currenttranslatex <= 0) {This.currenttranslatex = 0;
Drawstatus.state = drawstatus.closed;
Isstop = true;
} else if (drawstatus.state = drawstatus.closed && This.currenttranslatex >= thisview.maxtranslatex/5) {
This.currenttranslatex + = distance;
if (This.currenttranslatex >= thisview.maxtranslatex) {This.currenttranslatex = Thisview.maxtranslatex;
Drawstatus.state = Drawstatus.open;
Isstop = true; } else if (drawstatus.state = Drawstatus.open && This.currenttranslatex < THISVIEW.MAXTRANSLATEX/5 * 4)
{This.currenttranslatex-= distance; if (This.currenttranslatex <= 0) {this.Currenttranslatex = 0;
Drawstatus.state = drawstatus.closed;
Isstop = true; } else if (drawstatus.state = Drawstatus.open && this.currenttranslatex >= THISVIEW.MAXTRANSLATEX/5 * 4)
{This.currenttranslatex + = distance;
if (This.currenttranslatex >= thisview.maxtranslatex) {This.currenttranslatex = Thisview.maxtranslatex;
Drawstatus.state = Drawstatus.open;
Isstop = true;
} else if (drawstatus.state = drawstatus.goclosing) {This.currenttranslatex = distance;
if (This.currenttranslatex <= 0) {This.currenttranslatex = 0;
Drawstatus.state = drawstatus.closed;
Isstop = true;
} setposition ();
if (isstop) {openlooper.stop ();
LOG.E ("Looper stop ...");
}
}
}
Code for Mainview:
Package Com.example.wz.view;
Import Android.graphics.Color;
Import Android.util.DisplayMetrics;
Import Android.view.ViewGroup.LayoutParams;
Import Android.widget.RelativeLayout;
Import Android.widget.TextView;
Import com.example.wz.MainActivity;
Import COM.EXAMPLE.WZ.R;
Import Com.example.wz.controller.MainController;
Import Com.example.wz.util.MyLog;
public class Mainview {public MyLog log = new MyLog (this, true);
Public mainactivity mainactivity;
Public Maincontroller Thiscontroller;
Public Mainview Thisview;
Public Mainview (mainactivity mainactivity) {this.mainactivity = mainactivity;
This.thisview = this;
Public Displaymetrics Displaymetrics;
public float screenwidth;
public float screenheight;
public float density;
public float Maxtranslatex;
Public Relativelayout Maxview;
Public Relativelayout v1;
Public Relativelayout v2;
public void Initviews () {this.displaymetrics = new displaymetrics (); This.mainActivity.getWindowManager (). GetdefaultdisPlay (). Getmetrics (This.displaymetrics);
This.screenheight = This.displayMetrics.heightPixels;
This.screenwidth = This.displayMetrics.widthPixels;
this.density = this.displayMetrics.density;
This.maxtranslatex = This.screenwidth * 0.8f;
This.mainActivity.setContentView (R.layout.activity_main);
This.maxview = (relativelayout) this.mainActivity.findViewById (R.id.maxview);
V1 = new Relativelayout (mainactivity);
V1.setbackgroundcolor (color.red); Relativelayout.layoutparams params1 = new Relativelayout.layoutparams (int) This.maxtranslatex, Layoutparams.match_
PARENT);
This.maxView.addView (v1, params1);
TextView T1 = new TextView (mainactivity);
T1.settext ("left menu bar");
T1.settextcolor (Color.White);
V1.addview (t1);
V1.settranslationx (0-this.maxtranslatex);
v2 = new Relativelayout (mainactivity);
V2.setbackgroundcolor (Color.parsecolor ("#0099cd")); Relativelayout.layoutparams params2 = new Relativelayout.layoutparams (int) this.screenwidth, LAYOUTPARAMS.MAtch_parent);
This.maxView.addView (v2, PARAMS2);
V2.settranslationx (0);
TextView t2 = new TextView (mainactivity);
T2.settext ("body content");
T2.settextcolor (Color.White);
V2.addview (T2);
}
}
Log Management class MyLog:
Package com.example.wz.util;
Import Android.util.Log;
public class MyLog {public static Boolean Isglobalturnon = true;
public Boolean Isturnon = true;
Public String tag = null;
Public MyLog (String tag, Boolean isturnon) {This.tag = tag;
This.isturnon = Isturnon;
Public MyLog (Object clazz, Boolean isturnon) {This.tag = Clazz.getclass (). Getsimplename ();
This.isturnon = Isturnon;
public void V (String message) {THIS.V (this.tag, message);
public void D (String message) {THIS.D (this.tag, message);
} public void I (String message) {this.i (this.tag, message);
public void W (String message) {THIS.W (this.tag, message);
public void E (String message) {THIS.E (this.tag, message);
public void V. (string tag, string message) {if (Isturnon && isglobalturnon) {LOG.V (tag, message);
} public void D (string tag, string message) {if (Isturnon && isglobalturnon) {LOG.D (tag, message); } public void I (STRing tag, String message} {if (Isturnon && isglobalturnon) {LOG.I (tag, message);
} public void W (string tag, string message) {if (Isturnon && isglobalturnon) {LOG.W (tag, message);
} public void E (string tag, string message) {if (Isturnon && isglobalturnon) {LOG.E (tag, message);
}
}
}
Core class Openlooper to achieve animation effects:
Package com.example.wz.util;
Import Android.annotation.TargetApi;
Import Android.os.Build;
Import Android.os.Handler;
Import Android.os.SystemClock;
Import Android.view.Choreographer;
public class Openlooper {public legacyandroidspringlooper legacyandroidspringlooper = null;
Public Choreographerandroidspringlooper choreographerandroidspringlooper = null;
Public Loopcallback loopcallback = null; public void Createopenlooper () {if (Build.VERSION.SDK_INT >= build.version_codes.
Jelly_bean) {choreographerandroidspringlooper = new choreographerandroidspringlooper ();
else {legacyandroidspringlooper = new legacyandroidspringlooper (); }} public void Start () {if (choreographerandroidspringlooper!= null) {Choreographerandroidspringlooper.start ()
;
else if (legacyandroidspringlooper!= null) {Legacyandroidspringlooper.start ();
} public void Stop () {if (choreographerandroidspringlooper!= null) {choreographerandroidspringlooper.stop (); else if (legacyandroidspringlooper!= null) {legacyandroidspringlooper.stop (); The public class Loopcallback {The public void loop (double ellapsedmillis) {}} is public void loop (double ellapsed)
Millis) {if (this.loopcallback!= null) {This.loopCallback.loop (ellapsedmillis);
} public class Legacyandroidspringlooper {public Handler mhandler;
Public Runnable mlooperrunnable;
public Boolean mstarted;
public long mlasttime;
Public Legacyandroidspringlooper () {Initialize (new Handler ());
public void Initialize (Handler Handler) {mhandler = Handler;
mlooperrunnable = new Runnable () {@Override public void run () {if (!mstarted) {return;
Long currenttime = Systemclock.uptimemillis ();
Loop (Currenttime-mlasttime);
Mhandler.post (mlooperrunnable);
}
};
public void Start () {if (mstarted) {return;
} mstarted = true;
Mlasttime = Systemclock.uptimemillis (); MhandleR.removecallbacks (mlooperrunnable);
Mhandler.post (mlooperrunnable);
public void Stop () {mstarted = false;
Mhandler.removecallbacks (mlooperrunnable); }} @TargetApi (Build.version_codes.
Jelly_bean) public class Choreographerandroidspringlooper {public choreographer Mchoreographer;
Public Choreographer.framecallback Mframecallback;
public Boolean mstarted;
public long mlasttime;
Public Choreographerandroidspringlooper () {Initialize (choreographer.getinstance ());
public void Initialize (choreographer choreographer) {mchoreographer = choreographer; Mframecallback = new Choreographer.framecallback () {@Override public void Doframe (long Frametimenanos) {if (!mstarted)
{return;
Long currenttime = Systemclock.uptimemillis ();
Loop (Currenttime-mlasttime);
Mlasttime = currenttime;
Mchoreographer.postframecallback (Mframecallback);
}
}; public void Start () {if (mstarted) {RetuRn
} mstarted = true;
Mlasttime = Systemclock.uptimemillis ();
Mchoreographer.removeframecallback (Mframecallback);
Mchoreographer.postframecallback (Mframecallback);
public void Stop () {mstarted = false;
Mchoreographer.removeframecallback (Mframecallback);
}
}
}
Reprinted from: http://blog.csdn.net/qxs965266509
SOURCE Download: Drawer effect
The above is the entire content of this article, I hope to help you learn.