平滑的动画崩工具栏与Android设计支持库 [英] Smooth animated Collapsing Toolbar with Android Design Support Library
问题描述
是否有反正让Android设计支持库的折叠动画更加平滑滚动时?当我释放滚动,它突然停止。但我要的是:倒塌的动画将继续平稳,即使你停止滚动。 Android的ObservableScrollView 和滚动是被倒塌顺利的库。
Are there anyway to make Android Design Support Library's Collapsing animation smoother while scrolling? When I release scrolling, it stops suddenly. But what I want is: collapsing animation will continue smoothly even if you stop scrolling. Android-ObservableScrollView and Scrollable are the libraries that are collapsing smoothly.
推荐答案
我的工作在这个问题上也是一样,纷纷拿出可能不是很优化的解决方案,但你可以提高it.Once我改进我一定会到编辑答案在看看这个。
I am working on this problem too and have come up with may not very optimized solution but you can improve it.Once I improve it I will definitely edit answer till the have a look at this.
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
private static final String TAG = "app_AppBarStateChange";
public enum State {
EXPANDED,
COLLAPSED,
IDLE
}
private State mCurrentState = State.IDLE;
private int mInitialPosition = 0;
private boolean mWasExpanded;
private boolean isAnimating;
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i == 0) {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED);
}
mCurrentState = State.EXPANDED;
mInitialPosition = 0;
mWasExpanded = true;
Log.d(TAG, "onOffsetChanged 1");
isAnimating = false;
appBarLayout.setEnabled(true);
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED);
}
mCurrentState = State.COLLAPSED;
mInitialPosition = appBarLayout.getTotalScrollRange();
mWasExpanded = false;
Log.d(TAG, "onOffsetChanged 2");
isAnimating = false;
appBarLayout.setEnabled(true);
} else {
Log.d(TAG, "onOffsetChanged 3");
int diff = Math.abs(Math.abs(i) - mInitialPosition);
if(diff >= appBarLayout.getTotalScrollRange()/3 && !isAnimating) {
Log.d(TAG, "onOffsetChanged 4");
isAnimating = true;
appBarLayout.setEnabled(false);
appBarLayout.setExpanded(!mWasExpanded,true);
}
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE);
}
mCurrentState = State.IDLE;
}
}
public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
public State getCurrentState() {
return mCurrentState;
}
}
创建这个类,并调用以下code
Create this class and call following code
private AppBarStateChangeListener mAppBarStateChangeListener = new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
Log.d(TAG, "ToBeDeletedActivity.onStateChanged :: " + state);
if(state == State.EXPANDED || state == State.IDLE) {
getSupportActionBar().setTitle("");
} else {
getSupportActionBar().setTitle("Hello World");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mAppBarLayout.setElevation(0);
}
}
}
};
mAppBarLayout.addOnOffsetChangedListener(mAppBarStateChangeListener);
注意,不要设置annonymous类OffsetChangedListener因为这是保持弱参考,并将于GC.I收集找到我的硬盘的方式自我。
Note that do not set annonymous class OffsetChangedListener as this is kept as weak reference and will be collected by GC.I found my self in hard way.
请探索这个code和改善它(任何人),并重新股吧.Thanks
Kindly explore this code and improve it(anyone) and re-share it .Thanks
这篇关于平滑的动画崩工具栏与Android设计支持库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!