├── .classpath ├── .gitignore ├── .project ├── README.md ├── example └── Sensortest │ ├── .classpath │ ├── .project │ ├── .settings │ └── org.eclipse.jdt.core.prefs │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── libs │ └── android-support-v4.jar │ ├── lint.xml │ ├── proguard-project.txt │ ├── project.properties │ ├── res │ ├── anim │ │ └── anim_info_popup.xml │ ├── drawable-hdpi │ │ ├── backgr.png │ │ └── ic_launcher.png │ ├── drawable-ldpi │ │ ├── backgr.png │ │ ├── button_full.9.png │ │ ├── button_full_pressed.png │ │ └── logo.png │ ├── drawable-mdpi │ │ ├── backgr.png │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── drawable │ │ ├── background.png │ │ └── button_full_style.xml │ ├── layout │ │ └── activity_main.xml │ ├── menu │ │ └── main.xml │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ ├── values-w820dp │ │ └── dimens.xml │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── src │ └── com │ └── example │ └── sensortest │ ├── Connector.java │ └── MainActivity.java ├── library ├── .classpath ├── .project ├── AndroidManifest.xml ├── LICENSE ├── proguard-project.txt ├── project.properties ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ └── values │ │ ├── strings.xml │ │ └── styles.xml └── src │ └── com │ └── android │ └── irimiaionut │ └── parallaxImageView │ └── ParallaxImageView.java └── screen.png /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | lib_ParallaxImageView 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Parallax ImageView Android library 2 | 3 | ![alt tag](https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/master/screen.png) 4 | 5 | **1. Project** 6 | 7 | You have here a library that lets you use a imageview to simulate a 3D effect like on Amazon FirePhone and Iphone. To do so, the imageview is set with a margin and based on the phone's movement and interpreted with the Sensor.TYPE_ROTATION_VECTOR moves the margin so that it creates a 3D in depht effect. 8 | 9 | 10 | **2. Recommended usage** 11 | 12 | This can be used on any layout but keep in mind to check how it affects the overall behavior of the app(better no animations than interrupted ones). 13 | My recommendation would be to use it on light implementations like loginscreens / settings. 14 | 15 | 16 | **3. Recommended implementation** 17 | ```java 18 | 25 | 26 | 33 | 34 | ``` 35 | 36 | ```java 37 | backgr = (ParallaxImageView) findViewById(R.id.backgr); 38 | backgr.setMargins(300, 200); 39 | backgr.setMultipliers(1.5f, 1.7f); 40 | ``` 41 | 42 | 43 | 44 | 45 | **4. Notes** 46 | 47 | * Solved some Galaxy devices exception for event.values on ```getRotationMatrixFromVector();``` 48 | * Min SDK 11 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /example/Sensortest/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /example/Sensortest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sensortest 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /example/Sensortest/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /example/Sensortest/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 12 | 13 | 19 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /example/Sensortest/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/ic_launcher-web.png -------------------------------------------------------------------------------- /example/Sensortest/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/libs/android-support-v4.jar -------------------------------------------------------------------------------- /example/Sensortest/lint.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /example/Sensortest/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /example/Sensortest/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | android.library.reference.1=../../library 16 | -------------------------------------------------------------------------------- /example/Sensortest/res/anim/anim_info_popup.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 | 19 | 20 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-hdpi/backgr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-hdpi/backgr.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-ldpi/backgr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-ldpi/backgr.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-ldpi/button_full.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-ldpi/button_full.9.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-ldpi/button_full_pressed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-ldpi/button_full_pressed.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-ldpi/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-ldpi/logo.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-mdpi/backgr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-mdpi/backgr.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/example/Sensortest/res/drawable/background.png -------------------------------------------------------------------------------- /example/Sensortest/res/drawable/button_full_style.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /example/Sensortest/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 15 | 16 | 26 | 27 | 34 | 35 | 36 | 44 | 45 | 57 | 58 | 59 | 65 | 66 | 76 | 77 | 88 | 89 | 90 | 91 | 92 | 93 | 98 | 99 | 112 | 113 | 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /example/Sensortest/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /example/Sensortest/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /example/Sensortest/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /example/Sensortest/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 64dp 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/Sensortest/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/Sensortest/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Sensortest 5 | Hello world! 6 | Settings 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/Sensortest/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /example/Sensortest/src/com/example/sensortest/Connector.java: -------------------------------------------------------------------------------- 1 | package com.example.sensortest; 2 | 3 | public class Connector { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example/Sensortest/src/com/example/sensortest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sensortest; 2 | 3 | import com.android.irimiaionut.parallaxImageView.ParallaxImageView; 4 | 5 | import android.app.Activity; 6 | import android.os.Bundle; 7 | import android.os.Handler; 8 | import android.view.Menu; 9 | import android.view.MenuItem; 10 | import android.view.View; 11 | import android.view.animation.Animation; 12 | import android.view.animation.AnimationUtils; 13 | import android.widget.LinearLayout; 14 | import android.widget.TextView; 15 | 16 | public class MainActivity extends Activity { 17 | 18 | 19 | TextView welcome; 20 | 21 | 22 | ParallaxImageView backgr; 23 | LinearLayout logo_holder; 24 | 25 | Animation anim_logo; 26 | Animation anim_welcome; 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_main); 32 | backgr = (ParallaxImageView) findViewById(R.id.backgr); 33 | backgr.setMargins(800, 500); 34 | backgr.setMultipliers(2.5f, 2.7f); 35 | 36 | welcome = (TextView) findViewById(R.id.welcome); 37 | logo_holder = (LinearLayout) findViewById(R.id.logo_holder); 38 | welcome.setVisibility(View.INVISIBLE); 39 | logo_holder.setVisibility(View.INVISIBLE); 40 | 41 | anim_logo = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_info_popup); 42 | anim_welcome = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_info_popup); 43 | } 44 | 45 | protected void onStart(){ 46 | super.onStart(); 47 | 48 | } 49 | 50 | 51 | @Override 52 | public boolean onCreateOptionsMenu(Menu menu) { 53 | // Inflate the menu; this adds items to the action bar if it is present. 54 | getMenuInflater().inflate(R.menu.main, menu); 55 | return true; 56 | } 57 | 58 | @Override 59 | public boolean onOptionsItemSelected(MenuItem item) { 60 | int id = item.getItemId(); 61 | if (id == R.id.action_settings){ 62 | return true; 63 | } 64 | return super.onOptionsItemSelected(item); 65 | } 66 | 67 | 68 | 69 | protected void onPause() { 70 | super.onPause(); 71 | backgr.onPause(); 72 | welcome.setVisibility(View.INVISIBLE); 73 | logo_holder.setVisibility(View.INVISIBLE); 74 | } 75 | 76 | protected void onResume() { 77 | super.onResume(); 78 | backgr.onResume(); 79 | welcome.startAnimation(anim_welcome); 80 | 81 | Handler handler = new Handler(); 82 | Runnable startMain = new Runnable() { 83 | 84 | @Override 85 | public void run() { 86 | if (!isFinishing()){ 87 | logo_holder.startAnimation(anim_logo); 88 | } 89 | } 90 | }; 91 | 92 | handler.postDelayed(startMain, 200); 93 | 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /library/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /library/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | lib_ParallaxImageView 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /library/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /library/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014 irimiaionut 2 | -------------------------------------------------------------------------------- /library/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /library/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-20 15 | android.library=true 16 | -------------------------------------------------------------------------------- /library/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/library/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/library/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/library/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /library/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /library/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | lib_ParallaxImageVeiw 4 | 5 | 6 | -------------------------------------------------------------------------------- /library/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /library/src/com/android/irimiaionut/parallaxImageView/ParallaxImageView.java: -------------------------------------------------------------------------------- 1 | package com.android.irimiaionut.parallaxImageView; 2 | 3 | import android.content.Context; 4 | import android.hardware.Sensor; 5 | import android.hardware.SensorEvent; 6 | import android.hardware.SensorEventListener; 7 | import android.hardware.SensorManager; 8 | import android.util.AttributeSet; 9 | import android.widget.ImageView; 10 | import android.widget.RelativeLayout; 11 | 12 | public class ParallaxImageView extends ImageView implements SensorEventListener { 13 | float[] rotMat = new float[16]; 14 | float[] vals = new float[3]; 15 | //sensor parallax effect 16 | private SensorManager senSensorManager; 17 | private Sensor senAccelerometer; 18 | private int sideVerticalMargin, sideHorizontalMargin; 19 | private float verticalMultiplier=1, horizontalMultiplier=1; 20 | 21 | public ParallaxImageView(Context context, AttributeSet attrs) { 22 | super(context, attrs); 23 | 24 | senSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); 25 | senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); 26 | senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_FASTEST); 27 | 28 | } 29 | 30 | public ParallaxImageView(Context context) { 31 | super(context); 32 | } 33 | 34 | public void setMargins(int VM, int HM){ 35 | this.sideVerticalMargin = -VM; 36 | this.sideHorizontalMargin = -HM; 37 | RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)this.getLayoutParams(); 38 | params.setMargins(-HM, -VM, -HM, -VM); 39 | this.setLayoutParams(params); 40 | } 41 | 42 | public void setMultipliers(float Vertical, float Horizontal){ 43 | this.verticalMultiplier = Vertical; 44 | this.horizontalMultiplier = Horizontal; 45 | } 46 | 47 | @Override 48 | public void onSensorChanged(SensorEvent event) { 49 | Sensor mySensor = event.sensor; 50 | 51 | if (mySensor.getType() == Sensor.TYPE_ROTATION_VECTOR) { 52 | 53 | // Convert the rotation-vector to a 4x4 matrix. 54 | try { 55 | SensorManager.getRotationMatrixFromVector(rotMat, event.values); 56 | } catch (IllegalArgumentException e) { 57 | if (event.values.length > 3) { 58 | // Note 3 bug 59 | float[] newVector = new float[] { 60 | event.values[0], 61 | event.values[1], 62 | event.values[2] 63 | }; 64 | SensorManager.getRotationMatrixFromVector(rotMat, newVector); 65 | } 66 | } 67 | SensorManager.remapCoordinateSystem(rotMat, 68 | SensorManager.AXIS_Y, SensorManager.AXIS_X, 69 | rotMat); 70 | 71 | SensorManager.getOrientation(rotMat, vals); 72 | 73 | vals[0] = (float) Math.toDegrees(vals[0]); 74 | vals[1] = (float) Math.toDegrees(vals[1]); 75 | vals[2] = (float) Math.toDegrees(vals[2]); 76 | 77 | int leftfloat = (int) (this.sideHorizontalMargin-(vals[1]*this.horizontalMultiplier)); 78 | 79 | int topfloat; 80 | 81 | if(vals[2]>0){ 82 | topfloat=(int) (this.sideVerticalMargin+(vals[2]*this.verticalMultiplier)); 83 | }else{ 84 | topfloat=(int) (this.sideVerticalMargin-(vals[2]*this.verticalMultiplier)); 85 | } 86 | 87 | this.setX(leftfloat); 88 | this.setY(topfloat); 89 | } 90 | 91 | } 92 | 93 | @Override 94 | public void onAccuracyChanged(Sensor sensor, int accuracy) { 95 | // TODO Auto-generated method stub 96 | 97 | } 98 | 99 | public void onPause() { 100 | senSensorManager.unregisterListener(this); 101 | } 102 | public void onResume() { 103 | senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_FASTEST); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irimiaionut/ParallaxImageView/8e4e194b286e7b9d7c6d6e0ac3a4bf74d1b2944f/screen.png --------------------------------------------------------------------------------