android滑动关闭activity

移动开发
android 滑动关闭activity ,支持三种效果:左滑,右滑,及底部滑动关闭 sdk 需5.0以上,不然会报错。源码在这了。需要的话自己看看

源码简介:android 滑动关闭activity ,支持三种效果:左滑,右滑,及底部滑动关闭 sdk 需5.0以上,不然会报错。源码在这了。需要的话自己看看

源码效果:

源码片段:

  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.   

下载地址:http://down.51cto.com/data/2108012

责任编辑:倪明 来源: devstore
相关推荐

2012-02-17 17:07:30

Android安全Activity劫持

2013-01-08 13:33:07

Android开发Activity入门指南

2011-04-12 17:16:52

Activity跳转开发实例Android学习笔记

2014-08-08 10:36:12

ActivityAndroid

2011-06-02 11:13:10

Android Activity

2010-02-06 10:14:36

Android Act

2013-11-13 16:27:02

Windows 8.1关机

2013-02-01 13:47:47

Android开发Activity

2010-01-25 17:21:34

Android Act

2015-11-09 14:46:49

Android安全

2014-05-27 15:36:01

AndroidActivity启动模式

2014-08-21 11:07:40

AndroidActivity销毁

2017-01-04 18:29:20

AndroidNestedScrol嵌套滑动机制

2011-05-19 17:49:08

ActivityAndroid开发

2011-06-16 09:31:21

ActivityAndroid

2010-01-28 14:12:20

Android Act

2011-05-30 14:00:35

Android Activity Intent

2011-05-26 15:22:57

Android Activity

2010-01-28 14:57:36

Android滑动手势

2017-04-27 21:00:33

Android滑动分析
点赞
收藏

51CTO技术栈公众号