ViewPropertyAnimator - 动画不会第一次出现
ViewPropertyAnimator - animation wouldn't happen the first time
我将以下代码用于 AppBarLayout
的滑入/滑出切换。
public void showToolbar(boolean show) {
if (appBar == null) {
Log.e(TAG, "showToolbar: Toolbar is null");
return;
}
boolean toolbarShown = Utils.isViewVisible(appBar);
Log.d(TAG, "showToolbar: shown:" +shown);
boolean changed = (show != toolbarShown);
if (changed) {
if (show) {
Log.d(TAG, "showToolbar: showing");
appBar.setVisibility(View.VISIBLE);
appBar.animate()
.translationY(0)
.setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
appBar.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animator animator) { }
@Override
public void onAnimationCancel(Animator animator) { }
@Override
public void onAnimationRepeat(Animator animator) { }
})
.start();
} else {
Log.d(TAG, "showToolbar: hiding");
appBar.animate()
.translationY(-toolbar.getBottom())
.setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) { }
@Override
public void onAnimationEnd(Animator animator) {
appBar.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationCancel(Animator animator) { }
@Override
public void onAnimationRepeat(Animator animator) { }
})
.start();
}
} else {
Log.d(TAG, "showToolbar: no change");
}
}
除了第一次调用 showToolbar(true)
以显示工具栏外,动画效果完美。该视图第一次显示时没有动画。我搜索了该网站并发现了类似的问题,但解决方案似乎对我不起作用。
可能值得注意的是,只有当我们希望首先隐藏 appBar
时才会发生这种情况。我的猜测是动画可能
更新 1:
public static boolean isViewVisible(View view) {
if (View.VISIBLE == view.getVisibility()) return true;
else return false;
}
更新 2
我删除了 isViewWithinScreenBounds()
方法,因为实际上不需要该检查。
试试这个
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:translationY="-120dp"
android:layout_height="120dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SOME TEXT HERE" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Activity代码
@BindView(R.id.app_bar)
AppBarLayout appBar;
@BindView(R.id.toolbar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
animateToolbar(false); //Just for testing purpose - delay execute of function animateToolbar() for 4 seconds
}
}, 4000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
animateToolbar(true);
return super.onCreateOptionsMenu(menu);
}
private void animateToolbar(boolean show) {
if (show) {
// As you can see in the xml layout initial position of the appBar is translated by its height to the top of the screen "-" sign
// Slide int from out of the screen to initial position -> from -120 dp (height of the app bar, see xml) to 0 dp
appBar.animate()
.translationY(0)
.setDuration(1000)
.start();
} else {
// Slide out from initial position to the top of the screen -> from 0 dp to -120 dp (height of the app bar, see xml)
appBar.animate()
.translationY(-appBar.getHeight())
.setDuration(1000)
.start();
}
}
务必设置 visibility
和 translationY
的初始值。
如果您希望工具栏最初隐藏并显示第一个动画,请务必设置 android:visibility="invisible"
和 NOT "gone"
,以及负面 android:translationY
喜欢 -56dp
.
我将以下代码用于 AppBarLayout
的滑入/滑出切换。
public void showToolbar(boolean show) {
if (appBar == null) {
Log.e(TAG, "showToolbar: Toolbar is null");
return;
}
boolean toolbarShown = Utils.isViewVisible(appBar);
Log.d(TAG, "showToolbar: shown:" +shown);
boolean changed = (show != toolbarShown);
if (changed) {
if (show) {
Log.d(TAG, "showToolbar: showing");
appBar.setVisibility(View.VISIBLE);
appBar.animate()
.translationY(0)
.setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
appBar.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animator animator) { }
@Override
public void onAnimationCancel(Animator animator) { }
@Override
public void onAnimationRepeat(Animator animator) { }
})
.start();
} else {
Log.d(TAG, "showToolbar: hiding");
appBar.animate()
.translationY(-toolbar.getBottom())
.setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) { }
@Override
public void onAnimationEnd(Animator animator) {
appBar.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationCancel(Animator animator) { }
@Override
public void onAnimationRepeat(Animator animator) { }
})
.start();
}
} else {
Log.d(TAG, "showToolbar: no change");
}
}
除了第一次调用 showToolbar(true)
以显示工具栏外,动画效果完美。该视图第一次显示时没有动画。我搜索了该网站并发现了类似的问题,但解决方案似乎对我不起作用。
可能值得注意的是,只有当我们希望首先隐藏 appBar
时才会发生这种情况。我的猜测是动画可能
更新 1:
public static boolean isViewVisible(View view) {
if (View.VISIBLE == view.getVisibility()) return true;
else return false;
}
更新 2
我删除了 isViewWithinScreenBounds()
方法,因为实际上不需要该检查。
试试这个
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:translationY="-120dp"
android:layout_height="120dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SOME TEXT HERE" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Activity代码
@BindView(R.id.app_bar)
AppBarLayout appBar;
@BindView(R.id.toolbar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
animateToolbar(false); //Just for testing purpose - delay execute of function animateToolbar() for 4 seconds
}
}, 4000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
animateToolbar(true);
return super.onCreateOptionsMenu(menu);
}
private void animateToolbar(boolean show) {
if (show) {
// As you can see in the xml layout initial position of the appBar is translated by its height to the top of the screen "-" sign
// Slide int from out of the screen to initial position -> from -120 dp (height of the app bar, see xml) to 0 dp
appBar.animate()
.translationY(0)
.setDuration(1000)
.start();
} else {
// Slide out from initial position to the top of the screen -> from 0 dp to -120 dp (height of the app bar, see xml)
appBar.animate()
.translationY(-appBar.getHeight())
.setDuration(1000)
.start();
}
}
务必设置 visibility
和 translationY
的初始值。
如果您希望工具栏最初隐藏并显示第一个动画,请务必设置 android:visibility="invisible"
和 NOT "gone"
,以及负面 android:translationY
喜欢 -56dp
.