package cativity.cyq.layoutanimation; import android.support.v7.app.ActionBarActivity; import android.support.v4.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; import android.view.animation.LayoutAnimationController; import android.view.animation.ScaleAnimation; import android.widget.LinearLayout; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().addToBackStack(null) .add(R.id.container, new PlaceholderFragment()) .commit(); } } public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.fragment_main, container, false); ScaleAnimation sAnimatio = new ScaleAnimation(0, 1, 0, 1); sAnimatio.setDuration(200); //使用布局动画管理器 LayoutAnimationController layoutAniController = new LayoutAnimationController(sAnimatio, 0.9f); rootView.setLayoutAnimation(layoutAniController); rootView.findViewById(R.id.skipto).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager().beginTransaction().addToBackStack(null) .replace(R.id.container, new MyFragment()) .commit(); } }); return rootView; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
时间: 2024-11-08 23:39:32