Advertisement
tangducthuan

AnimatedLayout

May 20th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.71 KB | None | 0 0
  1. public class AnimatedLayout extends FrameLayout implements AnimationUpdateListener {
  2.     private static final int DEFAULT_RES_ID = -1;
  3.     private static final long DEFAULT_ANIMATION_DURATION = 200;
  4.     /*
  5.     Settings here
  6.      */
  7.     private View background;
  8.     private TransformAnimation backgroundAnimation;
  9.     private TranslateAnimation translateAnimation;
  10.     private boolean shouldPreventKey, scrolling;
  11.     private long duration = DEFAULT_ANIMATION_DURATION;
  12.     private int backgroundResId = -1;
  13.     private StatusListener statusListener;
  14.     public AnimatedLayout(Context context) {
  15.         super(context);
  16.         init(context);
  17.     }
  18.     public AnimatedLayout(Context context, @Nullable AttributeSet attrs) {
  19.         super(context, attrs);
  20.         getStyle(context, attrs);
  21.         init(context);
  22.     }
  23.     public AnimatedLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  24.         super(context, attrs, defStyleAttr);
  25.         getStyle(context, attrs);
  26.         init(context);
  27.     }
  28.     private void getStyle(Context context, AttributeSet attrs) {
  29.         if (attrs == null)
  30.             return;
  31.         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AnimatedLayout, 0, 0);
  32.         try {
  33.             backgroundResId = ta.getResourceId(R.styleable.AnimatedLayout_background_item, DEFAULT_RES_ID);
  34.             duration = (long) ta.getFloat(R.styleable.AnimatedLayout_animation_duration, DEFAULT_ANIMATION_DURATION);
  35.         } finally {
  36.             ta.recycle();
  37.         }
  38.     }
  39.     /**
  40.      * Make recyclerView match the size of layout -> Just set the size of TVRecycler in xml
  41.      */
  42.     private void init(Context context) {
  43.     }
  44.     /**
  45.      * Create background from resourceId with location (in parent) and size
  46.      * @param x coordinate in parent
  47.      * @param y coordinate in parent
  48.      * @param width
  49.      * @param height
  50.      */
  51.     public boolean makeBackground(float x, float y, int width, int height) {
  52.         if (backgroundResId == -1) {
  53.             shouldPreventKey = false;
  54.             if (statusListener != null)
  55.                 statusListener.onStatusChange(false);
  56.             return false;
  57.         }
  58.         if (background == null) {
  59.             background = new View(getContext());
  60.             addView(background);
  61.         } else {
  62.             background.setVisibility(VISIBLE);
  63.         }
  64.         background.setBackgroundResource(backgroundResId);
  65.         background.getLayoutParams().width = width;
  66.         background.getLayoutParams().height = height;
  67.         background.setX(x);
  68.         background.setY(y);
  69.         background.setZ(-1);
  70.         backgroundAnimation = new TransformAnimation(background);
  71.         backgroundAnimation
  72.                 .translate(x, y)
  73.                 .resize(width, height)
  74.                 .withDuration(duration)
  75.                 .withListener(animationListener);
  76.         translateAnimation = new TranslateAnimation(background);
  77.         translateAnimation
  78.                 .translate(x, y, x, y)
  79.                 .withDuration(duration)
  80.                 .withListener(animationListener).withUpdateListener(this);
  81.         return true;
  82.     }
  83.     public boolean isShowing() {
  84.         return background != null && background.getVisibility() == VISIBLE;
  85.     }
  86.     /**
  87.      * Animate background to x, y and resize background to width and height from last x,y and width, height
  88.      * @param x coordinate in parent
  89.      * @param y coordinate in parent
  90.      * @param width final width background
  91.      * @param height final height background
  92.      */
  93.     public void animateBackground(float x, float y, int width, int height) {
  94.         if (background != null) {
  95.             backgroundAnimation.translate(x, y).resize(width, height);
  96.             background.startAnimation(backgroundAnimation);
  97.         }
  98.     }
  99.     /**
  100.      * Move background (Background must created)
  101.      * @param fromX coordinate in parent
  102.      * @param fromY coordinate in parent
  103.      * @param toX coordinate in parent
  104.      * @param toY coordinate in parent
  105.      */
  106.     public void moveBackground(float fromX, float fromY, float toX, float toY) {
  107.         if (background != null) {
  108.             translateAnimation = new TranslateAnimation(background)
  109.                     .translate(fromX, fromY, toX, toY)
  110.                     .withDuration(duration)
  111.                     .withListener(animationListener)
  112.                     .withUpdateListener(this);
  113.             background.startAnimation(translateAnimation);
  114.         }
  115.     }
  116.     /**
  117.      * Move background (Background must created)
  118.      * @param toX coordinate in parent
  119.      * @param toY coordinate in parent
  120.      */
  121.     public void moveBackground(float toX, float toY) {
  122.         if (background != null) {
  123.             try {
  124.                 translateAnimation.cancel();
  125.                 translateAnimation.translate(toX, toY);
  126.                 background.startAnimation(translateAnimation);
  127.             } catch (Exception e) {
  128.                 e.printStackTrace();
  129.             }
  130.         }
  131.     }
  132.     /**
  133.      * Animate background size (Background must created)
  134.      * @param fromWidth start width of background
  135.      * @param fromHeight start height of background
  136.      * @param toWidth end width of background
  137.      * @param toHeight end height of background
  138.      */
  139.     public void resizeBackground(int fromWidth, int fromHeight, int toWidth, int toHeight) {
  140.         if (background != null) {
  141.             ResizeAnimation animation = new ResizeAnimation(background)
  142.                     .resize(fromWidth, fromHeight, toWidth, toHeight);
  143.             background.startAnimation(animation);
  144.         }
  145.     }
  146.     /**
  147.      * Remove background
  148.      */
  149.     public void removeBackground(boolean scrolling) {
  150.         this.scrolling = scrolling;
  151.         if (background != null)
  152.             background.setVisibility(GONE);
  153.     }
  154.     /*
  155.     Method get/set herre
  156.      */
  157.     /**
  158.      * Set background animation duration
  159.      * @param duration
  160.      * @return return this for chain
  161.      */
  162.     public AnimatedLayout setAnimationDuration(long duration) {
  163.         this.duration = duration;
  164.         return this;
  165.     }
  166.     /**
  167.      * Set background resId to render
  168.      * @param backgroundResId id of drawable/layout
  169.      * @return return this for chain
  170.      */
  171.     public AnimatedLayout setBackgroundResId(int backgroundResId) {
  172.         this.backgroundResId = backgroundResId;
  173.         return this;
  174.     }
  175.     public boolean shouldPreventKey() {
  176.         return shouldPreventKey;
  177.     }
  178.     public AnimatedLayout setStatusListener(StatusListener statusListener) {
  179.         this.statusListener = statusListener;
  180.         return this;
  181.     }
  182.     private Animation.AnimationListener animationListener = new Animation.AnimationListener() {
  183.         @Override
  184.         public void onAnimationStart(Animation animation) {
  185.             shouldPreventKey = true;
  186.             if (statusListener != null)
  187.                 statusListener.onStatusChange(true);
  188.         }
  189.         @Override
  190.         public void onAnimationEnd(Animation animation) {
  191.             shouldPreventKey = false;
  192.             if (statusListener != null)
  193.                 statusListener.onStatusChange(false);
  194.         }
  195.         @Override
  196.         public void onAnimationRepeat(Animation animation) {
  197.         }
  198.     };
  199.     /**
  200.      * Pass the view and let the system auto handle life cycle
  201.      * @param view just the view you want (Location and width will be use for background)
  202.      * @param resize
  203.      */
  204.     public void autoHandle(View view, boolean resize) {
  205.         autoHandle(view, 0, 0, 0, 0, resize);
  206.     }
  207.     /**
  208.      * Pass the view here, background will create base on view location and size
  209.      * Since the background inside this layout only recongize the location base on it parent (0, 0) is top left
  210.      * So user should pass the offSet if using multi recyclerView with one AnimatedLayout
  211.      * Or want to change the background size (Smaller or bigger the focused View)
  212.      * @param view just the view you want (Location and width will be use for background)
  213.      * @param offSetX the X position base on view's windows location
  214.      * @param offSetY the Y position base on view's windows location
  215.      * @param offSetWidth additional witdh base on view's witdh
  216.      * @param offSetHeight additional height base on view's height
  217.      * @param resize true if want to resize the background while animating
  218.      */
  219.     public void autoHandle(View view, float offSetX, float offSetY, int offSetWidth, int offSetHeight, boolean resize) {
  220.         autoHandle(view.getLeft() + offSetX, view.getTop() + offSetY, view.getWidth() + offSetWidth, view.getHeight() + offSetHeight, resize);
  221.     }
  222.     /**
  223.      * Handle the background life cycle, create if not exist, move if exist at the given location
  224.      * @param x X position of background want to create/move
  225.      * @param y Y position of background want to create/move
  226.      * @param width Width of background want to create/resize
  227.      * @param height Height of background want to create/resize
  228.      * @param resize true if want to resize the background while animating
  229.      */
  230.     public void autoHandle(float x, float y, int width, int height, boolean resize) {
  231.         if (scrolling) {
  232.             scrolling = false;
  233.             return;
  234.         }
  235.         if (background == null || background.getVisibility() == GONE) {
  236.             makeBackground(x, y, width, height);
  237.         } else {
  238.             if (resize) {
  239.                 animateBackground(x, y, width, height);
  240.             } else {
  241.                 moveBackground(x, y);
  242.             }
  243.         }
  244.     }
  245.     @Override
  246.     public void onUpdate(float value) {
  247. //        if (value >= 0.9 && shouldPreventKey)
  248. //            shouldPreventKey = false;
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement