diff --git a/.idea/misc.xml b/.idea/misc.xml index ba7052b..59f8a0c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -24,7 +24,7 @@ - + diff --git a/.idea/modules.xml b/.idea/modules.xml index 9a37092..c3b1192 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,9 +2,11 @@ - + + - + + diff --git a/EasyFlipView/build.gradle b/EasyFlipView/build.gradle index b512bd8..009c532 100644 --- a/EasyFlipView/build.gradle +++ b/EasyFlipView/build.gradle @@ -21,7 +21,7 @@ ext { siteUrl = 'https://github.com/wajahatkarim3/EasyFlipView' gitUrl = 'https://github.com/wajahatkarim3/EasyFlipView.git' - libraryVersion = '2.0.2' + libraryVersion = '2.0.5' developerId = 'wajahatkarim3' developerName = 'Wajahat Karim' diff --git a/EasyFlipView/src/main/java/com/wajahatkarim3/easyflipview/EasyFlipView.java b/EasyFlipView/src/main/java/com/wajahatkarim3/easyflipview/EasyFlipView.java index 1f9177e..a2fb717 100644 --- a/EasyFlipView/src/main/java/com/wajahatkarim3/easyflipview/EasyFlipView.java +++ b/EasyFlipView/src/main/java/com/wajahatkarim3/easyflipview/EasyFlipView.java @@ -15,491 +15,541 @@ /** * A quick and easy flip view through which you can create views with two sides like credit cards, * poker cards, flash cards etc. - * + *

* Add com.wajahatkarim3.easyflipview.EasyFlipView into your XML layouts with two direct children * views and you are done! * For more information, check http://github.com/wajahatkarim3/EasyFlipView * * @author Wajahat Karim (http://wajahatkarim.com) - * @version 1.0.1 01/11/2017 + * @version 2.0.4 18/12/2017 */ public class EasyFlipView extends FrameLayout { - public static final String TAG = EasyFlipView.class.getSimpleName(); - - public static final int DEFAULT_FLIP_DURATION = 400; - private int animFlipHorizontalOutId = R.animator.animation_horizontal_flip_out; - private int animFlipHorizontalInId = R.animator.animation_horizontal_flip_in; - private int animFlipVerticalOutId = R.animator.animation_vertical_flip_out; - private int animFlipVerticalInId = R.animator.animation_vertical_flip_in; - - public enum FlipState { - FRONT_SIDE, BACK_SIDE - } - - private AnimatorSet mSetRightOut; - private AnimatorSet mSetLeftIn; - private AnimatorSet mSetTopOut; - private AnimatorSet mSetBottomIn; - private boolean mIsBackVisible = false; - private View mCardFrontLayout; - private View mCardBackLayout; - private String flipType = "vertical"; - - private boolean flipOnTouch; - private int flipDuration; - private boolean flipEnabled; - - private Context context; - private float x1; - private float y1; - - private FlipState mFlipState = FlipState.FRONT_SIDE; - - private OnFlipAnimationListener onFlipListener = null; - - public EasyFlipView(Context context) { - super(context); - this.context = context; - init(context, null); - } - - public EasyFlipView(Context context, AttributeSet attrs) { - super(context, attrs); - this.context = context; - init(context, attrs); - } - - private void init(Context context, AttributeSet attrs) { - // Setting Defaul Values - flipOnTouch = true; - flipDuration = DEFAULT_FLIP_DURATION; - flipEnabled = true; - - // Check for the attributes - if (attrs != null) { - // Attribute initialization - final TypedArray attrArray = - context.obtainStyledAttributes(attrs, R.styleable.easy_flip_view, 0, 0); - try { - flipOnTouch = attrArray.getBoolean(R.styleable.easy_flip_view_flipOnTouch, true); - flipDuration = - attrArray.getInt(R.styleable.easy_flip_view_flipDuration, DEFAULT_FLIP_DURATION); - flipEnabled = attrArray.getBoolean(R.styleable.easy_flip_view_flipEnabled, true); - flipType = attrArray.getString(R.styleable.easy_flip_view_flipType); - if (TextUtils.isEmpty(flipType)) { - flipType = "vertical"; - } - //animFlipInId = attrArray.getResourceId(R.styleable.easy_flip_view_animFlipInId, R.animator.animation_horizontal_flip_in); - //animFlipOutId = attrArray.getResourceId(R.styleable.easy_flip_view_animFlipOutId, R.animator.animation_horizontal_flip_out); - } finally { - attrArray.recycle(); - } - } - - loadAnimations(); - } + public static final String TAG = EasyFlipView.class.getSimpleName(); - @Override - protected void onFinishInflate() { - super.onFinishInflate(); + public static final int DEFAULT_FLIP_DURATION = 400; + private int animFlipHorizontalOutId = R.animator.animation_horizontal_flip_out; + private int animFlipHorizontalInId = R.animator.animation_horizontal_flip_in; + private int animFlipVerticalOutId = R.animator.animation_vertical_flip_out; + private int animFlipVerticalInId = R.animator.animation_vertical_flip_in; - if (getChildCount() > 2) { - throw new IllegalStateException("EasyFlipView can host only two direct children!"); + public enum FlipState { + FRONT_SIDE, BACK_SIDE } - findViews(); - changeCameraDistance(); - } - - @Override - public void addView(View v, int pos, ViewGroup.LayoutParams params) { - if (getChildCount() == 2) { - throw new IllegalStateException("EasyFlipView can host only two direct children!"); + public static class FlipType { + public static int HORIZONTAL = 0; + public static int VERTICAL = 1; } - super.addView(v, pos, params); + private AnimatorSet mSetRightOut; + private AnimatorSet mSetLeftIn; + private AnimatorSet mSetTopOut; + private AnimatorSet mSetBottomIn; + private boolean mIsBackVisible = false; + private View mCardFrontLayout; + private View mCardBackLayout; + private int flipType = FlipType.VERTICAL; - findViews(); - changeCameraDistance(); - } + private boolean flipOnTouch; + private int flipDuration; + private boolean flipEnabled; - @Override - public void removeView(View v) { - super.removeView(v); + private Context context; + private float x1; + private float y1; - findViews(); - } + private FlipState mFlipState = FlipState.FRONT_SIDE; - @Override - public void removeAllViewsInLayout() { - super.removeAllViewsInLayout(); + private OnFlipAnimationListener onFlipListener = null; - // Reset the state - mFlipState = FlipState.FRONT_SIDE; + public EasyFlipView(Context context) { + super(context); + this.context = context; + init(context, null); + } - findViews(); - } + public EasyFlipView(Context context, AttributeSet attrs) { + super(context, attrs); + this.context = context; + init(context, attrs); + } - private void findViews() { - // Invalidation since we use this also on removeView - mCardBackLayout = null; - mCardFrontLayout = null; + private void init(Context context, AttributeSet attrs) { + // Setting Defaul Values + flipOnTouch = true; + flipDuration = DEFAULT_FLIP_DURATION; + flipEnabled = true; + flipType = FlipType.VERTICAL; + + // Check for the attributes + if (attrs != null) { + // Attribute initialization + final TypedArray attrArray = + context.obtainStyledAttributes(attrs, R.styleable.easy_flip_view, 0, 0); + try { + flipOnTouch = attrArray.getBoolean(R.styleable.easy_flip_view_flipOnTouch, true); + flipDuration = + attrArray.getInt(R.styleable.easy_flip_view_flipDuration, DEFAULT_FLIP_DURATION); + flipEnabled = attrArray.getBoolean(R.styleable.easy_flip_view_flipEnabled, true); + + flipType = attrArray.getInt(R.styleable.easy_flip_view_flipType, FlipType.HORIZONTAL); + + //flipType = attrArray.getString(R.styleable.easy_flip_view_flipType); + //animFlipInId = attrArray.getResourceId(R.styleable.easy_flip_view_animFlipInId, R.animator.animation_horizontal_flip_in); + //animFlipOutId = attrArray.getResourceId(R.styleable.easy_flip_view_animFlipOutId, R.animator.animation_horizontal_flip_out); + } finally { + attrArray.recycle(); + } + } - int childs = getChildCount(); - if (childs < 1) { - return; + loadAnimations(); } - if (childs < 2) { - // Only invalidate flip state if we have a single child - mFlipState = FlipState.FRONT_SIDE; + @Override + protected void onFinishInflate() { + super.onFinishInflate(); - mCardFrontLayout = getChildAt(0); - } else if (childs == 2) { - mCardFrontLayout = getChildAt(1); - mCardBackLayout = getChildAt(0); + if (getChildCount() > 2) { + throw new IllegalStateException("EasyFlipView can host only two direct children!"); + } + + findViews(); + changeCameraDistance(); } - if (!isFlipOnTouch()) { - mCardFrontLayout.setVisibility(VISIBLE); + @Override + public void addView(View v, int pos, ViewGroup.LayoutParams params) { + if (getChildCount() == 2) { + throw new IllegalStateException("EasyFlipView can host only two direct children!"); + } + + super.addView(v, pos, params); - if (mCardBackLayout != null) { - mCardBackLayout.setVisibility(GONE); - } + findViews(); + changeCameraDistance(); } - } - private void loadAnimations() { - if (flipType.equalsIgnoreCase("horizontal")) { - mSetRightOut = - (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipHorizontalOutId); - mSetLeftIn = - (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipHorizontalInId); - if (mSetRightOut == null || mSetLeftIn == null) { - throw new RuntimeException( - "No Animations Found! Please set Flip in and Flip out animation Ids."); - } + @Override + public void removeView(View v) { + super.removeView(v); - mSetRightOut.removeAllListeners(); - mSetRightOut.addListener(new Animator.AnimatorListener() { - @Override - public void onAnimationStart(Animator animator) { + findViews(); + } - } + @Override + public void removeAllViewsInLayout() { + super.removeAllViewsInLayout(); - @Override - public void onAnimationEnd(Animator animator) { + // Reset the state + mFlipState = FlipState.FRONT_SIDE; - if (mFlipState == FlipState.FRONT_SIDE) { - mCardBackLayout.setVisibility(GONE); - mCardFrontLayout.setVisibility(VISIBLE); + findViews(); + } - if (onFlipListener != null) onFlipListener.onViewFlipCompleted(FlipState.FRONT_SIDE); - } else { - mCardBackLayout.setVisibility(VISIBLE); - mCardFrontLayout.setVisibility(GONE); + private void findViews() { + // Invalidation since we use this also on removeView + mCardBackLayout = null; + mCardFrontLayout = null; - if (onFlipListener != null) onFlipListener.onViewFlipCompleted(FlipState.BACK_SIDE); - } + int childs = getChildCount(); + if (childs < 1) { + return; } - @Override - public void onAnimationCancel(Animator animator) { + if (childs < 2) { + // Only invalidate flip state if we have a single child + mFlipState = FlipState.FRONT_SIDE; + mCardFrontLayout = getChildAt(0); + } else if (childs == 2) { + mCardFrontLayout = getChildAt(1); + mCardBackLayout = getChildAt(0); } - @Override - public void onAnimationRepeat(Animator animator) { + if (!isFlipOnTouch()) { + mCardFrontLayout.setVisibility(VISIBLE); + + if (mCardBackLayout != null) { + mCardBackLayout.setVisibility(GONE); + } + } + } + private void loadAnimations() { + if (flipType == FlipType.HORIZONTAL) { + mSetRightOut = + (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipHorizontalOutId); + mSetLeftIn = + (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipHorizontalInId); + if (mSetRightOut == null || mSetLeftIn == null) { + throw new RuntimeException( + "No Animations Found! Please set Flip in and Flip out animation Ids."); + } + + mSetRightOut.removeAllListeners(); + mSetRightOut.addListener(new Animator.AnimatorListener() { + @Override + public void onAnimationStart(Animator animator) { + + } + + @Override + public void onAnimationEnd(Animator animator) { + + if (mFlipState == FlipState.FRONT_SIDE) { + mCardBackLayout.setVisibility(GONE); + mCardFrontLayout.setVisibility(VISIBLE); + + if (onFlipListener != null) + onFlipListener.onViewFlipCompleted(EasyFlipView.this, FlipState.FRONT_SIDE); + } else { + mCardBackLayout.setVisibility(VISIBLE); + mCardFrontLayout.setVisibility(GONE); + + if (onFlipListener != null) + onFlipListener.onViewFlipCompleted(EasyFlipView.this, FlipState.BACK_SIDE); + } + } + + @Override + public void onAnimationCancel(Animator animator) { + + } + + @Override + public void onAnimationRepeat(Animator animator) { + + } + }); + setFlipDuration(flipDuration); + } else { + mSetTopOut = (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipVerticalOutId); + mSetBottomIn = + (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipVerticalInId); + + if (mSetTopOut == null || mSetBottomIn == null) { + throw new RuntimeException( + "No Animations Found! Please set Flip in and Flip out animation Ids."); + } + + mSetTopOut.removeAllListeners(); + mSetTopOut.addListener(new Animator.AnimatorListener() { + @Override + public void onAnimationStart(Animator animator) { + + } + + @Override + public void onAnimationEnd(Animator animator) { + + if (mFlipState == FlipState.FRONT_SIDE) { + mCardBackLayout.setVisibility(GONE); + mCardFrontLayout.setVisibility(VISIBLE); + + if (onFlipListener != null) + onFlipListener.onViewFlipCompleted(EasyFlipView.this, FlipState.FRONT_SIDE); + } else { + mCardBackLayout.setVisibility(VISIBLE); + mCardFrontLayout.setVisibility(GONE); + + if (onFlipListener != null) + onFlipListener.onViewFlipCompleted(EasyFlipView.this, FlipState.BACK_SIDE); + } + } + + @Override + public void onAnimationCancel(Animator animator) { + + } + + @Override + public void onAnimationRepeat(Animator animator) { + + } + }); + setFlipDuration(flipDuration); } - }); - setFlipDuration(flipDuration); - } else { - mSetTopOut = (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipVerticalOutId); - mSetBottomIn = - (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipVerticalInId); - - if (mSetTopOut == null || mSetBottomIn == null) { - throw new RuntimeException( - "No Animations Found! Please set Flip in and Flip out animation Ids."); - } - - mSetTopOut.removeAllListeners(); - mSetTopOut.addListener(new Animator.AnimatorListener() { - @Override - public void onAnimationStart(Animator animator) { + } + private void changeCameraDistance() { + int distance = 8000; + float scale = getResources().getDisplayMetrics().density * distance; + + if (mCardFrontLayout != null) { + mCardFrontLayout.setCameraDistance(scale); + } + if (mCardBackLayout != null) { + mCardBackLayout.setCameraDistance(scale); } + } - @Override - public void onAnimationEnd(Animator animator) { + /** + * Play the animation of flipping and flip the view for one side! + */ + public void flipTheView() { + if (!flipEnabled || getChildCount() < 2) return; - if (mFlipState == FlipState.FRONT_SIDE) { - mCardBackLayout.setVisibility(GONE); + if (flipType == FlipType.HORIZONTAL) { + if (mSetRightOut.isRunning() || mSetLeftIn.isRunning()) return; + + mCardBackLayout.setVisibility(VISIBLE); mCardFrontLayout.setVisibility(VISIBLE); - if (onFlipListener != null) onFlipListener.onViewFlipCompleted(FlipState.FRONT_SIDE); - } else { + if (mFlipState == FlipState.FRONT_SIDE) { + // From front to back + mSetRightOut.setTarget(mCardFrontLayout); + mSetLeftIn.setTarget(mCardBackLayout); + mSetRightOut.start(); + mSetLeftIn.start(); + mIsBackVisible = true; + mFlipState = FlipState.BACK_SIDE; + } else { + // from back to front + mSetRightOut.setTarget(mCardBackLayout); + mSetLeftIn.setTarget(mCardFrontLayout); + mSetRightOut.start(); + mSetLeftIn.start(); + mIsBackVisible = false; + mFlipState = FlipState.FRONT_SIDE; + } + } else { + if (mSetTopOut.isRunning() || mSetBottomIn.isRunning()) return; + mCardBackLayout.setVisibility(VISIBLE); - mCardFrontLayout.setVisibility(GONE); + mCardFrontLayout.setVisibility(VISIBLE); - if (onFlipListener != null) onFlipListener.onViewFlipCompleted(FlipState.BACK_SIDE); - } + if (mFlipState == FlipState.FRONT_SIDE) { + // From front to back + mSetTopOut.setTarget(mCardFrontLayout); + mSetBottomIn.setTarget(mCardBackLayout); + mSetTopOut.start(); + mSetBottomIn.start(); + mIsBackVisible = true; + mFlipState = FlipState.BACK_SIDE; + } else { + // from back to front + mSetTopOut.setTarget(mCardBackLayout); + mSetBottomIn.setTarget(mCardFrontLayout); + mSetTopOut.start(); + mSetBottomIn.start(); + mIsBackVisible = false; + mFlipState = FlipState.FRONT_SIDE; + } } + } - @Override - public void onAnimationCancel(Animator animator) { + /** + * Flip the view for one side with or without animation. + * + * @param withAnimation true means flip view with animation otherwise without animation. + */ + public void flipTheView(boolean withAnimation) { + if (getChildCount() < 2) return; + + if (flipType == FlipType.HORIZONTAL) { + if (!withAnimation) { + mSetLeftIn.setDuration(0); + mSetRightOut.setDuration(0); + boolean oldFlipEnabled = flipEnabled; + flipEnabled = true; + + flipTheView(); + + mSetLeftIn.setDuration(flipDuration); + mSetRightOut.setDuration(flipDuration); + flipEnabled = oldFlipEnabled; + } else { + flipTheView(); + } + } else { + if (!withAnimation) { + mSetBottomIn.setDuration(0); + mSetTopOut.setDuration(0); + boolean oldFlipEnabled = flipEnabled; + flipEnabled = true; + + flipTheView(); + + mSetBottomIn.setDuration(flipDuration); + mSetTopOut.setDuration(flipDuration); + flipEnabled = oldFlipEnabled; + } else { + flipTheView(); + } + } + } + @Override + public boolean onTouchEvent(MotionEvent event) { + + if (isEnabled() && flipOnTouch) { + this.getParent().requestDisallowInterceptTouchEvent(true); + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + x1 = event.getX(); + y1 = event.getY(); + return true; + case MotionEvent.ACTION_UP: + float x2 = event.getX(); + float y2 = event.getY(); + float dx = x2 - x1; + float dy = y2 - y1; + float MAX_CLICK_DISTANCE = 0.5f; + if ((dx >= 0 && dx < MAX_CLICK_DISTANCE) && (dy >= 0 && dy < MAX_CLICK_DISTANCE)) { + flipTheView(); + } + return true; + } + } else { + return super.onTouchEvent(event); } + return super.onTouchEvent(event); + } + + /** + * Whether view is set to flip on touch or not. + * + * @return true or false + */ + public boolean isFlipOnTouch() { + return flipOnTouch; + } + + /** + * Set whether view should be flipped on touch or not! + * + * @param flipOnTouch value (true or false) + */ + public void setFlipOnTouch(boolean flipOnTouch) { + this.flipOnTouch = flipOnTouch; + } - @Override - public void onAnimationRepeat(Animator animator) { + /** + * Returns duration of flip in milliseconds! + * + * @return duration in milliseconds + */ + public int getFlipDuration() { + return flipDuration; + } + /** + * Sets the flip duration (in milliseconds) + * + * @param flipDuration duration in milliseconds + */ + public void setFlipDuration(int flipDuration) { + this.flipDuration = flipDuration; + if (flipType == FlipType.HORIZONTAL) { + //mSetRightOut.setDuration(flipDuration); + mSetRightOut.getChildAnimations().get(0).setDuration(flipDuration); + mSetRightOut.getChildAnimations().get(1).setStartDelay(flipDuration / 2); + + //mSetLeftIn.setDuration(flipDuration); + mSetLeftIn.getChildAnimations().get(1).setDuration(flipDuration); + mSetLeftIn.getChildAnimations().get(2).setStartDelay(flipDuration / 2); + } else { + mSetTopOut.getChildAnimations().get(0).setDuration(flipDuration); + mSetTopOut.getChildAnimations().get(1).setStartDelay(flipDuration / 2); + + mSetBottomIn.getChildAnimations().get(1).setDuration(flipDuration); + mSetBottomIn.getChildAnimations().get(2).setStartDelay(flipDuration / 2); } - }); - setFlipDuration(flipDuration); - } - } - - private void changeCameraDistance() { - int distance = 8000; - float scale = getResources().getDisplayMetrics().density * distance; - - if (mCardFrontLayout != null) { - mCardFrontLayout.setCameraDistance(scale); - } - if (mCardBackLayout != null) { - mCardBackLayout.setCameraDistance(scale); - } - } - - /** - * Play the animation of flipping and flip the view for one side! - */ - public void flipTheView() { - if (!flipEnabled || getChildCount() < 2) return; - - if (flipType.equalsIgnoreCase("horizontal")) { - if (mSetRightOut.isRunning() || mSetLeftIn.isRunning()) return; - - mCardBackLayout.setVisibility(VISIBLE); - mCardFrontLayout.setVisibility(VISIBLE); - - if (mFlipState == FlipState.FRONT_SIDE) { - // From front to back - mSetRightOut.setTarget(mCardFrontLayout); - mSetLeftIn.setTarget(mCardBackLayout); - mSetRightOut.start(); - mSetLeftIn.start(); - mIsBackVisible = true; - mFlipState = FlipState.BACK_SIDE; - } else { - // from back to front - mSetRightOut.setTarget(mCardBackLayout); - mSetLeftIn.setTarget(mCardFrontLayout); - mSetRightOut.start(); - mSetLeftIn.start(); - mIsBackVisible = false; - mFlipState = FlipState.FRONT_SIDE; - } - } else { - if (mSetTopOut.isRunning() || mSetBottomIn.isRunning()) return; - - mCardBackLayout.setVisibility(VISIBLE); - mCardFrontLayout.setVisibility(VISIBLE); - - if (mFlipState == FlipState.FRONT_SIDE) { - // From front to back - mSetTopOut.setTarget(mCardFrontLayout); - mSetBottomIn.setTarget(mCardBackLayout); - mSetTopOut.start(); - mSetBottomIn.start(); - mIsBackVisible = true; - mFlipState = FlipState.BACK_SIDE; - } else { - // from back to front - mSetTopOut.setTarget(mCardBackLayout); - mSetBottomIn.setTarget(mCardFrontLayout); - mSetTopOut.start(); - mSetBottomIn.start(); - mIsBackVisible = false; - mFlipState = FlipState.FRONT_SIDE; - } - } - } - - /** - * Flip the view for one side with or without animation. - * - * @param withAnimation true means flip view with animation otherwise without animation. - */ - public void flipTheView(boolean withAnimation) { - if (getChildCount() < 2) return; - - if (flipType.equalsIgnoreCase("horizontal")) { - if (!withAnimation) { - mSetLeftIn.setDuration(0); - mSetRightOut.setDuration(0); - boolean oldFlipEnabled = flipEnabled; - flipEnabled = true; + } - flipTheView(); - - mSetLeftIn.setDuration(flipDuration); - mSetRightOut.setDuration(flipDuration); - flipEnabled = oldFlipEnabled; - } else { - flipTheView(); - } - } else { - if (!withAnimation) { - mSetBottomIn.setDuration(0); - mSetTopOut.setDuration(0); - boolean oldFlipEnabled = flipEnabled; - flipEnabled = true; + /** + * Returns whether flip is enabled or not! + * + * @return true or false + */ + public boolean isFlipEnabled() { + return flipEnabled; + } + + /** + * Enable / Disable flip view. + * + * @param flipEnabled true or false + */ + public void setFlipEnabled(boolean flipEnabled) { + this.flipEnabled = flipEnabled; + } + + /** + * Returns which flip state is currently on of the flip view. + * + * @return current state of flip view + */ + public FlipState getCurrentFlipState() { + return mFlipState; + } + + /** + * Returns true if the front side of flip view is visible. + * + * @return true if the front side of flip view is visible. + */ + public boolean isFrontSide() { + return (mFlipState == FlipState.FRONT_SIDE); + } + + /** + * Returns true if the back side of flip view is visible. + * + * @return true if the back side of flip view is visible. + */ + public boolean isBackSide() { + return (mFlipState == FlipState.BACK_SIDE); + } + + /** + * Returns the current OnFlipAnimationListener. Null if no listener is set. + * @return Returns the current OnFlipAnimationListener. Null if no listener is set. + */ + public OnFlipAnimationListener getOnFlipListener() { + return onFlipListener; + } + + /** + * Sets the OnFlipAnimationListener for the view + * @param onFlipListener + */ + public void setOnFlipListener(OnFlipAnimationListener onFlipListener) { + this.onFlipListener = onFlipListener; + } + + /** + * Returns true if the Flip Type of animation is FlipType.HORIZONTAL? + */ + public boolean isHorizontalType() + { + return flipType == FlipType.HORIZONTAL; + } + + /** + * Returns true if the Flip Type of animation is FlipType.VERTICAL? + */ + public boolean isVerticalType() + { + return flipType == FlipType.VERTICAL; + } + + /** + * Sets the Flip Type of animation to FlipType.HORIZONTAL + */ + public void setToHorizontalType() + { + flipType = FlipType.HORIZONTAL; + } - flipTheView(); - - mSetBottomIn.setDuration(flipDuration); - mSetTopOut.setDuration(flipDuration); - flipEnabled = oldFlipEnabled; - } else { - flipTheView(); - } - } - } - - @Override - public boolean onTouchEvent(MotionEvent event) { - - if (isEnabled() && flipOnTouch) { - this.getParent().requestDisallowInterceptTouchEvent(true); - switch (event.getAction()) { - case MotionEvent.ACTION_DOWN: - x1 = event.getX(); - y1 = event.getY(); - return true; - case MotionEvent.ACTION_UP: - float x2 = event.getX(); - float y2 = event.getY(); - float dx = x2 - x1; - float dy = y2 - y1; - float MAX_CLICK_DISTANCE = 0.5f; - if ((dx >= 0 && dx < MAX_CLICK_DISTANCE) && (dy >= 0 && dy < MAX_CLICK_DISTANCE)) { - flipTheView(); - } - return true; - } - } else { - return super.onTouchEvent(event); - } - return super.onTouchEvent(event); - } - - /** - * Whether view is set to flip on touch or not. - * - * @return true or false - */ - public boolean isFlipOnTouch() { - return flipOnTouch; - } - - /** - * Set whether view should be flipped on touch or not! - * - * @param flipOnTouch value (true or false) - */ - public void setFlipOnTouch(boolean flipOnTouch) { - this.flipOnTouch = flipOnTouch; - } - - /** - * Returns duration of flip in milliseconds! - * - * @return duration in milliseconds - */ - public int getFlipDuration() { - return flipDuration; - } - - /** - * Sets the flip duration (in milliseconds) - * - * @param flipDuration duration in milliseconds - */ - public void setFlipDuration(int flipDuration) { - this.flipDuration = flipDuration; - if (flipType.equalsIgnoreCase("horizontal")) { - //mSetRightOut.setDuration(flipDuration); - mSetRightOut.getChildAnimations().get(0).setDuration(flipDuration); - mSetRightOut.getChildAnimations().get(1).setStartDelay(flipDuration / 2); - - //mSetLeftIn.setDuration(flipDuration); - mSetLeftIn.getChildAnimations().get(1).setDuration(flipDuration); - mSetLeftIn.getChildAnimations().get(2).setStartDelay(flipDuration / 2); - } else { - mSetTopOut.getChildAnimations().get(0).setDuration(flipDuration); - mSetTopOut.getChildAnimations().get(1).setStartDelay(flipDuration / 2); - - mSetBottomIn.getChildAnimations().get(1).setDuration(flipDuration); - mSetBottomIn.getChildAnimations().get(2).setStartDelay(flipDuration / 2); - } - } - - /** - * Returns whether flip is enabled or not! - * - * @return true or false - */ - public boolean isFlipEnabled() { - return flipEnabled; - } - - /** - * Enable / Disable flip view. - * - * @param flipEnabled true or false - */ - public void setFlipEnabled(boolean flipEnabled) { - this.flipEnabled = flipEnabled; - } - - /** - * Returns which flip state is currently on of the flip view. - * - * @return current state of flip view - */ - public FlipState getCurrentFlipState() { - return mFlipState; - } - - /** - * Returns true if the front side of flip view is visible. - * - * @return true if the front side of flip view is visible. - */ - public boolean isFrontSide() { - return (mFlipState == FlipState.FRONT_SIDE); - } - - /** - * Returns true if the back side of flip view is visible. - * - * @return true if the back side of flip view is visible. - */ - public boolean isBackSide() { - return (mFlipState == FlipState.BACK_SIDE); - } - - public OnFlipAnimationListener getOnFlipListener() { - return onFlipListener; - } - - public void setOnFlipListener(OnFlipAnimationListener onFlipListener) { - this.onFlipListener = onFlipListener; - } + /** + * Sets the Flip Type of animation to FlipType.VERTICAL + */ + public void setToVerticalType() + { + flipType = FlipType.VERTICAL; + } /* public @AnimatorRes int getAnimFlipOutId() { @@ -521,16 +571,17 @@ public void setAnimFlipInId(@AnimatorRes int animFlipInId) { } */ - /** - * The Flip Animation Listener for animations and flipping complete listeners - */ - public interface OnFlipAnimationListener { /** - * Called when flip animation is completed. - * - * @param newCurrentSide After animation, the new side of the view. Either can be - * FlipState.FRONT_SIDE or FlipState.BACK_SIDE + * The Flip Animation Listener for animations and flipping complete listeners */ - void onViewFlipCompleted(FlipState newCurrentSide); - } + public interface OnFlipAnimationListener { + /** + * Called when flip animation is completed. + * + * @param easyFlipView The current EasyFlipView instance + * @param newCurrentSide After animation, the new side of the view. Either can be + * FlipState.FRONT_SIDE or FlipState.BACK_SIDE + */ + void onViewFlipCompleted(EasyFlipView easyFlipView, FlipState newCurrentSide); + } } diff --git a/EasyFlipView/src/main/res/values/attrs.xml b/EasyFlipView/src/main/res/values/attrs.xml index 942ba65..9e22d39 100644 --- a/EasyFlipView/src/main/res/values/attrs.xml +++ b/EasyFlipView/src/main/res/values/attrs.xml @@ -3,7 +3,10 @@ - + + + + diff --git a/README.md b/README.md index 3e2aec8..aa79d72 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,18 @@ EasyFlipView A quick and easy flip view through which you can create views with two sides like credit cards, poker cards etc. -![](https://github.com/sachinvarma/EasyFlipView/blob/master/Art/demo.gif) +![](https://github.com/wajahatkarim3/EasyFlipView/blob/master/Art/demo.gif) Changelog ========= -Changes exist in the [releases](https://github.com/sachinvarma/EasyFlipView/releases) tab. +Changes exist in the [releases](https://github.com/wajahatkarim3/EasyFlipView/releases) tab. Installation ============ Add this in your app's build.gradle file: ```groovy dependencies { - compile 'com.github.sachinvarma:EasyFlipView:2.0.4' + compile 'com.wajahatkarim3.EasyFlipView:EasyFlipView:2.0.5' } ``` @@ -23,9 +23,9 @@ Or add EasyFlipView as a new dependency inside your pom.xml ```xml - com.github.sachinvarma + com.wajahatkarim3.EasyFlipView EasyFlipView - 2.0.4 + 2.0.5 pom ``` @@ -73,6 +73,7 @@ EasyFlipView In XML layouts("Horizontal") ``` + All customizable attributes for EasyFlipView @@ -126,6 +127,14 @@ EasyFlipView.FlipState flipSide = mYourFlipView.getCurrentFlipState(); boolean frontVal = mYourFlipView.isFrontSide(); boolean backVal = mYourFlipView.isBackSide(); +// Get/Set the FlipType to FlipType.Horizontal +boolean isHorizontal = mYourFlipView.isHorizontalType(); +mYourFlipView.setToHorizontalType(); + +// Get/Set the FlipType to FlipType.Vertical +boolean isVertical = mYourFlipView.isVerticalType(); +mYourFlipView.setToVerticalType(); + ``` Flip Animation Listener @@ -134,7 +143,7 @@ Flip Animation Listener EasyFlipView easyFlipView = (EasyFlipView) findViewById(R.id.easyFlipView); easyFlipView.setOnFlipListener(new EasyFlipView.OnFlipAnimationListener() { @Override - public void onViewFlipCompleted(EasyFlipView.FlipState newCurrentSide) + public void onViewFlipCompleted(EasyFlipView flipView, EasyFlipView.FlipState newCurrentSide) { // ... @@ -159,6 +168,7 @@ This project needs you! If you would like to support this project's further deve * **[Donate $50](https://www.paypal.me/WajahatKarim/50)**: I really really want to support this project, great job! * **[Donate $100](https://www.paypal.me/WajahatKarim/100)**: You are the man! This project saved me hours (if not days) of struggle and hard work, simply awesome! * **[Donate $2799](https://www.paypal.me/WajahatKarim/2799)**: Go buddy, buy Macbook Pro for yourself! + Of course, you can also choose what you want to donate, all donations are awesome! Developed By @@ -171,12 +181,11 @@ Wajahat Karim - Medium (http://www.medium.com/@wajahatkarim3) - LinkedIn (http://www.linkedin.com/in/wajahatkarim) -Updated By +Special Thanks ========= -``` -Sachin Varma -``` -- LinkedIn (https://www.linkedin.com/in/sachin-varma-58b243118/) +- [**iGio90**](https://github.com/iGio90) for adding dynamic views support [Pull Request # 10](https://github.com/wajahatkarim3/EasyFlipView/pull/10) + +- [**Sachin Varma**](https://www.linkedin.com/in/sachin-varma-58b243118/) for adding vertical animations support [Pull Request # 12](https://github.com/wajahatkarim3/EasyFlipView/pull/12) # How to Contribute @@ -188,7 +197,7 @@ Sachin Varma # License - Copyright 2016 Wajahat Karim + Copyright 2017 Wajahat Karim Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/app/src/main/java/com/wajahatkarim3/easyflipview/demo/SimpleViewFlipActivity.java b/app/src/main/java/com/wajahatkarim3/easyflipview/demo/SimpleViewFlipActivity.java index 43ee8d9..ea4cb05 100644 --- a/app/src/main/java/com/wajahatkarim3/easyflipview/demo/SimpleViewFlipActivity.java +++ b/app/src/main/java/com/wajahatkarim3/easyflipview/demo/SimpleViewFlipActivity.java @@ -39,7 +39,7 @@ public void onClick(View view) { easyFlipView.setOnFlipListener(new EasyFlipView.OnFlipAnimationListener() { @Override - public void onViewFlipCompleted(EasyFlipView.FlipState newCurrentSide) { + public void onViewFlipCompleted(EasyFlipView easyView, EasyFlipView.FlipState newCurrentSide) { Toast.makeText(SimpleViewFlipActivity.this, "Flip Completed! New Side is: " + newCurrentSide, Toast.LENGTH_LONG).show(); }
Attribute Name