Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AnimatedLayout extends FrameLayout implements AnimationUpdateListener {
- private static final int DEFAULT_RES_ID = -1;
- private static final long DEFAULT_ANIMATION_DURATION = 200;
- /*
- Settings here
- */
- private View background;
- private TransformAnimation backgroundAnimation;
- private TranslateAnimation translateAnimation;
- private boolean shouldPreventKey, scrolling;
- private long duration = DEFAULT_ANIMATION_DURATION;
- private int backgroundResId = -1;
- private StatusListener statusListener;
- public AnimatedLayout(Context context) {
- super(context);
- init(context);
- }
- public AnimatedLayout(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- getStyle(context, attrs);
- init(context);
- }
- public AnimatedLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- getStyle(context, attrs);
- init(context);
- }
- private void getStyle(Context context, AttributeSet attrs) {
- if (attrs == null)
- return;
- TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AnimatedLayout, 0, 0);
- try {
- backgroundResId = ta.getResourceId(R.styleable.AnimatedLayout_background_item, DEFAULT_RES_ID);
- duration = (long) ta.getFloat(R.styleable.AnimatedLayout_animation_duration, DEFAULT_ANIMATION_DURATION);
- } finally {
- ta.recycle();
- }
- }
- /**
- * Make recyclerView match the size of layout -> Just set the size of TVRecycler in xml
- */
- private void init(Context context) {
- }
- /**
- * Create background from resourceId with location (in parent) and size
- * @param x coordinate in parent
- * @param y coordinate in parent
- * @param width
- * @param height
- */
- public boolean makeBackground(float x, float y, int width, int height) {
- if (backgroundResId == -1) {
- shouldPreventKey = false;
- if (statusListener != null)
- statusListener.onStatusChange(false);
- return false;
- }
- if (background == null) {
- background = new View(getContext());
- addView(background);
- } else {
- background.setVisibility(VISIBLE);
- }
- background.setBackgroundResource(backgroundResId);
- background.getLayoutParams().width = width;
- background.getLayoutParams().height = height;
- background.setX(x);
- background.setY(y);
- background.setZ(-1);
- backgroundAnimation = new TransformAnimation(background);
- backgroundAnimation
- .translate(x, y)
- .resize(width, height)
- .withDuration(duration)
- .withListener(animationListener);
- translateAnimation = new TranslateAnimation(background);
- translateAnimation
- .translate(x, y, x, y)
- .withDuration(duration)
- .withListener(animationListener).withUpdateListener(this);
- return true;
- }
- public boolean isShowing() {
- return background != null && background.getVisibility() == VISIBLE;
- }
- /**
- * Animate background to x, y and resize background to width and height from last x,y and width, height
- * @param x coordinate in parent
- * @param y coordinate in parent
- * @param width final width background
- * @param height final height background
- */
- public void animateBackground(float x, float y, int width, int height) {
- if (background != null) {
- backgroundAnimation.translate(x, y).resize(width, height);
- background.startAnimation(backgroundAnimation);
- }
- }
- /**
- * Move background (Background must created)
- * @param fromX coordinate in parent
- * @param fromY coordinate in parent
- * @param toX coordinate in parent
- * @param toY coordinate in parent
- */
- public void moveBackground(float fromX, float fromY, float toX, float toY) {
- if (background != null) {
- translateAnimation = new TranslateAnimation(background)
- .translate(fromX, fromY, toX, toY)
- .withDuration(duration)
- .withListener(animationListener)
- .withUpdateListener(this);
- background.startAnimation(translateAnimation);
- }
- }
- /**
- * Move background (Background must created)
- * @param toX coordinate in parent
- * @param toY coordinate in parent
- */
- public void moveBackground(float toX, float toY) {
- if (background != null) {
- try {
- translateAnimation.cancel();
- translateAnimation.translate(toX, toY);
- background.startAnimation(translateAnimation);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * Animate background size (Background must created)
- * @param fromWidth start width of background
- * @param fromHeight start height of background
- * @param toWidth end width of background
- * @param toHeight end height of background
- */
- public void resizeBackground(int fromWidth, int fromHeight, int toWidth, int toHeight) {
- if (background != null) {
- ResizeAnimation animation = new ResizeAnimation(background)
- .resize(fromWidth, fromHeight, toWidth, toHeight);
- background.startAnimation(animation);
- }
- }
- /**
- * Remove background
- */
- public void removeBackground(boolean scrolling) {
- this.scrolling = scrolling;
- if (background != null)
- background.setVisibility(GONE);
- }
- /*
- Method get/set herre
- */
- /**
- * Set background animation duration
- * @param duration
- * @return return this for chain
- */
- public AnimatedLayout setAnimationDuration(long duration) {
- this.duration = duration;
- return this;
- }
- /**
- * Set background resId to render
- * @param backgroundResId id of drawable/layout
- * @return return this for chain
- */
- public AnimatedLayout setBackgroundResId(int backgroundResId) {
- this.backgroundResId = backgroundResId;
- return this;
- }
- public boolean shouldPreventKey() {
- return shouldPreventKey;
- }
- public AnimatedLayout setStatusListener(StatusListener statusListener) {
- this.statusListener = statusListener;
- return this;
- }
- private Animation.AnimationListener animationListener = new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- shouldPreventKey = true;
- if (statusListener != null)
- statusListener.onStatusChange(true);
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- shouldPreventKey = false;
- if (statusListener != null)
- statusListener.onStatusChange(false);
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- };
- /**
- * Pass the view and let the system auto handle life cycle
- * @param view just the view you want (Location and width will be use for background)
- * @param resize
- */
- public void autoHandle(View view, boolean resize) {
- autoHandle(view, 0, 0, 0, 0, resize);
- }
- /**
- * Pass the view here, background will create base on view location and size
- * Since the background inside this layout only recongize the location base on it parent (0, 0) is top left
- * So user should pass the offSet if using multi recyclerView with one AnimatedLayout
- * Or want to change the background size (Smaller or bigger the focused View)
- * @param view just the view you want (Location and width will be use for background)
- * @param offSetX the X position base on view's windows location
- * @param offSetY the Y position base on view's windows location
- * @param offSetWidth additional witdh base on view's witdh
- * @param offSetHeight additional height base on view's height
- * @param resize true if want to resize the background while animating
- */
- public void autoHandle(View view, float offSetX, float offSetY, int offSetWidth, int offSetHeight, boolean resize) {
- autoHandle(view.getLeft() + offSetX, view.getTop() + offSetY, view.getWidth() + offSetWidth, view.getHeight() + offSetHeight, resize);
- }
- /**
- * Handle the background life cycle, create if not exist, move if exist at the given location
- * @param x X position of background want to create/move
- * @param y Y position of background want to create/move
- * @param width Width of background want to create/resize
- * @param height Height of background want to create/resize
- * @param resize true if want to resize the background while animating
- */
- public void autoHandle(float x, float y, int width, int height, boolean resize) {
- if (scrolling) {
- scrolling = false;
- return;
- }
- if (background == null || background.getVisibility() == GONE) {
- makeBackground(x, y, width, height);
- } else {
- if (resize) {
- animateBackground(x, y, width, height);
- } else {
- moveBackground(x, y);
- }
- }
- }
- @Override
- public void onUpdate(float value) {
- // if (value >= 0.9 && shouldPreventKey)
- // shouldPreventKey = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement