Skip to content

Commit

Permalink
Added module and BookFlipPageTransformer class
Browse files Browse the repository at this point in the history
  • Loading branch information
wajahatkarim3 committed Nov 26, 2018
1 parent c5a248d commit bbab07e
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions easyflipviewpager/.gitignore
@@ -0,0 +1 @@
/build
34 changes: 34 additions & 0 deletions easyflipviewpager/build.gradle
@@ -0,0 +1,34 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28



defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
21 changes: 21 additions & 0 deletions easyflipviewpager/proguard-rules.pro
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,26 @@
package com.wajahatkarim3.easyflipviewpager;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.wajahatkarim3.easyflipviewpager.test", appContext.getPackageName());
}
}
2 changes: 2 additions & 0 deletions easyflipviewpager/src/main/AndroidManifest.xml
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wajahatkarim3.easyflipviewpager"/>
@@ -0,0 +1,84 @@
package com.wajahatkarim3.easyflipviewpager;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;

/**
* A book based page flip animation PageTransformer implementation for ViewPager
*
* Set the object of this transformer to any ViewPager object.
* For example, myViewPager.setPageTransformer(true, new BookFlipPageTransformer());
*
* @see <a href="http://github.com/wajahatkarim3/EasyFlipViewPager">EasyFlipViewPager</a>
*
* @author Wajahat Karim (http://wajahatkarim.com)
*/
public class BookFlipPageTransformer implements ViewPager.PageTransformer {

private final int LEFT = -1;
private final int RIGHT = 1;
private final int CENTER = 0;

@Override
public void transformPage(@NonNull View page, float position) {
float percentage = 1 - Math.abs(position);
// Don't move pages once they are on left or right
if (position > CENTER && position <= RIGHT)
{
// This is behind page
page.setVisibility(View.VISIBLE);
page.setTranslationX(-position * (page.getWidth()));
page.setTranslationY(0);
page.setRotation(0);
}
// Otherwise flip the current page
else
{
flipPage(page, position, percentage);
}
}

private void flipPage(View page, float position, float percentage)
{
// Flip this page
page.setCameraDistance(-20000);
setVisibility(page, position);
setTranslation(page);
setPivot(page, 0f, page.getHeight() * 0.5f);
setRotation(page, position, percentage);
}

private void setPivot(View page, float pivotX, float pivotY)
{
page.setPivotX(pivotX);
page.setPivotY(pivotY);
}

private void setVisibility(View page, float position) {
if (position < 0.5 && position > -0.5) {
page.setVisibility(View.VISIBLE);
} else {
page.setVisibility(View.INVISIBLE);
}
}

private void setTranslation(View page) {
ViewPager viewPager = (ViewPager) page.getParent();
int scroll = viewPager.getScrollX() - page.getLeft();
page.setTranslationX(scroll);
}

private void setSize(View page, float position, float percentage) {
page.setScaleX((position != 0 && position != 1) ? percentage : 1);
page.setScaleY((position != 0 && position != 1) ? percentage : 1);
}

private void setRotation(View page, float position, float percentage) {
if (position > 0) {
page.setRotationY(-180 * (percentage + 1));
} else {
page.setRotationY(180 * (percentage + 1));
}
}
}
3 changes: 3 additions & 0 deletions easyflipviewpager/src/main/res/values/strings.xml
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">EasyFlipViewPager</string>
</resources>
@@ -0,0 +1,17 @@
package com.wajahatkarim3.easyflipviewpager;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}
2 changes: 1 addition & 1 deletion settings.gradle
@@ -1 +1 @@
include ':app'
include ':app', ':easyflipviewpager'

0 comments on commit bbab07e

Please sign in to comment.