├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── ParallaxSwipeBack.iml ├── README.md ├── app-debug.apk ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── bureak │ │ └── parallaxswipeback │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── bureak │ │ └── parallaxswipeback │ │ ├── MainActivity.java │ │ ├── NextActivity.java │ │ └── ParallaxSwipeBackActivity.java │ └── res │ ├── anim │ ├── slide_in_right.xml │ └── slide_out_right.xml │ ├── drawable │ ├── pic1.jpg │ ├── pic3.jpg │ ├── pic4.jpg │ ├── pic7.jpg │ └── shadow.png │ ├── layout │ ├── activity_main.xml │ └── activity_next.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── show.gif /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | ParallaxSwipeBack -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ParallaxSwipeBack.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Android ParallaxSwipeBack 2 | ======================= 3 | 4 | 带视觉差的侧滑返回,类似于新版微信和lofter的侧滑返回效果。核心代码小于50行. 5 | ![image](https://github.com/bushijie/ParallaxSwipeBack/blob/master/show.gif) 6 | 7 | Usage 8 | ----- 9 | `public class MainActivity extends ParallaxSwipeBackActivity { 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | findViewById(R.id.next).setOnClickListener(new View.OnClickListener() { 16 | @Override 17 | public void onClick(View v) { 18 | Intent intent = new Intent(MainActivity.this,NextActivity.class); 19 | intent.putExtra("index",0); 20 | startParallaxSwipeBackActivty(MainActivity.this, intent); 21 | } 22 | }); 23 | 24 | }` 25 | 26 | Very little code, are all here 27 | [ParallaxSwipeBackActivity](https://github.com/bushijie/ParallaxSwipeBack/blob/master/app/src/main/java/com/bureak/parallaxswipeback/ParallaxSwipeBackActivity.java) 28 | 29 | 30 | 代码非常少,都在这里 31 | 32 | [ParallaxSwipeBackActivity](https://github.com/bushijie/ParallaxSwipeBack/blob/master/app/src/main/java/com/bureak/parallaxswipeback/ParallaxSwipeBackActivity.java) 33 | Thinks 34 | ----- 35 | [http://blog.csdn.net/hanhailong726188/article/details/46433229](http://blog.csdn.net/hanhailong726188/article/details/46433229) 36 | -------------------------------------------------------------------------------- /app-debug.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bushijie/ParallaxSwipeBack/40e5507e91f1db132634f12de3a0114c2e65c759/app-debug.apk -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.bureak.parallaxswipeback" 9 | minSdkVersion 15 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:22.2.0' 25 | } 26 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\AndroidSdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/bureak/parallaxswipeback/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.bureak.parallaxswipeback; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/bureak/parallaxswipeback/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.bureak.parallaxswipeback; 2 | 3 | import android.content.Intent; 4 | import android.net.Uri; 5 | import android.os.Bundle; 6 | import android.view.Menu; 7 | import android.view.MenuItem; 8 | import android.view.View; 9 | 10 | /** 11 | * @Title: ${file_name} 12 | * @Package ${package_name} 13 | * @Description: 主界面 14 | * Created by bushijie33@gmail.com on 2015/7/5. 15 | */ 16 | public class MainActivity extends ParallaxSwipeBackActivity { 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_main); 22 | findViewById(R.id.next).setOnClickListener(new View.OnClickListener() { 23 | @Override 24 | public void onClick(View v) { 25 | Intent intent = new Intent(MainActivity.this,NextActivity.class); 26 | intent.putExtra("index",0); 27 | startParallaxSwipeBackActivty(MainActivity.this, intent); 28 | } 29 | }); 30 | 31 | } 32 | 33 | @Override 34 | public boolean onCreateOptionsMenu(Menu menu) { 35 | // Inflate the menu; this adds items to the action bar if it is present. 36 | getMenuInflater().inflate(R.menu.menu_main, menu); 37 | return true; 38 | } 39 | 40 | @Override 41 | public boolean onOptionsItemSelected(MenuItem item) { 42 | // Handle action bar item clicks here. The action bar will 43 | // automatically handle clicks on the Home/Up button, so long 44 | // as you specify a parent activity in AndroidManifest.xml. 45 | int id = item.getItemId(); 46 | 47 | //noinspection SimplifiableIfStatement 48 | if (id == R.id.action_settings) { 49 | Uri uri = Uri.parse("https://github.com/bushijie/ParallaxSwipeBack"); 50 | startActivity(new Intent(Intent.ACTION_VIEW,uri)); 51 | return true; 52 | } 53 | 54 | return super.onOptionsItemSelected(item); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/bureak/parallaxswipeback/NextActivity.java: -------------------------------------------------------------------------------- 1 | package com.bureak.parallaxswipeback; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.ImageView; 7 | import android.widget.TextView; 8 | 9 | import java.util.Random; 10 | 11 | /** 12 | * @Title: ${file_name} 13 | * @Package ${package_name} 14 | * @Description: ${todo}(用一句话描述该文件做什么) 15 | * Created by bushijie33@gmail.com on 2015/7/5. 16 | */ 17 | public class NextActivity extends ParallaxSwipeBackActivity { 18 | private int[] images = new int[]{R.drawable.pic1, R.drawable.pic3, R.drawable.pic4, R.drawable.pic7}; 19 | private int index; 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | this.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right); 23 | super.onCreate(savedInstanceState); 24 | 25 | setContentView(R.layout.activity_next); 26 | index = getIntent().getIntExtra("index",0); 27 | ImageView imageView = (ImageView) findViewById(R.id.image); 28 | imageView.setImageResource(images[index]); 29 | TextView textView = (TextView) findViewById(R.id.next); 30 | textView.setText(textView.getText().toString()+index); 31 | 32 | final Intent intent = new Intent(this,NextActivity.class); 33 | intent.putExtra("index",++index); 34 | if(index==images.length){ 35 | textView.setText("FINISH ACTIVITY"); 36 | } 37 | textView.setOnClickListener(new View.OnClickListener() { 38 | @Override 39 | public void onClick(View v) { 40 | if(index==images.length){ 41 | finish(); 42 | NextActivity.this.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right); 43 | }else 44 | startParallaxSwipeBackActivty(NextActivity.this,intent); 45 | } 46 | }); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/bureak/parallaxswipeback/ParallaxSwipeBackActivity.java: -------------------------------------------------------------------------------- 1 | package com.bureak.parallaxswipeback; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.graphics.Rect; 8 | import android.os.Bundle; 9 | import android.support.v4.widget.SlidingPaneLayout; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.util.Log; 12 | import android.view.Gravity; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | import android.widget.FrameLayout; 16 | import android.widget.ImageView; 17 | import android.widget.LinearLayout; 18 | 19 | import java.io.File; 20 | import java.io.FileOutputStream; 21 | import java.lang.reflect.Field; 22 | 23 | /** 24 | * @Title: ${file_name} 25 | * @Description: 带视觉差的滑动返回 26 | * Created by bushijie33@gmail.com on 2015/7/5. 27 | */ 28 | public class ParallaxSwipeBackActivity extends AppCompatActivity implements SlidingPaneLayout.PanelSlideListener { 29 | 30 | private final static String TAG = ParallaxSwipeBackActivity.class.getSimpleName(); 31 | private final static String WINDOWBITMAP = "screenshots.jpg"; 32 | private File mFileTemp; 33 | private SlidingPaneLayout slidingPaneLayout; 34 | private FrameLayout frameLayout; 35 | private ImageView behindImageView; 36 | private ImageView shadowImageView; 37 | private int defaultTranslationX = 100; 38 | private int shadowWidth = 20; 39 | 40 | @Override 41 | protected void onCreate(Bundle savedInstanceState) { 42 | //通过反射来改变SlidingPanelayout的值 43 | try { 44 | slidingPaneLayout = new SlidingPaneLayout(this); 45 | Field f_overHang = SlidingPaneLayout.class.getDeclaredField("mOverhangSize"); 46 | f_overHang.setAccessible(true); 47 | f_overHang.set(slidingPaneLayout, 0); 48 | slidingPaneLayout.setPanelSlideListener(this); 49 | slidingPaneLayout.setSliderFadeColor(getResources().getColor(android.R.color.transparent)); 50 | } catch (Exception e) { 51 | e.printStackTrace(); 52 | } 53 | super.onCreate(savedInstanceState); 54 | mFileTemp = new File(getCacheDir(), WINDOWBITMAP); 55 | defaultTranslationX = dip2px(defaultTranslationX); 56 | shadowWidth = dip2px(shadowWidth); 57 | //behindframeLayout 58 | FrameLayout behindframeLayout = new FrameLayout(this); 59 | behindImageView = new ImageView(this); 60 | behindImageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); 61 | behindframeLayout.addView(behindImageView, 0); 62 | 63 | //containerLayout 64 | LinearLayout containerLayout = new LinearLayout(this); 65 | containerLayout.setOrientation(LinearLayout.HORIZONTAL); 66 | containerLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent)); 67 | containerLayout.setLayoutParams(new ViewGroup.LayoutParams(getWindowManager().getDefaultDisplay().getWidth() + shadowWidth, ViewGroup.LayoutParams.MATCH_PARENT)); 68 | //you view container 69 | frameLayout = new FrameLayout(this); 70 | frameLayout.setBackgroundColor(getResources().getColor(android.R.color.white)); 71 | frameLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); 72 | 73 | //add shadow 74 | shadowImageView = new ImageView(this); 75 | shadowImageView.setBackgroundResource(R.drawable.shadow); 76 | shadowImageView.setLayoutParams(new LinearLayout.LayoutParams(shadowWidth, LinearLayout.LayoutParams.MATCH_PARENT)); 77 | containerLayout.addView(shadowImageView); 78 | containerLayout.addView(frameLayout); 79 | containerLayout.setTranslationX(-shadowWidth); 80 | //添加两个view 81 | slidingPaneLayout.addView(behindframeLayout, 0); 82 | slidingPaneLayout.addView(containerLayout, 1); 83 | } 84 | 85 | @Override 86 | public void setContentView(int id) { 87 | setContentView(getLayoutInflater().inflate(id, null)); 88 | } 89 | 90 | @Override 91 | public void setContentView(View v) { 92 | setContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 93 | try { 94 | behindImageView.setScaleType(ImageView.ScaleType.FIT_XY); 95 | behindImageView.setImageBitmap(getBitmap()); 96 | } catch (Exception e) { 97 | e.printStackTrace(); 98 | } 99 | } 100 | 101 | @Override 102 | public void setContentView(View v, ViewGroup.LayoutParams params) { 103 | super.setContentView(slidingPaneLayout, params); 104 | frameLayout.removeAllViews(); 105 | frameLayout.addView(v, params); 106 | } 107 | 108 | 109 | @Override 110 | public void onPanelClosed(View view) { 111 | 112 | } 113 | 114 | @Override 115 | public void onPanelOpened(View view) { 116 | finish(); 117 | this.overridePendingTransition(0, 0); 118 | } 119 | 120 | @Override 121 | public void onPanelSlide(View view, float v) { 122 | Log.e(TAG, "onPanelSlide :" + v); 123 | //duang duang duang 你可以在这里加入很多特效 124 | behindImageView.setTranslationX(v * defaultTranslationX - defaultTranslationX); 125 | shadowImageView.setAlpha(v<0.8?1:(1.5f-v)); 126 | } 127 | 128 | /** 129 | * 取得视觉差背景图 130 | * 131 | * @return 132 | */ 133 | public Bitmap getBitmap() { 134 | return BitmapFactory.decodeFile(mFileTemp.getAbsolutePath()); 135 | } 136 | 137 | /** 138 | * 启动视觉差返回Activity 139 | * 140 | * @param activity 141 | * @param intent 142 | */ 143 | public void startParallaxSwipeBackActivty(Activity activity, Intent intent) { 144 | startParallaxSwipeBackActivty(activity, intent, false); 145 | } 146 | 147 | /** 148 | * startParallaxSwipeBackActivty 149 | * 150 | * @param activity 151 | * @param intent 152 | * @param isFullScreen 153 | */ 154 | public void startParallaxSwipeBackActivty(Activity activity, Intent intent, boolean isFullScreen) { 155 | screenshots(activity, isFullScreen); 156 | startActivity(intent); 157 | this.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right); 158 | } 159 | 160 | /** 161 | * this screeshots form 162 | * 163 | * @param activity 164 | * @param isFullScreen 165 | */ 166 | public void screenshots(Activity activity, boolean isFullScreen) { 167 | try { 168 | //View是你需要截图的View 169 | View decorView = activity.getWindow().getDecorView(); 170 | decorView.setDrawingCacheEnabled(true); 171 | decorView.buildDrawingCache(); 172 | Bitmap b1 = decorView.getDrawingCache(); 173 | // 获取状态栏高度 / 174 | Rect frame = new Rect(); 175 | activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 176 | int statusBarHeight = frame.top; 177 | Log.e(TAG, "statusBarHeight:" + statusBarHeight); 178 | // 获取屏幕长和高 Get screen width and height 179 | int width = activity.getWindowManager().getDefaultDisplay().getWidth(); 180 | int height = activity.getWindowManager().getDefaultDisplay().getHeight(); 181 | // 去掉标题栏 Remove the statusBar Height 182 | Bitmap bitmap; 183 | if (isFullScreen) { 184 | bitmap = Bitmap.createBitmap(b1, 0, 0, width, height); 185 | } else { 186 | bitmap = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); 187 | } 188 | decorView.destroyDrawingCache(); 189 | FileOutputStream out = new FileOutputStream(mFileTemp); 190 | bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 191 | } catch (Exception e) { 192 | e.printStackTrace(); 193 | } 194 | } 195 | 196 | /** 197 | * 根据手机的分辨率从 dip 的单位 转成为 px(像素) 198 | */ 199 | public int dip2px(float dpValue) { 200 | final float scale = getResources().getDisplayMetrics().density; 201 | return (int) (dpValue * scale + 0.5f); 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_in_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_out_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bushijie/ParallaxSwipeBack/40e5507e91f1db132634f12de3a0114c2e65c759/app/src/main/res/drawable/pic1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bushijie/ParallaxSwipeBack/40e5507e91f1db132634f12de3a0114c2e65c759/app/src/main/res/drawable/pic3.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bushijie/ParallaxSwipeBack/40e5507e91f1db132634f12de3a0114c2e65c759/app/src/main/res/drawable/pic4.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bushijie/ParallaxSwipeBack/40e5507e91f1db132634f12de3a0114c2e65c759/app/src/main/res/drawable/pic7.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/shadow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bushijie/ParallaxSwipeBack/40e5507e91f1db132634f12de3a0114c2e65c759/app/src/main/res/drawable/shadow.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 |