我正在尝试进行交叉淡入淡出,Android开发者的教程使用“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);
}
这是动画类:
private void crossfade(){
// 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);
}
});
}
答案 0 :(得分:0)
使用android.R.integer.config_shortAnimTime
constant。
int time = getResources().getInteger(android.R.integer.config_shortAnimTime);
还有config_longAnimTime
,config_mediumAnimTime
常量。