26 | * Add com.wajahatkarim3.easyflipview.EasyFlipView into your XML layouts with two direct children
27 | * views and you are done!
28 | * For more information, check http://github.com/wajahatkarim3/EasyFlipView
29 | *
30 | * @author Wajahat Karim (http://wajahatkarim.com)
31 | * @version 3.0.0 28/03/2019
32 | */
33 | public class EasyFlipView extends FrameLayout {
34 |
35 | public static final String TAG = EasyFlipView.class.getSimpleName();
36 |
37 | public static final int DEFAULT_FLIP_DURATION = 400;
38 | public static final int DEFAULT_AUTO_FLIP_BACK_TIME = 1000;
39 | private int animFlipHorizontalOutId = R.animator.animation_horizontal_flip_out;
40 | private int animFlipHorizontalInId = R.animator.animation_horizontal_flip_in;
41 | private int animFlipHorizontalRightOutId = R.animator.animation_horizontal_right_out;
42 | private int animFlipHorizontalRightInId = R.animator.animation_horizontal_right_in;
43 | private int animFlipVerticalOutId = R.animator.animation_vertical_flip_out;
44 | private int animFlipVerticalInId = R.animator.animation_vertical_flip_in;
45 | private int animFlipVerticalFrontOutId = R.animator.animation_vertical_front_out;
46 | private int animFlipVerticalFrontInId = R.animator.animation_vertical_flip_front_in;
47 |
48 | public enum FlipState {
49 | FRONT_SIDE, BACK_SIDE
50 | }
51 |
52 | private AnimatorSet mSetRightOut;
53 | private AnimatorSet mSetLeftIn;
54 | private AnimatorSet mSetTopOut;
55 | private AnimatorSet mSetBottomIn;
56 | private boolean mIsBackVisible = false;
57 | private View mCardFrontLayout;
58 | private View mCardBackLayout;
59 | private String flipType = "vertical";
60 | private String flipTypeFrom = "right";
61 |
62 |
63 | private boolean flipOnTouch;
64 | private int flipDuration;
65 | private boolean flipEnabled;
66 | private boolean flipOnceEnabled;
67 | private boolean autoFlipBack;
68 | private int autoFlipBackTime;
69 |
70 | private Context context;
71 | private float x1;
72 | private float y1;
73 |
74 | private FlipState mFlipState = FlipState.FRONT_SIDE;
75 |
76 | private OnFlipAnimationListener onFlipListener = null;
77 |
78 | private GestureDetectorCompat gestureDetector;
79 |
80 | public EasyFlipView(Context context) {
81 | super(context);
82 | this.context = context;
83 | init(context, null);
84 | }
85 |
86 | public EasyFlipView(Context context, AttributeSet attrs) {
87 | super(context, attrs);
88 | this.context = context;
89 | init(context, attrs);
90 | }
91 |
92 | private void init(Context context, AttributeSet attrs) {
93 | // Setting Default Values
94 | flipOnTouch = true;
95 | flipDuration = DEFAULT_FLIP_DURATION;
96 | flipEnabled = true;
97 | flipOnceEnabled = false;
98 | autoFlipBack = false;
99 | autoFlipBackTime = DEFAULT_AUTO_FLIP_BACK_TIME;
100 |
101 | // Check for the attributes
102 | if (attrs != null) {
103 | // Attribute initialization
104 | final TypedArray attrArray =
105 | context.obtainStyledAttributes(attrs, R.styleable.easy_flip_view, 0, 0);
106 | try {
107 | flipOnTouch = attrArray.getBoolean(R.styleable.easy_flip_view_flipOnTouch, true);
108 | flipDuration = attrArray.getInt(R.styleable.easy_flip_view_flipDuration, DEFAULT_FLIP_DURATION);
109 | flipEnabled = attrArray.getBoolean(R.styleable.easy_flip_view_flipEnabled, true);
110 | flipOnceEnabled = attrArray.getBoolean(R.styleable.easy_flip_view_flipOnceEnabled, false);
111 | autoFlipBack = attrArray.getBoolean(R.styleable.easy_flip_view_autoFlipBack, false);
112 | autoFlipBackTime = attrArray.getInt(R.styleable.easy_flip_view_autoFlipBackTime, DEFAULT_AUTO_FLIP_BACK_TIME);
113 | flipType = attrArray.getString(R.styleable.easy_flip_view_flipType);
114 | flipTypeFrom = attrArray.getString(R.styleable.easy_flip_view_flipFrom);
115 |
116 | if (TextUtils.isEmpty(flipType)) {
117 | flipType = "vertical";
118 | }
119 | if (TextUtils.isEmpty(flipTypeFrom)) {
120 | flipTypeFrom = "left";
121 | }
122 | //animFlipInId = attrArray.getResourceId(R.styleable.easy_flip_view_animFlipInId, R.animator.animation_horizontal_flip_in);
123 | //animFlipOutId = attrArray.getResourceId(R.styleable.easy_flip_view_animFlipOutId, R.animator.animation_horizontal_flip_out);
124 | } finally {
125 | attrArray.recycle();
126 | }
127 | }
128 |
129 | loadAnimations();
130 | }
131 |
132 | @Override
133 | protected void onFinishInflate() {
134 | super.onFinishInflate();
135 |
136 | if (getChildCount() > 2) {
137 | throw new IllegalStateException("EasyFlipView can host only two direct children!");
138 | }
139 |
140 | findViews();
141 | changeCameraDistance();
142 | setupInitializations();
143 | initGestureDetector();
144 | }
145 |
146 | @Override
147 | public void addView(View v, int pos, ViewGroup.LayoutParams params) {
148 | if (getChildCount() == 2) {
149 | throw new IllegalStateException("EasyFlipView can host only two direct children!");
150 | }
151 |
152 | super.addView(v, pos, params);
153 |
154 | findViews();
155 | changeCameraDistance();
156 | }
157 |
158 | @Override
159 | public void removeView(View v) {
160 | super.removeView(v);
161 |
162 | findViews();
163 | }
164 |
165 | @Override
166 | public void removeAllViewsInLayout() {
167 | super.removeAllViewsInLayout();
168 |
169 | // Reset the state
170 | mFlipState = FlipState.FRONT_SIDE;
171 |
172 | findViews();
173 | }
174 |
175 | private void findViews() {
176 | // Invalidation since we use this also on removeView
177 | mCardBackLayout = null;
178 | mCardFrontLayout = null;
179 |
180 | int childs = getChildCount();
181 | if (childs < 1) {
182 | return;
183 | }
184 |
185 | if (childs < 2) {
186 | // Only invalidate flip state if we have a single child
187 | mFlipState = FlipState.FRONT_SIDE;
188 |
189 | mCardFrontLayout = getChildAt(0);
190 | } else if (childs == 2) {
191 | mCardFrontLayout = getChildAt(1);
192 | mCardBackLayout = getChildAt(0);
193 | }
194 |
195 | if (!isFlipOnTouch()) {
196 | mCardFrontLayout.setVisibility(VISIBLE);
197 |
198 | if (mCardBackLayout != null) {
199 | mCardBackLayout.setVisibility(GONE);
200 | }
201 | }
202 | }
203 |
204 | private void setupInitializations()
205 | {
206 | mCardBackLayout.setVisibility(View.GONE);
207 | }
208 |
209 | private void initGestureDetector() {
210 | gestureDetector = new GestureDetectorCompat(this.context, new SwipeDetector());
211 | }
212 |
213 | private void loadAnimations() {
214 | if (flipType.equalsIgnoreCase("horizontal")) {
215 |
216 | if (flipTypeFrom.equalsIgnoreCase("left")) {
217 | mSetRightOut =
218 | (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipHorizontalOutId);
219 | mSetLeftIn =
220 | (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipHorizontalInId);
221 | } else {
222 | mSetRightOut =
223 | (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipHorizontalRightOutId);
224 | mSetLeftIn =
225 | (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipHorizontalRightInId);
226 | }
227 |
228 |
229 | if (mSetRightOut == null || mSetLeftIn == null) {
230 | throw new RuntimeException(
231 | "No Animations Found! Please set Flip in and Flip out animation Ids.");
232 | }
233 |
234 | mSetRightOut.removeAllListeners();
235 | mSetRightOut.addListener(new Animator.AnimatorListener() {
236 | @Override
237 | public void onAnimationStart(Animator animator) {
238 |
239 | }
240 |
241 | @Override
242 | public void onAnimationEnd(Animator animator) {
243 |
244 | if (mFlipState == FlipState.FRONT_SIDE) {
245 | mCardBackLayout.setVisibility(GONE);
246 | mCardFrontLayout.setVisibility(VISIBLE);
247 |
248 | if (onFlipListener != null)
249 | onFlipListener.onViewFlipCompleted(EasyFlipView.this, FlipState.FRONT_SIDE);
250 | } else {
251 | mCardBackLayout.setVisibility(VISIBLE);
252 | mCardFrontLayout.setVisibility(GONE);
253 |
254 | if (onFlipListener != null)
255 | onFlipListener.onViewFlipCompleted(EasyFlipView.this, FlipState.BACK_SIDE);
256 |
257 | // Auto Flip Back
258 | if (autoFlipBack == true)
259 | {
260 | new Handler().postDelayed(new Runnable() {
261 | @Override
262 | public void run() {
263 | flipTheView();
264 | }
265 | }, autoFlipBackTime);
266 | }
267 | }
268 | }
269 |
270 | @Override
271 | public void onAnimationCancel(Animator animator) {
272 |
273 | }
274 |
275 | @Override
276 | public void onAnimationRepeat(Animator animator) {
277 |
278 | }
279 | });
280 | setFlipDuration(flipDuration);
281 | } else {
282 |
283 | if (!TextUtils.isEmpty(flipTypeFrom) && flipTypeFrom.equalsIgnoreCase("front")) {
284 | mSetTopOut = (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipVerticalFrontOutId);
285 | mSetBottomIn =
286 | (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipVerticalFrontInId);
287 | } else {
288 | mSetTopOut = (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipVerticalOutId);
289 | mSetBottomIn =
290 | (AnimatorSet) AnimatorInflater.loadAnimator(this.context, animFlipVerticalInId);
291 | }
292 |
293 | if (mSetTopOut == null || mSetBottomIn == null) {
294 | throw new RuntimeException(
295 | "No Animations Found! Please set Flip in and Flip out animation Ids.");
296 | }
297 |
298 | mSetTopOut.removeAllListeners();
299 | mSetTopOut.addListener(new Animator.AnimatorListener() {
300 | @Override
301 | public void onAnimationStart(Animator animator) {
302 |
303 | }
304 |
305 | @Override
306 | public void onAnimationEnd(Animator animator) {
307 |
308 | if (mFlipState == FlipState.FRONT_SIDE) {
309 | mCardBackLayout.setVisibility(GONE);
310 | mCardFrontLayout.setVisibility(VISIBLE);
311 |
312 | if (onFlipListener != null)
313 | onFlipListener.onViewFlipCompleted(EasyFlipView.this, FlipState.FRONT_SIDE);
314 | } else {
315 | mCardBackLayout.setVisibility(VISIBLE);
316 | mCardFrontLayout.setVisibility(GONE);
317 |
318 | if (onFlipListener != null)
319 | onFlipListener.onViewFlipCompleted(EasyFlipView.this, FlipState.BACK_SIDE);
320 |
321 | // Auto Flip Back
322 | if (autoFlipBack == true)
323 | {
324 | new Handler().postDelayed(new Runnable() {
325 | @Override
326 | public void run() {
327 | flipTheView();
328 | }
329 | }, autoFlipBackTime);
330 | }
331 | }
332 | }
333 |
334 | @Override
335 | public void onAnimationCancel(Animator animator) {
336 |
337 | }
338 |
339 | @Override
340 | public void onAnimationRepeat(Animator animator) {
341 |
342 | }
343 | });
344 | setFlipDuration(flipDuration);
345 | }
346 | }
347 |
348 | private void changeCameraDistance() {
349 | int distance = 8000;
350 | float scale = getResources().getDisplayMetrics().density * distance;
351 |
352 | if (mCardFrontLayout != null) {
353 | mCardFrontLayout.setCameraDistance(scale);
354 | }
355 | if (mCardBackLayout != null) {
356 | mCardBackLayout.setCameraDistance(scale);
357 | }
358 | }
359 |
360 | /**
361 | * Play the animation of flipping and flip the view for one side!
362 | */
363 | public void flipTheView() {
364 | if (!flipEnabled || getChildCount() < 2) return;
365 |
366 | if (flipOnceEnabled && mFlipState == FlipState.BACK_SIDE)
367 | return;
368 |
369 | if (flipType.equalsIgnoreCase("horizontal")) {
370 | if (mSetRightOut.isRunning() || mSetLeftIn.isRunning()) return;
371 |
372 | mCardBackLayout.setVisibility(VISIBLE);
373 | mCardFrontLayout.setVisibility(VISIBLE);
374 |
375 | if (mFlipState == FlipState.FRONT_SIDE) {
376 | // From front to back
377 | mSetRightOut.setTarget(mCardFrontLayout);
378 | mSetLeftIn.setTarget(mCardBackLayout);
379 | mSetRightOut.start();
380 | mSetLeftIn.start();
381 | mIsBackVisible = true;
382 | mFlipState = FlipState.BACK_SIDE;
383 | } else {
384 | // from back to front
385 | mSetRightOut.setTarget(mCardBackLayout);
386 | mSetLeftIn.setTarget(mCardFrontLayout);
387 | mSetRightOut.start();
388 | mSetLeftIn.start();
389 | mIsBackVisible = false;
390 | mFlipState = FlipState.FRONT_SIDE;
391 | }
392 | } else {
393 | if (mSetTopOut.isRunning() || mSetBottomIn.isRunning()) return;
394 |
395 | mCardBackLayout.setVisibility(VISIBLE);
396 | mCardFrontLayout.setVisibility(VISIBLE);
397 |
398 | if (mFlipState == FlipState.FRONT_SIDE) {
399 | // From front to back
400 | mSetTopOut.setTarget(mCardFrontLayout);
401 | mSetBottomIn.setTarget(mCardBackLayout);
402 | mSetTopOut.start();
403 | mSetBottomIn.start();
404 | mIsBackVisible = true;
405 | mFlipState = FlipState.BACK_SIDE;
406 | } else {
407 | // from back to front
408 | mSetTopOut.setTarget(mCardBackLayout);
409 | mSetBottomIn.setTarget(mCardFrontLayout);
410 | mSetTopOut.start();
411 | mSetBottomIn.start();
412 | mIsBackVisible = false;
413 | mFlipState = FlipState.FRONT_SIDE;
414 | }
415 | }
416 | }
417 |
418 | /**
419 | * Flip the view for one side with or without animation.
420 | *
421 | * @param withAnimation true means flip view with animation otherwise without animation.
422 | */
423 | public void flipTheView(boolean withAnimation) {
424 | if (getChildCount() < 2) return;
425 |
426 | if (flipType.equalsIgnoreCase("horizontal")) {
427 | if (!withAnimation) {
428 | mSetLeftIn.setDuration(0);
429 | mSetRightOut.setDuration(0);
430 | boolean oldFlipEnabled = flipEnabled;
431 | flipEnabled = true;
432 |
433 | flipTheView();
434 |
435 | mSetLeftIn.setDuration(flipDuration);
436 | mSetRightOut.setDuration(flipDuration);
437 | flipEnabled = oldFlipEnabled;
438 | } else {
439 | flipTheView();
440 | }
441 | } else {
442 | if (!withAnimation) {
443 | mSetBottomIn.setDuration(0);
444 | mSetTopOut.setDuration(0);
445 | boolean oldFlipEnabled = flipEnabled;
446 | flipEnabled = true;
447 |
448 | flipTheView();
449 |
450 | mSetBottomIn.setDuration(flipDuration);
451 | mSetTopOut.setDuration(flipDuration);
452 | flipEnabled = oldFlipEnabled;
453 | } else {
454 | flipTheView();
455 | }
456 | }
457 | }
458 |
459 | @Override
460 | public boolean dispatchTouchEvent(MotionEvent ev) {
461 | try {
462 | return gestureDetector.onTouchEvent(ev) || super.dispatchTouchEvent(ev);
463 | } catch (Throwable throwable) {
464 | throw new IllegalStateException("Error in dispatchTouchEvent: ", throwable);
465 | }
466 | }
467 |
468 | @Override
469 | public boolean onTouchEvent(MotionEvent event) {
470 | if (isEnabled() && flipOnTouch) {
471 | return gestureDetector.onTouchEvent(event);
472 | } else {
473 | return super.onTouchEvent(event);
474 | }
475 | }
476 |
477 | /**
478 | * Whether view is set to flip on touch or not.
479 | *
480 | * @return true or false
481 | */
482 | public boolean isFlipOnTouch() {
483 | return flipOnTouch;
484 | }
485 |
486 | /**
487 | * Set whether view should be flipped on touch or not!
488 | *
489 | * @param flipOnTouch value (true or false)
490 | */
491 | public void setFlipOnTouch(boolean flipOnTouch) {
492 | this.flipOnTouch = flipOnTouch;
493 | }
494 |
495 | /**
496 | * Returns duration of flip in milliseconds!
497 | *
498 | * @return duration in milliseconds
499 | */
500 | public int getFlipDuration() {
501 | return flipDuration;
502 | }
503 |
504 | /**
505 | * Sets the flip duration (in milliseconds)
506 | *
507 | * @param flipDuration duration in milliseconds
508 | */
509 | public void setFlipDuration(int flipDuration) {
510 | this.flipDuration = flipDuration;
511 | if (flipType.equalsIgnoreCase("horizontal")) {
512 | //mSetRightOut.setDuration(flipDuration);
513 | mSetRightOut.getChildAnimations().get(0).setDuration(flipDuration);
514 | mSetRightOut.getChildAnimations().get(1).setStartDelay(flipDuration / 2);
515 |
516 | //mSetLeftIn.setDuration(flipDuration);
517 | mSetLeftIn.getChildAnimations().get(1).setDuration(flipDuration);
518 | mSetLeftIn.getChildAnimations().get(2).setStartDelay(flipDuration / 2);
519 | } else {
520 | mSetTopOut.getChildAnimations().get(0).setDuration(flipDuration);
521 | mSetTopOut.getChildAnimations().get(1).setStartDelay(flipDuration / 2);
522 |
523 | mSetBottomIn.getChildAnimations().get(1).setDuration(flipDuration);
524 | mSetBottomIn.getChildAnimations().get(2).setStartDelay(flipDuration / 2);
525 | }
526 | }
527 |
528 | /**
529 | * Returns whether view can be flipped only once!
530 | *
531 | * @return true or false
532 | */
533 | public boolean isFlipOnceEnabled() {
534 | return flipOnceEnabled;
535 | }
536 |
537 | /**
538 | * Enable / Disable flip only once feature.
539 | *
540 | * @param flipOnceEnabled true or false
541 | */
542 | public void setFlipOnceEnabled(boolean flipOnceEnabled) {
543 | this.flipOnceEnabled = flipOnceEnabled;
544 | }
545 |
546 |
547 | /**
548 | * Returns whether flip is enabled or not!
549 | *
550 | * @return true or false
551 | */
552 | public boolean isFlipEnabled() {
553 | return flipEnabled;
554 | }
555 |
556 | /**
557 | * Enable / Disable flip view.
558 | *
559 | * @param flipEnabled true or false
560 | */
561 | public void setFlipEnabled(boolean flipEnabled) {
562 | this.flipEnabled = flipEnabled;
563 | }
564 |
565 | /**
566 | * Returns which flip state is currently on of the flip view.
567 | *
568 | * @return current state of flip view
569 | */
570 | public FlipState getCurrentFlipState() {
571 | return mFlipState;
572 | }
573 |
574 | /**
575 | * Returns true if the front side of flip view is visible.
576 | *
577 | * @return true if the front side of flip view is visible.
578 | */
579 | public boolean isFrontSide() {
580 | return (mFlipState == FlipState.FRONT_SIDE);
581 | }
582 |
583 | /**
584 | * Returns true if the back side of flip view is visible.
585 | *
586 | * @return true if the back side of flip view is visible.
587 | */
588 | public boolean isBackSide() {
589 | return (mFlipState == FlipState.BACK_SIDE);
590 | }
591 |
592 | public OnFlipAnimationListener getOnFlipListener() {
593 | return onFlipListener;
594 | }
595 |
596 | public void setOnFlipListener(OnFlipAnimationListener onFlipListener) {
597 | this.onFlipListener = onFlipListener;
598 | }
599 |
600 | /*
601 | public @AnimatorRes int getAnimFlipOutId() {
602 | return animFlipOutId;
603 | }
604 |
605 | public void setAnimFlipOutId(@AnimatorRes int animFlipOutId) {
606 | this.animFlipOutId = animFlipOutId;
607 | loadAnimations();
608 | }
609 |
610 | public @AnimatorRes int getAnimFlipInId() {
611 | return animFlipInId;
612 | }
613 |
614 | public void setAnimFlipInId(@AnimatorRes int animFlipInId) {
615 | this.animFlipInId = animFlipInId;
616 | loadAnimations();
617 | }
618 | */
619 |
620 | /**
621 | * Returns true if the Flip Type of animation is Horizontal?
622 | */
623 | public boolean isHorizontalType() {
624 | return flipType.equals("horizontal");
625 | }
626 |
627 | /**
628 | * Returns true if the Flip Type of animation is Vertical?
629 | */
630 | public boolean isVerticalType() {
631 | return flipType.equals("vertical");
632 | }
633 |
634 | /**
635 | * Sets the Flip Type of animation to Horizontal
636 | */
637 | public void setToHorizontalType() {
638 | flipType = "horizontal";
639 | loadAnimations();
640 | }
641 |
642 | /**
643 | * Sets the Flip Type of animation to Vertical
644 | */
645 | public void setToVerticalType() {
646 | flipType = "vertical";
647 | loadAnimations();
648 | }
649 |
650 | /**
651 | * Sets the flip type from direction to right
652 | */
653 | public void setFlipTypeFromRight() {
654 | if (flipType.equals("horizontal"))
655 | flipTypeFrom = "right";
656 | else flipTypeFrom = "front";
657 | loadAnimations();
658 | }
659 |
660 | /**
661 | * Sets the flip type from direction to left
662 | */
663 | public void setFlipTypeFromLeft() {
664 | if (flipType.equals("horizontal"))
665 | flipTypeFrom = "left";
666 | else flipTypeFrom = "back";
667 | loadAnimations();
668 | }
669 |
670 | /**
671 | * Sets the flip type from direction to front
672 | */
673 | public void setFlipTypeFromFront() {
674 | if (flipType.equals("vertical"))
675 | flipTypeFrom = "front";
676 | else flipTypeFrom = "right";
677 | loadAnimations();
678 | }
679 |
680 | /**
681 | * Sets the flip type from direction to back
682 | */
683 | public void setFlipTypeFromBack() {
684 | if (flipType.equals("vertical"))
685 | flipTypeFrom = "back";
686 | else flipTypeFrom = "left";
687 | loadAnimations();
688 | }
689 |
690 | /**
691 | * Returns the flip type from direction. For horizontal, it will be either right or left and for vertical, it will be front or back.
692 | */
693 | public String getFlipTypeFrom() {
694 | return flipTypeFrom;
695 | }
696 |
697 | /**
698 | * Returns true if Auto Flip Back is enabled
699 | */
700 | public boolean isAutoFlipBack() {
701 | return autoFlipBack;
702 | }
703 |
704 | /**
705 | * Set if the card should be flipped back to original front side.
706 | * @param autoFlipBack true if card should be flipped back to froont side
707 | */
708 | public void setAutoFlipBack(boolean autoFlipBack) {
709 | this.autoFlipBack = autoFlipBack;
710 | }
711 |
712 | /**
713 | * Return the time in milliseconds to auto flip back to original front side.
714 | * @return
715 | */
716 | public int getAutoFlipBackTime() {
717 | return autoFlipBackTime;
718 | }
719 |
720 | /**
721 | * Set the time in milliseconds to auto flip back the view to the original front side
722 | * @param autoFlipBackTime The time in milliseconds
723 | */
724 | public void setAutoFlipBackTime(int autoFlipBackTime) {
725 | this.autoFlipBackTime = autoFlipBackTime;
726 | }
727 |
728 | /**
729 | * The Flip Animation Listener for animations and flipping complete listeners
730 | */
731 | public interface OnFlipAnimationListener {
732 | /**
733 | * Called when flip animation is completed.
734 | *
735 | * @param newCurrentSide After animation, the new side of the view. Either can be
736 | * FlipState.FRONT_SIDE or FlipState.BACK_SIDE
737 | */
738 | void onViewFlipCompleted(EasyFlipView easyFlipView, FlipState newCurrentSide);
739 | }
740 |
741 | private class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
742 |
743 | @Override
744 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
745 | return false;
746 | }
747 |
748 | @Override
749 | public boolean onSingleTapConfirmed(MotionEvent e) {
750 | if (isEnabled() && flipOnTouch) {
751 | flipTheView();
752 | }
753 | return super.onSingleTapConfirmed(e);
754 | }
755 |
756 | @Override
757 | public boolean onDown(MotionEvent e) {
758 | if (isEnabled() && flipOnTouch) {
759 | return true;
760 | }
761 | return super.onDown(e);
762 | }
763 | }
764 | }
765 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/animator/animation_horizontal_flip_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
17 |
18 |
24 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/animator/animation_horizontal_flip_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
15 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/animator/animation_horizontal_right_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
15 |
21 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/animator/animation_horizontal_right_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
17 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/animator/animation_vertical_flip_front_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
16 |
17 |
23 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/animator/animation_vertical_flip_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
16 |
17 |
23 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/animator/animation_vertical_flip_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
15 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/animator/animation_vertical_front_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
15 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/EasyFlipView/src/main/res/values/integers.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 500
4 | 250
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
18 |
19 |
20 | A quick and easy flip view through which you can create views with two sides like credit cards, poker cards etc.
21 |
22 | 
23 |
24 | ✔️ Changelog
25 | =========
26 | Changes exist in the [releases](https://github.com/wajahatkarim3/EasyFlipView/releases) tab.
27 |
28 | 💻 Installation
29 | ============
30 | Add this in your app's `build.gradle` file:
31 | ```groovy
32 | dependencies {
33 | implementation 'com.wajahatkarim:EasyFlipView:3.0.3'
34 | }
35 | ```
36 |
37 | From the version `3.0.0`, this library will only support Android X naming artifacts. If you want to use the `android.support` versions, then use `2.1.2` version.
38 |
39 | Or add EasyFlipView as a new dependency inside your pom.xml
40 |
41 | ```xml
42 |
43 | com.wajahatkarim
44 | EasyFlipView
45 | 3.0.3
46 | pom
47 |
48 | ```
49 | ❔ Usage
50 | =====
51 |
52 | XML
53 | ---
54 | EasyFlipView In XML layouts("Vertical")
55 | ```xml
56 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | ```
76 |
77 | EasyFlipView In XML layouts("Horizontal")
78 | ```xml
79 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 | ```
98 |
99 | # 🎨 Customizations & Attributes
100 | All customizable attributes for EasyFlipView
101 |
102 |
Attribute Name
103 |
Default Value
104 |
Description
105 |
106 |
app:flipOnTouch="true"
107 |
true
108 |
Whether card should be flipped on touch or not.
109 |
110 |
111 |
app:flipDuration="400"
112 |
400
113 |
The duration of flip animation in milliseconds.
114 |
115 |
116 |
app:flipEnabled="true"
117 |
true
118 |
If this is set to false, then it won't flip ever in Single View and it has to be always false for RecyclerView
119 |
120 |
121 |
app:flipType="horizontal"
122 |
vertical
123 |
Whether card should flip in vertical or horizontal
124 |
125 |
126 |
app:flipType="horizontal"
127 |
vertical
128 |
Whether card should flip in vertical or horizontal
129 |
130 |
131 |
app:flipFrom="right"
132 | app:flipFrom="back"
133 |
left
134 | front
135 |
Whether card should flip from left to right Or right to left(Horizontal type) or car should flip to front or back(Vertical type)
136 |
137 |
138 |
app:autoFlipBack="true"
139 |
false
140 |
If this is set to true, then he card will be flipped back to original front side after the time set in autoFlipBackTime.
141 |
142 |
143 |
app:autoFlipBackTime="1000"
144 |
1000
145 |
The time in milliseconds (ms), after the card will be flipped back to original front side.
146 |
147 |
148 |
149 | In Code (Java)
150 | ----
151 | ```java
152 | // Flips the view with or without animation
153 | mYourFlipView.flipTheView();
154 | mYourFlipView.flipTheView(false);
155 |
156 | // Sets and Gets the Flip Animation Duration in milliseconds (Default is 400 ms)
157 | mYourFlipView.setFlipDuration(1000);
158 | int dur = mYourFlipView.getFlipDuration();
159 |
160 | // Sets and gets the flip enable status (Default is true)
161 | mYourFlipView.setFlipEnabled(false);
162 | boolean flipStatus = mYourFlipView.isFlipEnabled();
163 |
164 | // Sets and gets the flip on touch status (Default is true)
165 | mYourFlipView.setFlipOntouch(false);
166 | boolean flipTouchStatus = mYourFlipView.isFlipOnTouch();
167 |
168 | // Get current flip state in enum (FlipState.FRONT_SIDE or FlipState.BACK_SIDE)
169 | EasyFlipView.FlipState flipSide = mYourFlipView.getCurrentFlipState();
170 |
171 | // Get whether front/back side of flip is visible or not.
172 | boolean frontVal = mYourFlipView.isFrontSide();
173 | boolean backVal = mYourFlipView.isBackSide();
174 |
175 | // Get/Set the FlipType to FlipType.Horizontal
176 | boolean isHorizontal = mYourFlipView.isHorizontalType();
177 | mYourFlipView.setToHorizontalType();
178 |
179 | // Get/Set the FlipType to FlipType.Vertical
180 | boolean isVertical = mYourFlipView.isVerticalType();
181 | mYourFlipView.setToVerticalType();
182 |
183 | // Get/Set if the auto flip back is enabled
184 | boolean isAutoFlipBackEnabled = mYourFlipView.isAutoFlipBack();
185 | mYourFlipView.setAutoFlipBack(true);
186 |
187 | // Get/Set the time in milliseconds (ms) after the view is auto flip back to original front side
188 | int autoflipBackTimeInMilliseconds = mYourFlipView.getAutoFlipBackTime();
189 | mYourFlipView.setAutoFlipBackTime(2000);
190 |
191 | // Sets the animation direction from left (horizontal) and back (vertical)
192 | easyFlipView.setFlipTypeFromLeft();
193 |
194 | // Sets the animation direction from right (horizontal) and front (vertical)
195 | easyFlipView.setFlipTypeFromRight();
196 |
197 | // Sets the animation direction from front (vertical) and right (horizontal)
198 | easyFlipView.setFlipTypeFromFront();
199 |
200 | // Sets the animation direction from back (vertical) and left (horizontal)
201 | easyFlipView.setFlipTypeFromBack();
202 |
203 | // Returns the flip type from direction. For horizontal, it will be either right or left and for vertical, it will be front or back.
204 | easyFlipView.getFlipTypeFrom();
205 |
206 | ```
207 |
208 | Flip Animation Listener
209 | ---
210 | ```java
211 | EasyFlipView easyFlipView = (EasyFlipView) findViewById(R.id.easyFlipView);
212 | easyFlipView.setOnFlipListener(new EasyFlipView.OnFlipAnimationListener() {
213 | @Override
214 | public void onViewFlipCompleted(EasyFlipView flipView, EasyFlipView.FlipState newCurrentSide)
215 | {
216 |
217 | // ...
218 | // Your code goes here
219 | // ...
220 |
221 | }
222 | });
223 | ```
224 |
225 | ❌ Known Issues
226 | =============
227 |
228 | ## Clipping when rotation animation is running
229 |
230 | To avoid clipping of top/bottom while flipping the view, you can disable it through XML like this
231 |
232 | Add `android:clipChildren="false"` to the parent/root view of `EasyFlipView` component. And add `android:clipToPadding="false"` to the `EasyFlipView` itself. Special thanks to @ueen for this fix #64
233 |
234 |
235 | ## EasyFlipView in RecyclerViews
236 | The `EasyFlipView` doesn't flip when used in `RecyclerView`. This is because the `EasyFlipView` uses the `onTouch()` method to intercept the touch events and flip the view accordingly. One easier solution is to disable the `flipOnTouch` attribute in XML by this.
237 | ```xml
238 | app:flipOnTouch="false"
239 | ```
240 | Now, your `RecyclerView` will scroll but the `EasyFlipView` will not flip or animate on touch etc. You will have to manually flip the view by calling the method `mYourFlipView.flipTheView()` inside the adapter or `ViewHolder` class of the `RecyclerView`. For example,
241 | ```java
242 | public class MyRecyclerViewAdapter extends RecyclerView.Adapter {
243 |
244 | private List