仿iOS滑动返回效果

本文转载自:http://blog.csdn.net/jiangxuqaz/article/details/47264473

百度贴吧有滑动切换Activity的功能,感觉很方便: 
这里写图片描述 
这种功能要自己写还是挺复杂的,幸运的是,已经有比较成熟的开源项目,项目地址:https://github.com/r0adkll/Slidr。下面将使用这个开源项目,快速实现相似的功能。

一、导入Slidr到项目

在dependencies中加入下面一句,就可以引入开源库到当前项目:

compile 'com.r0adkll:slidableactivity:2.0.3'
 
 
  • 1
  • 1

二、Slidr使用

需要准备两个Activity,唯一需要注意的是Activity的Theme需要重写下面的代码:

<item name="android:windowIsTranslucent">true</item>  
<item name="android:windowBackground">@android:color/transparent</item>
 
 
  • 1
  • 2
  • 1
  • 2

这样配置以后,需要在这个两个Activity的布局文件中的最顶层的Layout中,为Activity设置背景(否则Activity会是透明的):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/white"
               tools:context="com.jx.slideactivity.OtherActivity">
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然后,在需要滑动的Activity里面配置Slidr: 
先定义两个全局变量:

    SlidrConfig mSlidrConfig;
    SlidrConfig.Builder mBuilder;
 
 
  • 1
  • 2
  • 1
  • 2

然后配置

 int primary = getResources().getColor(R.color.primaryDark);
        int secondary = getResources().getColor(R.color.accent);
        mBuilder = new SlidrConfig.Builder().primaryColor(primary)
                .secondaryColor(secondary)
                .scrimColor(Color.BLACK)
                .position(SlidrPosition.LEFT)
                .scrimStartAlpha(0.8f)
                .scrimEndAlpha(0f)
                .velocityThreshold(5f)
                .distanceThreshold(.35f);
        mSlidrConfig = mBuilder.build();
        Slidr.attach(this, mSlidrConfig);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

下面解释一下各个配置项代表的意思: 
.primaryColor(primary)//滑动时状态栏的渐变结束的颜色 
.secondaryColor(secondary)//滑动时状态栏的渐变开始的颜色 
.scrimColor(Color.BLACK)//滑动时Activity之间的颜色 
. position(SlidrPosition.LEFT)//从左边滑动 
.scrimStartAlpha(0.8f)//滑动开始时两个Activity之间的透明度 
.scrimEndAlpha(0f)//滑动结束时两个Activity之间的透明度 
.velocityThreshold(5f)//超过这个滑动速度,忽略位移限定值就切换Activity 
.distanceThreshold(.35f);//滑动位移占屏幕的百分比,超过这个间距就切换Activity 
至此,所有的配置已经完成。 
Demo地址: 
AndroidStudio版:http://download.csdn.net/detail/jiangxuqaz/8957411 
Eclipse版:http://download.csdn.net/detail/jiangxuqaz/9056051

三、运行效果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值