[关闭]
@XingdingCAO 2017-04-07T20:54:26.000000Z 字数 3706 阅读 3352

Slidr(当前最新版本:2.0.5) 开源库的使用

Android 开源库 slidr

参考:http://www.jcodecraeer.com/a/opensource/2015/0115/2317.html
http://blog.csdn.net/jiangxuqaz/article/details/47264473

作者在酷市场的开源库引用中看到Slidr开源库,这个库是用来是附加在Activity上,使其可以滑动退出。

下面开始动手:

  1. compile 'com.r0adkll:slidableactivity:2.0.5'
  1. dependencies {
  2. compile fileTree(dir: 'libs', include: ['*.jar'])
  3. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  4. exclude group: 'com.android.support', module: 'support-annotations'
  5. })
  6. compile 'com.android.support:appcompat-v7:25.1.1'
  7. compile 'com.r0adkll:slidableactivity:2.0.5'
  8. testCompile 'junit:junit:4.12'
  9. }
  1. <item name="android:windowIsTranslucent">true</item>
  2. <item name="android:windowBackground">@android:color/transparent</item>
  1. <application
  2. android:allowBackup="true"
  3. android:icon="@mipmap/ic_launcher"
  4. android:label="@string/app_name"
  5. android:supportsRtl="true"
  6. android:theme="@style/AppTheme">
  1. <resources>
  2. <!-- Base application theme. -->
  3. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  4. <!-- Customize your theme here. -->
  5. <item name="colorPrimary">@color/colorCoolGreen</item>
  6. <item name="colorPrimaryDark">@color/colorCoolGreen</item>
  7. <item name="colorAccent">@color/colorPrimary</item>x
  8. <item name="android:windowIsTranslucent">true</item>
  9. <item name="android:windowBackground">@android:color/transparent</item>
  10. </style>
  11. </resources>

未设置背景色(背景为应用抽屉):
未设置背景色

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_other"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:background="@color/colorPrimaryDark"//设置为白色
  9. tools:context="me.zxm.learnslidr.OtherActivity">
  10. <include layout="@layout/toolbar"/>
  11. </RelativeLayout>

开始敲代码:

  1. public class OtherActivity extends AppCompatActivity {
  2. private SlidrConfig mSlidrConfig;
  3. private SlidrConfig.Builder mBuilder;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_other);
  8. int primary = getResources().getColor(R.color.colorCoolGreen);
  9. int secondary = getResources().getColor(R.color.colorCoolGreen);
  10. mBuilder = new SlidrConfig.Builder().primaryColor(primary)//滑动时状态栏的渐变结束的颜色
  11. .secondaryColor(secondary)//滑动时状态栏的渐变开始的颜色
  12. .scrimColor(Color.BLACK)//滑动时Activity之间的颜色
  13. .position(SlidrPosition.LEFT)//从左边滑动
  14. .scrimStartAlpha(0.8f)//滑动开始时两个Activity之间的透明度
  15. .scrimEndAlpha(0f)//滑动结束时两个Activity之间的透明度
  16. .velocityThreshold(5f)//超过这个滑动速度,忽略位移限定值就切换Activity
  17. .distanceThreshold(.35f);//滑动位移占屏幕的百分比,超过这个间距就切换Activity
  18. mSlidrConfig = mBuilder.build();
  19. Slidr.attach(this, mSlidrConfig);
  20. }
  1. public class AnotherActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_another);
  6. Slidr.attach(this);
  7. }
  8. }
  1. SlidrInterface slidrInterface = Slidr.attach(this, mSlidrConfig);
  2. slidrInterface.unlock();
  3. slidrInterface.lock();

作者在实际上手时,还发现一个问题,activity切换时的默认动画是从下面弹到上面,然而却是从左滑动退出,操作逻辑有点难受,所以我又查询了如何更改切换动画:在activityonCreate的方法中添加如下一句:

  1. overridePendingTransition(R.anim.abc_fade_in,0);

R.java中的anime类:
F4之后

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注