From 3b5b7243d30c7926a6662283cf6062edac070bba Mon Sep 17 00:00:00 2001 From: Est Date: Sat, 25 Feb 2017 01:00:44 +0800 Subject: [PATCH] add new anim mode --- README.md | 6 ++-- .../activity/CardFlowLayoutActivity.java | 19 +++++++--- .../ecardflowdemo/activity/MainActivity.java | 23 +++++++++--- app/src/main/res/layout/activity_main.xml | 16 +++++++++ .../codeest/ecardflow/ECardFlowLayout.java | 6 ++-- .../codeest/ecardflow/mode/BlurAnimMode.java | 4 +-- .../ecardflow/mode/CrossMoveAnimMode.java | 36 +++++++++++++++++++ .../codeest/ecardflow/mode/ScaleAnimMode.java | 28 +++++++++++++++ 8 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 ecardflow/src/main/java/moe/codeest/ecardflow/mode/CrossMoveAnimMode.java create mode 100644 ecardflow/src/main/java/moe/codeest/ecardflow/mode/ScaleAnimMode.java diff --git a/README.md b/README.md index 55f0700..050ae60 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Step 1. Add the JitPack repository to your build file Step 2. Add the dependency dependencies { - compile 'com.github.codeestX:ECardFlow:v1.0.3' + compile 'com.github.codeestX:ECardFlow:v1.0.5' } Step 3. Just use it as a ViewPager @@ -119,7 +119,7 @@ Step 1. Add the JitPack repository to your build file Step 2. Add the dependency dependencies { - compile 'com.github.codeestX:ECardFlow:v1.0.4' + compile 'com.github.codeestX:ECardFlow:v1.0.5' } Step 3. Just put a ViewPager in ECardFlowLayout @@ -176,6 +176,8 @@ Java Code: | DefaultAnimMode | No Effect | | BlurAnimMode | Blur Effect | | MoveAnimMode | Parallax Effect | +| ScaleAnimMode | Scale Effect | +| CrossMoveAnimMode | Cross Parallax Effect | You can custom AnimMode with class which implements `AnimMode` | ImageProvider | Des | diff --git a/app/src/main/java/moe/codeest/ecardflowdemo/activity/CardFlowLayoutActivity.java b/app/src/main/java/moe/codeest/ecardflowdemo/activity/CardFlowLayoutActivity.java index 16fc992..b8853ba 100644 --- a/app/src/main/java/moe/codeest/ecardflowdemo/activity/CardFlowLayoutActivity.java +++ b/app/src/main/java/moe/codeest/ecardflowdemo/activity/CardFlowLayoutActivity.java @@ -9,8 +9,10 @@ import moe.codeest.ecardflow.ECardFlowLayout; import moe.codeest.ecardflow.mode.BlurAnimMode; +import moe.codeest.ecardflow.mode.CrossMoveAnimMode; import moe.codeest.ecardflow.mode.DefaultAnimMode; import moe.codeest.ecardflow.mode.MoveAnimMode; +import moe.codeest.ecardflow.mode.ScaleAnimMode; import moe.codeest.ecardflow.provider.DrawableImageProvider; import moe.codeest.ecardflow.util.DimenUtils; import moe.codeest.ecardflowdemo.R; @@ -24,9 +26,12 @@ public class CardFlowLayoutActivity extends AppCompatActivity { - public static int MODE_DEFAULT = 0; - public static int MODE_BLUR = 1; - public static int MODE_MOVE = 2; + public final static int MODE_DEFAULT = 0; + public final static int MODE_BLUR = 1; + public final static int MODE_MOVE = 2; + public final static int MODE_SCALE = 3; + public final static int MODE_CROSS_MOVE = 4; + public final static String ANIM_MODE = "mode"; private ECardFlowLayout mVPLayout; private ViewPager mViewPager; @@ -40,7 +45,7 @@ public class CardFlowLayoutActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cardflow_layout); - mMode = getIntent().getIntExtra("mode", MODE_DEFAULT); + mMode = getIntent().getIntExtra(ANIM_MODE, MODE_DEFAULT); init(); } @@ -58,6 +63,12 @@ private void init() { case 2: mVPLayout.setAnimMode(new MoveAnimMode()); break; + case 3: + mVPLayout.setAnimMode(new ScaleAnimMode()); + break; + case 4: + mVPLayout.setAnimMode(new CrossMoveAnimMode()); + break; } mVPLayout.setImageProvider(new DrawableImageProvider(this, res, DimenUtils.getScreenWidth(getApplicationContext()), DimenUtils.getScreenHeight(getApplicationContext()))); for (int i = 0; i < 4; i++) { diff --git a/app/src/main/java/moe/codeest/ecardflowdemo/activity/MainActivity.java b/app/src/main/java/moe/codeest/ecardflowdemo/activity/MainActivity.java index 4efcdfc..1938d75 100644 --- a/app/src/main/java/moe/codeest/ecardflowdemo/activity/MainActivity.java +++ b/app/src/main/java/moe/codeest/ecardflowdemo/activity/MainActivity.java @@ -8,10 +8,11 @@ import moe.codeest.ecardflowdemo.R; -public class MainActivity extends AppCompatActivity implements View.OnClickListener { +import static moe.codeest.ecardflowdemo.activity.CardFlowLayoutActivity.ANIM_MODE; - AppCompatButton btnCard, btnLayout, btnLayoutBlur, btnLayoutMove; +public class MainActivity extends AppCompatActivity implements View.OnClickListener { + AppCompatButton btnCard, btnLayout, btnLayoutBlur, btnLayoutMove, btnLayoutScale, btnLayoutCrossMove; @Override public void onCreate(Bundle savedInstanceState) { @@ -22,11 +23,15 @@ public void onCreate(Bundle savedInstanceState) { btnLayout = (AppCompatButton) findViewById(R.id.btn_layout); btnLayoutBlur = (AppCompatButton) findViewById(R.id.btn_layout_blur); btnLayoutMove = (AppCompatButton) findViewById(R.id.btn_layout_move); + btnLayoutScale = (AppCompatButton) findViewById(R.id.btn_layout_scale); + btnLayoutCrossMove = (AppCompatButton) findViewById(R.id.btn_layout_cross); btnCard.setOnClickListener(this); btnLayout.setOnClickListener(this); btnLayoutBlur.setOnClickListener(this); btnLayoutMove.setOnClickListener(this); + btnLayoutScale.setOnClickListener(this); + btnLayoutCrossMove.setOnClickListener(this); } @Override @@ -36,15 +41,23 @@ public void onClick(View v) { startActivity(it); } else if (v == btnLayout) { Intent it = new Intent(this, CardFlowLayoutActivity.class); - it.putExtra("mode", CardFlowLayoutActivity.MODE_DEFAULT); + it.putExtra(CardFlowLayoutActivity.ANIM_MODE, CardFlowLayoutActivity.MODE_DEFAULT); startActivity(it); } else if (v == btnLayoutBlur) { Intent it = new Intent(this, CardFlowLayoutActivity.class); - it.putExtra("mode", CardFlowLayoutActivity.MODE_BLUR); + it.putExtra(CardFlowLayoutActivity.ANIM_MODE, CardFlowLayoutActivity.MODE_BLUR); + startActivity(it); + } else if (v == btnLayoutMove) { + Intent it = new Intent(this, CardFlowLayoutActivity.class); + it.putExtra(CardFlowLayoutActivity.ANIM_MODE, CardFlowLayoutActivity.MODE_MOVE); + startActivity(it); + } else if (v == btnLayoutScale) { + Intent it = new Intent(this, CardFlowLayoutActivity.class); + it.putExtra(CardFlowLayoutActivity.ANIM_MODE, CardFlowLayoutActivity.MODE_SCALE); startActivity(it); } else { Intent it = new Intent(this, CardFlowLayoutActivity.class); - it.putExtra("mode", CardFlowLayoutActivity.MODE_MOVE); + it.putExtra(CardFlowLayoutActivity.ANIM_MODE, CardFlowLayoutActivity.MODE_CROSS_MOVE); startActivity(it); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f266eb1..e7edf03 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -36,4 +36,20 @@ android:textAllCaps="false" android:layout_marginTop="20dp" android:text="ECardFlowLayout(Move)"/> + + + + \ No newline at end of file diff --git a/ecardflow/src/main/java/moe/codeest/ecardflow/ECardFlowLayout.java b/ecardflow/src/main/java/moe/codeest/ecardflow/ECardFlowLayout.java index 4feb92c..813cb73 100644 --- a/ecardflow/src/main/java/moe/codeest/ecardflow/ECardFlowLayout.java +++ b/ecardflow/src/main/java/moe/codeest/ecardflow/ECardFlowLayout.java @@ -109,7 +109,7 @@ private void initViewPager() { } } if (mViewPager == null) { - throw new RuntimeException("Can't find ViewPager in ECardFlowLayout"); + throw new IllegalStateException("Can't find ViewPager in ECardFlowLayout"); } mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override @@ -225,7 +225,7 @@ public void run() { } }); } else { - throw new RuntimeException("setImageProvider is necessary"); + throw new IllegalArgumentException("setImageProvider is necessary"); } } @@ -373,7 +373,7 @@ public void setImageProvider(ImageProvider provider) { curBp = loadBitmap(0); nextBp = loadBitmap(1); if (mAnimMode == null) { - throw new RuntimeException("You should setAnimMode before setImageProvider"); + throw new IllegalStateException("You should setAnimMode before setImageProvider"); } if (mBlurImage != null) { curBlurBp = blurBitmap(0); diff --git a/ecardflow/src/main/java/moe/codeest/ecardflow/mode/BlurAnimMode.java b/ecardflow/src/main/java/moe/codeest/ecardflow/mode/BlurAnimMode.java index e16dc12..b543aeb 100644 --- a/ecardflow/src/main/java/moe/codeest/ecardflow/mode/BlurAnimMode.java +++ b/ecardflow/src/main/java/moe/codeest/ecardflow/mode/BlurAnimMode.java @@ -15,8 +15,8 @@ public BlurAnimMode() { this.mRadius = DEFAULT_BLUR_RADIUS; } - public BlurAnimMode(int mRadius) { - this.mRadius = mRadius; + public BlurAnimMode(int radius) { + this.mRadius = radius; } @Override diff --git a/ecardflow/src/main/java/moe/codeest/ecardflow/mode/CrossMoveAnimMode.java b/ecardflow/src/main/java/moe/codeest/ecardflow/mode/CrossMoveAnimMode.java new file mode 100644 index 0000000..3e11d69 --- /dev/null +++ b/ecardflow/src/main/java/moe/codeest/ecardflow/mode/CrossMoveAnimMode.java @@ -0,0 +1,36 @@ +package moe.codeest.ecardflow.mode; + +import android.widget.ImageView; + +/** + * Created by codeest on 2017/2/25. + */ + +public class CrossMoveAnimMode implements AnimMode { + + private static final float DEFAULT_MOVE_SCALE = 1.3f; + private float mScale; + + public CrossMoveAnimMode() { + this.mScale = DEFAULT_MOVE_SCALE; + } + + public CrossMoveAnimMode(float mScale) { + this.mScale = mScale; + } + + @Override + public void transformPage(ImageView ivBg, float position, int direction) { + ivBg.setScaleX(mScale); + ivBg.setScaleY(mScale); + float totalMoveWidth = ivBg.getWidth() * ((mScale - 1) / 2); + int lastPosition = Math.round(position); + float mFraction; + if (lastPosition % 2 == 0) { + mFraction = -1 * (float) Math.sin(Math.PI * position); + } else { + mFraction = (float) Math.sin(Math.PI * position); + } + ivBg.setTranslationY(totalMoveWidth * mFraction); + } +} diff --git a/ecardflow/src/main/java/moe/codeest/ecardflow/mode/ScaleAnimMode.java b/ecardflow/src/main/java/moe/codeest/ecardflow/mode/ScaleAnimMode.java new file mode 100644 index 0000000..6c8a557 --- /dev/null +++ b/ecardflow/src/main/java/moe/codeest/ecardflow/mode/ScaleAnimMode.java @@ -0,0 +1,28 @@ +package moe.codeest.ecardflow.mode; + +import android.widget.ImageView; + +/** + * Created by codeest on 2017/2/25. + */ + +public class ScaleAnimMode implements AnimMode { + + private static final int DEFAULT_SCALE_RATE = 1; + private int mScaleRate; + + public ScaleAnimMode() { + this.mScaleRate = DEFAULT_SCALE_RATE; + } + + public ScaleAnimMode(int scaleRate) { + this.mScaleRate = scaleRate; + } + + @Override + public void transformPage(ImageView ivBg, float position, int direction) { + float mFraction = mScaleRate * (float) Math.abs(Math.sin(Math.PI * position)); + ivBg.setScaleX(1 + mFraction); + ivBg.setScaleY(1 + mFraction); + } +}