我正在尝试进行淡入淡出,Android Developers 上的教程使用“animationDuration”作为动画的持续时间。是否应该检索这个“animationDuration”,以便褪色动画的持续时间取决于处理器的速度?我是android编程的新手,所以像这样简单的事情对我来说仍然很陌生。
这是代码:
公共类 CrossfadeActivity 扩展 Activity {
private View mContentView;
private View mLoadingView;
private int AnimationDuration;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crossfade);
mContentView = findViewById(R.id.content);
mLoadingView = findViewById(R.id.loading_spinner);
// Initially hide the content view.
mContentView.setVisibility(View.GONE);
// Retrieve and cache the system's default "short" animation time.
AnimationDuration = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
这是动画类:
私人无效交叉淡入淡出(){
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
mContentView.setAlpha(0f);
mContentView.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
mContentView.animate()
.alpha(1f)
.setDuration(AnimationDuration)
.setListener(null);
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
mLoadingView.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration) //???
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mLoadingView.setVisibility(View.GONE);
}
});
}