FragmentCustomAnimation实现Fragment的界面切换

1.知识点:FragmentCustomAnimation

2.演示样例:来自于官网演示样例的简化,这样更方便于学习该知识点。

   本演示样例的源代码下载地址为:http://download.csdn.net/detail/far_sight/7932287

3.项目FragmentCustomAnimationTest1效果:反复点buttonnew fragment,第点一次,数字加一,实现原因是第点一次加了一个新的Fragment在栈中。 当点返回键时,数字会降低,原因是Fragment在出栈。当最后一个出栈后再点返回键,程序退出。

两个在layout中的xml文件fragment_stack.xml与hello_world.xml

fragment_stack.xml文件内容为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="4dip" >

    <LinearLayout
        android:id="@+id/simple_fragment"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="#ffff0000" >
    </LinearLayout>
    <Button
        android:id="@+id/new_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="new fragment" >
        <requestFocus />
    </Button>

</LinearLayout>


hello_world.xml文件的内容为:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:text=""
    android:textSize="40sp" />


类FragmentCustomAnimations的内容为:

package com.fs.act;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FragmentCustomAnimations extends Activity {
	private int mStackLevel = 100;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_stack);
		Button button = (Button) findViewById(R.id.new_fragment);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				addFragmentToStack();
			}
		});
	}
	void addFragmentToStack() {
		Fragment newFragment = CountingFragment.newInstance(++mStackLevel);
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(R.id.simple_fragment, newFragment);
		ft.addToBackStack(null);
		ft.commit();
	}

	public static class CountingFragment extends Fragment {
		static CountingFragment newInstance(int num) {
			CountingFragment f = new CountingFragment();
			Bundle args = new Bundle();
			args.putInt("num", num);
			f.setArguments(args);
			return f;
		}
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.hello_world, container, false);
			View tv = v.findViewById(R.id.text);
			((TextView) tv).setText("Fragment #"+ this.getArguments().getInt("num"));
			return v;
		}
	}

}
<img src="http://img.blog.csdn.net/20140917172541900?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ2d1YW5ncm9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

4.项目FragmentCustomAnimationTest2加了界面切换时滑动效果。下官是在栈中加Fragment还是按返回键让Fragment从栈中出栈,都有滑动效果。其他与项目

FragmentCustomAnimationTest1同样。

    (1) 在上一项目基础上在res以下添加�目录animator,里面加下四个文件

 fragment_slide_left_enter.xml

fragment_slide_left_exit.xml

fragment_slide_right_enter.xml

fragment_slide_right_exit.xml

  (2)FragmentCustomAnimations.java的内容改为

package com.fs.act;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FragmentCustomAnimations extends Activity {
	private int mStackLevel = 100;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_stack);
		Button button = (Button) findViewById(R.id.new_fragment);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				addFragmentToStack();
			}
		});
	}

	@SuppressLint("NewApi")
	void addFragmentToStack() {
		Fragment newFragment = CountingFragment.newInstance(++mStackLevel);
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
				R.animator.fragment_slide_left_exit,
				R.animator.fragment_slide_right_enter,
				R.animator.fragment_slide_right_exit);
		ft.replace(R.id.simple_fragment, newFragment);
		ft.addToBackStack(null);
		ft.commit();
	}

	public static class CountingFragment extends Fragment {
		static CountingFragment newInstance(int num) {
			CountingFragment f = new CountingFragment();
			Bundle args = new Bundle();
			args.putInt("num", num);
			f.setArguments(args);
			return f;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.hello_world, container, false);
			View tv = v.findViewById(R.id.text);
			((TextView) tv).setText("Fragment #"
					+ this.getArguments().getInt("num"));
			return v;
		}
	}

}


原文地址:https://www.cnblogs.com/hrhguanli/p/4007287.html