Android slide close activity

Android slide close activity

Source code introduction: Android slides to close the activity, supports three effects: left slide, right slide, and bottom slide to close. SDK 5.0 or above is required, otherwise an error will be reported. The source code is here. Take a look if you need it

Source code effect:

Source code snippet:

  1. package me.imid.swipebacklayout.demo;
  2.   
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.res.Resources;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.os.Vibrator;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.widget.RadioGroup;
  13.   
  14. import me.imid.swipebacklayout.lib.SwipeBackLayout;
  15. import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
  16.   
  17. /**
  18. * Created by Issac on 8/11/13.
  19. */  
  20. public   class DemoActivity extends SwipeBackActivity implements View.OnClickListener {
  21. private   static   final   int VIBRATE_DURATION = 20 ;
  22.   
  23. private   int [] mBgColors;
  24.   
  25. private   static   int mBgIndex = 0 ;
  26.   
  27. private String mKeyTrackingMode;
  28.   
  29. private RadioGroup mTrackingModeGroup;
  30.   
  31. private SwipeBackLayout mSwipeBackLayout;
  32.   
  33. // private Toolbar mToolbar;  
  34.   
  35. @Override  
  36. protected   void onCreate(Bundle savedInstanceState) {
  37. super .onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_demo);
  39. findViews();
  40. // changeActionBarColor();  
  41. mKeyTrackingMode = getString(R.string.key_tracking_mode);
  42. mSwipeBackLayout = getSwipeBackLayout();
  43.   
  44. mTrackingModeGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
  45. @Override  
  46. public   void onCheckedChanged(RadioGroup group, int checkedId) {
  47. int edgeFlag;
  48. switch (checkedId) {
  49. case R.id.mode_left:
  50. edgeFlag = SwipeBackLayout.EDGE_LEFT;
  51. break ;
  52. case R.id.mode_right:
  53. edgeFlag = SwipeBackLayout.EDGE_RIGHT;
  54. break ;
  55. case R.id.mode_bottom:
  56. edgeFlag = SwipeBackLayout.EDGE_BOTTOM;
  57. break ;
  58. default :
  59. edgeFlag = SwipeBackLayout.EDGE_ALL;
  60. }
  61. mSwipeBackLayout.setEdgeTrackingEnabled(edgeFlag);
  62. saveTrackingMode(edgeFlag);
  63. }
  64. });
  65. mSwipeBackLayout.addSwipeListener( new SwipeBackLayout.SwipeListener() {
  66. @Override  
  67. public   void onScrollStateChange( int state, float scrollPercent) {
  68.   
  69. }
  70.   
  71. @Override  
  72. public   void onEdgeTouch( int edgeFlag) {
  73. vibrate(VIBRATE_DURATION);
  74. }
  75.   
  76. @Override  
  77. public   void onScrollOverThreshold() {
  78. vibrate(VIBRATE_DURATION);
  79. }
  80. });
  81. }
  82.   
  83. @Override  
  84. protected   void onResume() {
  85. super .onResume();
  86. restoreTrackingMode();
  87. }
  88.   
  89. private   void saveTrackingMode( int flag) {
  90. PreferenceUtils.setPrefInt(getApplicationContext(), mKeyTrackingMode, flag);
  91. }
  92.   
  93. private   void restoreTrackingMode() {
  94. int flag = PreferenceUtils.getPrefInt(getApplicationContext(), mKeyTrackingMode,
  95. SwipeBackLayout.EDGE_LEFT);
  96. mSwipeBackLayout.setEdgeTrackingEnabled(flag);
  97. switch (flag) {
  98. case SwipeBackLayout.EDGE_LEFT:
  99. mTrackingModeGroup.check(R.id.mode_left);
  100. break ;
  101. case SwipeBackLayout.EDGE_RIGHT:
  102. mTrackingModeGroup.check(R.id.mode_right);
  103. break ;
  104. case SwipeBackLayout.EDGE_BOTTOM:
  105. mTrackingModeGroup.check(R.id.mode_bottom);
  106. break ;
  107. case SwipeBackLayout.EDGE_ALL:
  108. mTrackingModeGroup.check(R.id.mode_all);
  109. break ;
  110. }
  111. }
  112.   
  113. private   void changeActionBarColor() {
  114. // getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getColors()[mBgIndex]));  
  115. mBgIndex++;
  116. if (mBgIndex >= getColors().length) {
  117. mBgIndex = 0 ;
  118. }
  119. }
  120.   
  121. private   void findViews() {
  122. // setSupportActionBar((Toolbar) findViewById(R.id.toolbar));  
  123. findViewById(R.id.btn_start).setOnClickListener( this );
  124. findViewById(R.id.btn_finish).setOnClickListener( this );
  125. mTrackingModeGroup = (RadioGroup) findViewById(R.id.tracking_mode);
  126. }
  127.   
  128. private   int [] getColors() {
  129. if (mBgColors == null ) {
  130. Resources resource = getResources();
  131. mBgColors = new   int [] {
  132. resource.getColor(R.color.androidColorA),
  133. resource.getColor(R.color.androidColorB),
  134. resource.getColor(R.color.androidColorC),
  135. resource.getColor(R.color.androidColorD),
  136. resource.getColor(R.color.androidColorE),
  137. };
  138. }
  139. return mBgColors;
  140. }
  141.   
  142. private   void vibrate( long duration) {
  143. Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  144. long [] pattern = {
  145. 0 , duration
  146. };
  147. vibrator.vibrate(pattern, - 1 );
  148. }
  149.   
  150. @Override  
  151. public   void onClick(View v) {
  152. switch (v.getId()) {
  153. case R.id.btn_start:
  154. startActivity( new Intent(DemoActivity. this , DemoActivity. class ));
  155. break ;
  156. case R.id.btn_finish:
  157. scrollToFinishActivity();
  158. break ;
  159. }
  160. }
  161.   
  162. @Override  
  163. public   boolean onCreateOptionsMenu(Menu menu) {
  164. getMenuInflater().inflate(R.menu.main, menu);
  165. return   true ;
  166. }
  167.   
  168. @Override  
  169. public   boolean onOptionsItemSelected(MenuItem item) {
  170. switch (item.getItemId()) {
  171. case R.id.action_github:
  172. Intent intent = new Intent(Intent.ACTION_VIEW);
  173. intent.setData(Uri.parse( "https://github.com/Issacw0ng/SwipeBackLayout" ));
  174. startActivity(intent);
  175. return   true ;
  176. default :
  177. return   super .onOptionsItemSelected(item);
  178. }
  179. }
  180.   
  181. }

Download address: http://download..com/data/2108012

<<:  Useful information sharing: Correct use of const, static, extern

>>:  The fastest way to get started with ReactiveCocoa: Advanced Edition

Recommend

From Li Jiaqi's IP: The "hidden rules" of cross-platform account operations

In the Internet age, information flows faster and...

Let’s talk about the user growth system of Honor of Kings

As a popular national game, the success of Honor ...

Father’s Day copy is here, it’s from the heart, not the body! !

Father's Day is coming. They are good at many...

Deconstructing e-commerce and O2O: Operational CRM of marketing channels

CRM is divided into sales CRM and user operation ...

How to conduct user behavior analysis? What is the value?

Many people may not know that the meaning of the ...

Gou Wenqiang's 31 Posture Correction Training Camp

Gou Wenqiang's 31 Posture Correction Training...

Why does marketing to women so often fail?

Since entering the 21st century, the rise of fema...