Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gentle parallax animation on ImageView. #8

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TextView | PEWTextView
Gradle dependencies:

```groovy
compile 'com.fmsirvent:parallaxeverywhere:1.0.4'
compile 'com.github.alexanderbezverhni:parallaxeverywhere:1.0.0'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I can't accept the PR with all this lines.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peer-reviewed

```

Code in layout:
Expand Down
32 changes: 12 additions & 20 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,23 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

POM_NAME=ParallaxEverywhere
POM_NAME=GentleParallaxEverywhere
POM_ARTIFACT_ID=parallaxeverywhere
POM_PACKAGING=aar
VERSION_NAME=1.0.4
VERSION_CODE=5
GROUP=com.fmsirvent

POM_DESCRIPTION=Widget with parallax effect on scroll.
POM_URL=https://github.com/narfss/ParallaxEverywhere
POM_SCM_URL=https://github.com/narfss/ParallaxEverywhere
POM_SCM_CONNECTION=scm:[email protected]:narfss/parallaxeverywhere.git
POM_SCM_DEV_CONNECTION=scm:[email protected]:narfss/parallaxeverywhere.git
VERSION_NAME=1.0.0
VERSION_CODE=1
GROUP=com.github.alexanderbezverhni

POM_DESCRIPTION=Widget with gentle parallax effect on scroll.
POM_URL=https://github.com/alexanderbezverhni/ParallaxEverywhere
POM_SCM_URL=https://github.com/alexanderbezverhni/ParallaxEverywhere
POM_SCM_CONNECTION=scm:[email protected]:alexanderbezverhni/parallaxeverywhere.git
POM_SCM_DEV_CONNECTION=scm:[email protected]:alexanderbezverhni/parallaxeverywhere.git
POM_LICENCE_NAME=MIT license. See the LICENSE file for more info
POM_LICENCE_URL=http://opensource.org/licenses/MIT
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=narfss
POM_DEVELOPER_NAME=Francisco M Sirvent

signing.keyId=XXXXXX
signing.password=XXXXXX
signing.secretKeyRingFile=/home/narf/.gnupg/secring.gpg

NEXUS_USERNAME=XXXXXX
NEXUS_PASSWORD=XXXXXX

POM_DEVELOPER_ID=alexanderbezverhni
POM_DEVELOPER_NAME=Alexander Bezverhni

SNAPSHOT_REPOSITORY_URL=https://oss.sonatype.org/content/repositories/snapshots
RELEASE_REPOSITORY_URL=https://oss.sonatype.org/service/local/staging/deploy/maven2
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@

/**
* Created by fmsirvent on 03/11/14.
*
* Changed by @alexanderbezverhni on 10/06/15 - added support for parallax distance limit.
*/
public class PEWImageView extends ImageView {

public static final float FACTOR_DEFAULT = -1.0f;

public boolean reverseX = false;
public boolean reverseY = false;
public boolean updateOnDraw = false;
Expand All @@ -34,6 +38,7 @@ public class PEWImageView extends ImageView {
private float scrollSpaceY = 0;
private float heightImageView;
private float widthImageView;
private float factor = FACTOR_DEFAULT;

private Interpolator interpolator = new LinearInterpolator();

Expand Down Expand Up @@ -151,6 +156,10 @@ private void checkAttributes(AttributeSet attrs) {
blockParallaxX = arr.getBoolean(R.styleable.PEWAttrs_block_parallax_x, false);
blockParallaxY = arr.getBoolean(R.styleable.PEWAttrs_block_parallax_y, false);

float factor = arr.getFloat(R.styleable.PEWAttrs_factor, FACTOR_DEFAULT);
if (factor != FACTOR_DEFAULT)
setFactor(factor);

reverseX = false;
reverseY = false;
switch (reverse) {
Expand Down Expand Up @@ -215,23 +224,36 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
break;
}

scrollSpaceY = (dnewHeight > vheight) ? (dnewHeight - vheight) : 0;
scrollSpaceX = (dnewWidth > vwidth) ? (dnewWidth - vwidth) : 0;
scrollSpaceY = getScrollSpace(vheight, dnewHeight, factor);
scrollSpaceX = getScrollSpace(vwidth, dnewWidth, factor);
}
applyParallax();
}

private float getScrollSpace(int viewEdgeSize, float imageEdgeSize, float factor){
if (viewEdgeSize > imageEdgeSize){
return 0;
}

float maxScrollSpace = imageEdgeSize - viewEdgeSize;
if (factor == FACTOR_DEFAULT) {
return maxScrollSpace;
}

float factored = viewEdgeSize * (factor - 1.0f);
return Math.min(factored, maxScrollSpace);
}

private void parallaxAnimation() {
initSizeScreen();

applyParallax();

}

private void initSizeScreen() {
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
display.getSize(size);
screenHeight = size.y;
Expand Down Expand Up @@ -275,15 +297,15 @@ private void applyParallax() {
}

private void setMyScrollX(int value) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
setScrollX(value);
} else {
scrollTo(value, getScrollY());
}
}

private void setMyScrollY(int value) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
setScrollY(value);
} else {
scrollTo(getScrollX(),value);
Expand Down Expand Up @@ -325,4 +347,35 @@ public boolean isBlockParallaxY() {
public void setBlockParallaxY(boolean blockParallaxY) {
this.blockParallaxY = blockParallaxY;
}

@Override
public void scrollTo(int x, int y) {
super.scrollTo(isBlockParallaxX() ? 0 : x, isBlockParallaxY() ? 0 : y);
}

/**
* Limit parallax distance to some relative value, to make parallax animation more gentle.<br>
* Example: for an ImageView with 400x300 dimensions, we set an image with 400x1000 dimensions.
* Enabling Y axis animation, we will experience very "aggressive" animation, while image will be moved
* by 600 pixels vertically. Setting factor to 1.5f will result in 150px diff: 300*1.5=150
*
* @param factor
* must be larger or equal than 1.0f
*/
public void setFactor(float factor) {
if (factor < 1.0f)
throw new IllegalArgumentException("Factor value must be larger or equal than 1.0f !");

this.factor = factor;
}

/**
* Get parallax animation bounds factor.
*
* @return
* either {@value #FACTOR_DEFAULT} (default value, when none factor is applied) or value that is larger or equal to 1.0f.
*/
public float getFactor(){
return factor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,9 @@ public boolean isBlockParallaxY() {
public void setBlockParallaxY(boolean blockParallaxY) {
this.blockParallaxY = blockParallaxY;
}

@Override
public void scrollTo(int x, int y) {
super.scrollTo(isBlockParallaxX() ? 0 : x, isBlockParallaxY() ? 0 : y);
}
}
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
<enum name="overshoot" value="7" />
</attr>

<attr name="factor" format="float"/>

</declare-styleable>
</resources>