Crossfading Two Views

来源:互联网 发布:成都编程教育 编辑:程序博客网 时间:2024/05/02 14:37

//实现淡入淡出


import android.animation.Animator;

import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;


public class MainActivity extends Activity {
private View mContentView;
private View mLoadingView;
private int mShortAnimationDuration;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentView = findViewById(R.id.content);
mLoadingView = findViewById(R.id.loading_spinner);


// Initially hide the content view.
mContentView.setVisibility(View.GONE);
crossfade();


// Retrieve and cache the system's default "short" animation time.
mShortAnimationDuration = getResources().getInteger(
android.R.integer.config_shortAnimTime);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}


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(mShortAnimationDuration)
.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);
}
});
}


/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {


public PlaceholderFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}


}
0 0