Iphone-like, right-click to close Activity

Source: Internet
Author: User

Iphone-like, right-click to close Activity

It is easy to use. just inherit suppactivity activity and add android: theme = "@ android: style/Theme. Translucent" to the list.

 

Package com. support; import com. nineoldandroids. animation. animator; import com. nineoldandroids. animation. animatorListenerAdapter; import com. nineoldandroids. animation. objectAnimator; import com. nineoldandroids. animation. valueAnimator; import com. nineoldandroids. animation. valueAnimator. animatorUpdateListener; import android. app. activity; import android. content. context; import android. content. intent; import android. graphics. color; import android. util. attributeSet; import android. view. layoutInflater; import android. view. motionEvent; import android. view. view; import android. view. viewConfiguration; import android. view. window; import android. view. windowManager; import android. widget. frameLayout;/*** base class. All activities must inherit the Activity and declare it in the list. For example, ** do not use android on the application node or the default startup Activity: theme = "@ android: style/Theme. translucent "* @ author Young **/public abstract class SupportActivity extends Activity {private boolean isTouchable = true; // you can right-drag the returned result, the default MAIN Activity to be started must be set to falseprivate FrameLayout rootView; private static final String MAIN = "android. intent. action. MAIN "; private int width; // screen width private final int time = 200; // animation duration public void setContentView (View root) {addContainer (root );} public void setContentView (int layoutRes) {addContainer (LayoutInflater. from (getApplicationContext ()). inflate (layoutRes, null);} private void addContainer (View root) {requestWindowFeature (Window. FEATURE_NO_TITLE); Intent intent = getIntent (); // Add the interface to be displayed to the right-stroke layout. ContainerView frameLayout = new ContainerView (getApplicationContext (); frameLayout. setBackgroundColor (Color. parseColor ("#88000000"); frameLayout. addView (root); rootView = frameLayout; if (intent = null | MAIN. equals (intent. getAction () {frameLayout. canTouch (false); isTouchable = false;} super. setContentView (rootView);} @ Overridepublic void startActivity (Intent intent) {// TODO Auto-generated method stubsuper. startActivity (intent); overridePendingTransition (0, 0); // cancel the animation set by the user or the system's default Activity switching} @ Overridepublic void finish () {if (isTouchable = false) {super. finish (); overridePendingTransition (0, 0); // cancel the return of the animation set by the user or the default Activity switchover;} // TODO Auto-generated method stubObjectAnimator animator = ObjectAnimator. ofFloat (rootView. getChildAt (0), "translationX", 0, width); animator. setDuration (time * 2); animator. addListener (new AnimatorListenerAdapter () {@ Overridepublic void onAnimationEnd (Animator animation) {// TODO Auto-generated method stubsuper. onAnimationEnd (animation); endToFinish () ;}}); animator. addUpdateListener (new AnimatorUpdateListener () {@ Overridepublic void onAnimationUpdate (ValueAnimator animation) {// TODO Auto-generated method stubFloat cost = (Float) animation. getAnimatedValue (); float phosphatase = (float) (1-cost/time/2); rootView. setAlpha (phosphatase); rootView. getChildAt (0 ). setAlpha (phosphatase) ;}}); animator. start (); overridePendingTransition (0, 0);} private void endToFinish () {super. finish () ;}@ Override // cancel the public void overridePendingTransition (int enterAnim, int exitAnim) {super. overridePendingTransition (0, 0 );} /*** right-stroke container * @ author Young **/private final class ContainerView extends FrameLayout {/*** x coordinate */private float downX when pressed; private float downY; private Context context;/*** right-stroke the 8dp on the left */private final int scaledTouchSlop = 8; public ContainerView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); this. context = context; getScreenWidth ();} public ContainerView (Context context, AttributeSet attrs) {super (context, attrs); this. context = context; getScreenWidth ();} public ContainerView (Context context) {super (context); this. context = context; getScreenWidth ();} private void getScreenWidth () {// TODO Auto-generated method stubWindowManager wm = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); width = wm. getdefadisplay display (). getWidth () ;}@ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {if (ev. getAction () = MotionEvent. ACTION_DOWN) {downX = ev. getRawX (); downY = ev. getRawX (); return super. onInterceptTouchEvent (ev); // hand it over to the child control to handle the event} if (ev. getAction () = MotionEvent. ACTION_MOVE) {float x = ev. getRawX (); float y = ev. getRawX (); // ViewConfiguration. get (context ). the getScaledTouchSlop () mobile phone can recognize the minimum sliding distance if (x-downX> Math. abs (y-downY) & Math. abs (x-downX)> = ViewConfiguration. get (context ). getScaledTouchSlop () & downX
 
  
Width/2) {// ObjectAnimator animator = ObjectAnimator. ofFloat (getChildAt (0), "translationX", t, width); animator. setDuration (time); ObjectAnimator. ofFloat (this, "translationX", t, width ). setDuration (time ). start (); animator. addListener (new AnimatorListenerAdapter () {@ Overridepublic void onAnimationEnd (Animator animation) {// TODO Auto-generated method stubsuper. onAnimationEnd (animation); endToFinish () ;}}); animator. start ();} else {ObjectAnimator animator = ObjectAnimator. ofFloat (getChildAt (0), "translationX", t, 0); animator. setDuration (time); animator. addListener (new AnimatorListenerAdapter () {@ Overridepublic void onAnimationEnd (Animator animation) {// TODO Auto-generated method stubsuper. onAnimationEnd (animation); getChildAt (0 ). setAlpha (1); setAlpha (1) ;}}); animator. start () ;}} break;} return true;} public int dip2px (float dpValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);} public void canTouch (boolean flag) {isTouchable = flag ;}}}
 

Http://pan.baidu.com/s/1kT1PvRD

 

Related Article

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.