2012-06-08 04:48:29 +00:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2012-06-13 05:47:52 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.ArrayList;
|
2012-06-08 04:48:29 +00:00
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.view.animation.Interpolator;
|
|
|
|
import android.view.animation.DecelerateInterpolator;
|
|
|
|
import android.view.View;
|
2012-06-13 05:47:52 +00:00
|
|
|
import android.view.ViewGroup;
|
2012-06-08 04:48:29 +00:00
|
|
|
|
|
|
|
public class PropertyAnimator extends TimerTask {
|
|
|
|
private static final String LOGTAG = "GeckoPropertyAnimator";
|
|
|
|
|
|
|
|
private Timer mTimer;
|
|
|
|
private Interpolator mInterpolator;
|
|
|
|
|
|
|
|
public static enum Property {
|
2012-06-13 05:47:52 +00:00
|
|
|
SHRINK_LEFT,
|
|
|
|
SHRINK_TOP,
|
|
|
|
SLIDE_TOP,
|
|
|
|
SLIDE_LEFT
|
2012-06-08 04:48:29 +00:00
|
|
|
}
|
|
|
|
|
2012-06-13 05:47:52 +00:00
|
|
|
private class ElementHolder {
|
|
|
|
View view;
|
|
|
|
Property property;
|
|
|
|
int from;
|
|
|
|
int to;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static interface PropertyAnimationListener {
|
|
|
|
public void onPropertyAnimationStart();
|
|
|
|
public void onPropertyAnimationEnd();
|
|
|
|
}
|
2012-06-08 04:48:29 +00:00
|
|
|
|
|
|
|
private int mCount;
|
2012-06-13 05:47:52 +00:00
|
|
|
private int mDuration;
|
|
|
|
private List<ElementHolder> mElementsList;
|
|
|
|
private PropertyAnimationListener mListener;
|
2012-06-08 04:48:29 +00:00
|
|
|
|
|
|
|
// Default refresh rate in ms.
|
2012-06-13 05:47:52 +00:00
|
|
|
private static final int INTERVAL = 10;
|
2012-06-08 04:48:29 +00:00
|
|
|
|
2012-06-13 05:47:52 +00:00
|
|
|
public PropertyAnimator(int duration) {
|
2012-06-08 04:48:29 +00:00
|
|
|
mDuration = duration;
|
|
|
|
mTimer = new Timer();
|
|
|
|
mInterpolator = new DecelerateInterpolator();
|
2012-06-13 05:47:52 +00:00
|
|
|
mElementsList = new ArrayList<ElementHolder>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void attach(View view, Property property, int to) {
|
|
|
|
ElementHolder element = new ElementHolder();
|
|
|
|
|
|
|
|
element.view = view;
|
|
|
|
element.property = property;
|
|
|
|
|
|
|
|
// Sliding should happen in the negative.
|
|
|
|
if (property == Property.SLIDE_TOP || property == Property.SLIDE_LEFT)
|
|
|
|
element.to = to * -1;
|
|
|
|
else
|
|
|
|
element.to = to;
|
|
|
|
|
|
|
|
mElementsList.add(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPropertyAnimationListener(PropertyAnimationListener listener) {
|
|
|
|
mListener = listener;
|
2012-06-08 04:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2012-06-13 05:47:52 +00:00
|
|
|
float interpolation = mInterpolator.getInterpolation((float) (mCount * INTERVAL) / (float) mDuration);
|
2012-06-08 04:48:29 +00:00
|
|
|
|
2012-06-13 05:47:52 +00:00
|
|
|
for (ElementHolder element : mElementsList) {
|
|
|
|
int delta = element.from + (int) ((element.to - element.from) * interpolation);
|
|
|
|
invalidate(element, delta);
|
|
|
|
}
|
2012-06-08 04:48:29 +00:00
|
|
|
|
|
|
|
mCount++;
|
|
|
|
|
2012-06-13 05:47:52 +00:00
|
|
|
if (mCount * INTERVAL >= mDuration)
|
2012-06-08 04:48:29 +00:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void start() {
|
|
|
|
mCount = 0;
|
2012-06-13 05:47:52 +00:00
|
|
|
|
|
|
|
// Fix the from value based on current position and property
|
|
|
|
for (ElementHolder element : mElementsList) {
|
|
|
|
ViewGroup.MarginLayoutParams params = ((ViewGroup.MarginLayoutParams) element.view.getLayoutParams());
|
|
|
|
|
|
|
|
if (element.property == Property.SHRINK_TOP)
|
|
|
|
element.from = params.topMargin;
|
|
|
|
else if (element.property == Property.SHRINK_LEFT)
|
|
|
|
element.from = params.leftMargin;
|
|
|
|
else if (element.property == Property.SLIDE_TOP)
|
|
|
|
element.from = element.view.getScrollY();
|
|
|
|
else if (element.property == Property.SLIDE_LEFT)
|
|
|
|
element.from = element.view.getScrollX();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDuration != 0) {
|
|
|
|
mTimer.scheduleAtFixedRate(this, 0, INTERVAL);
|
|
|
|
|
|
|
|
if (mListener != null)
|
|
|
|
mListener.onPropertyAnimationStart();
|
|
|
|
}
|
2012-06-08 04:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
cancel();
|
|
|
|
mTimer.cancel();
|
|
|
|
mTimer.purge();
|
|
|
|
|
|
|
|
// Make sure to snap to the end position.
|
2012-06-13 05:47:52 +00:00
|
|
|
for (ElementHolder element : mElementsList) {
|
|
|
|
invalidate(element, element.to);
|
|
|
|
}
|
|
|
|
|
|
|
|
mElementsList.clear();
|
|
|
|
|
|
|
|
if (mListener != null) {
|
|
|
|
mListener.onPropertyAnimationEnd();
|
|
|
|
mListener = null;
|
|
|
|
}
|
2012-06-08 04:48:29 +00:00
|
|
|
}
|
|
|
|
|
2012-06-13 05:47:52 +00:00
|
|
|
private void invalidate(final ElementHolder element, final int delta) {
|
2012-06-08 04:48:29 +00:00
|
|
|
// Post the layout changes on the view's UI thread.
|
2012-06-13 05:47:52 +00:00
|
|
|
element.view.getHandler().post(new Runnable() {
|
2012-06-08 04:48:29 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2012-06-13 05:47:52 +00:00
|
|
|
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) element.view.getLayoutParams();
|
|
|
|
|
|
|
|
if (element.property == Property.SHRINK_TOP)
|
|
|
|
params.setMargins(params.leftMargin, delta, params.rightMargin, params.bottomMargin);
|
|
|
|
else if (element.property == Property.SHRINK_LEFT)
|
|
|
|
params.setMargins(delta, params.topMargin, params.rightMargin, params.bottomMargin);
|
|
|
|
else if (element.property == Property.SLIDE_TOP)
|
|
|
|
element.view.scrollTo(element.view.getScrollX(), delta);
|
|
|
|
else if (element.property == Property.SLIDE_LEFT)
|
|
|
|
element.view.scrollTo(delta, element.view.getScrollY());
|
|
|
|
|
|
|
|
element.view.requestLayout();
|
2012-06-08 04:48:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|