Android基础:Activity转场动画:overridePendingTransition(...)

XML创建补间动画

translate

有4个Activity,分别是ActivityAActivityB  ActivityC    ActivityD,

从C-D之间是不加动画的效果,我们可以看到页面是瞬间完成,覆盖掉当前的页面。

从A-B-C这之间是使用了平移动画,我们可以看到页面是缓慢的移动(时间设为500MS),最后覆盖掉当前的页面。

效果图:

加动画   无动画

Android基础:Activity转场动画:overridePendingTransition(...)_第1张图片Android基础:Activity转场动画:overridePendingTransition(...)_第2张图片

步骤:

1 创建4个activity,4个layout,

2 在res目录下创建aim目录,放动画xml

分4种:  当前页向左移动  ,当前页向右移动,左边页向右移动,右边页向左移动。

        对应4种动画:self_to_left.xml  self_to_right.xml   left_other_to_right.xml    right_other_to_left.xml

self_to_left.xml:




self_to_right.xml :




 left_other_to_right.xml: 




right_other_to_left.xml:




ActivityA.java 从A到B

public class ActivityA extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        
        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActivityA.this, ActivityB.class);
				startActivity(intent);
				finish();
				//enterAnim进入动画              exitAnim退出动画,确保动画的完成
				overridePendingTransition(R.anim.right_other_to_left, R.anim.self_to_left);
			}
		});
    }
}

ActivityB.java 从B到C,从B到A.

public class ActivityB extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        
        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActivityB.this, ActivityA.class);
				startActivity(intent);
				finish();
				//enterAnim进入动画              exitAnim退出动画
				overridePendingTransition(R.anim.left_other_to_right, R.anim.self_to_right);
			}
		});
        
        Button btn2 = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActivityB.this, ActivityC.class);
				startActivity(intent);
				finish();
				//enterAnim进入动画              exitAnim退出动画
				overridePendingTransition(R.anim.right_other_to_left, R.anim.self_to_left);
			}
		});
    }   
}

ActivityC.java 从C到B(有动画), 从C到D(无动画).

public class ActivityC extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_c);

		Button btn1 = (Button) findViewById(R.id.btn1);
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActivityC.this, ActivityB.class);
				startActivity(intent);
				finish();
				// enterAnim进入动画 exitAnim退出动画
				overridePendingTransition(R.anim.left_other_to_right,R.anim.self_to_right);
			}
		});
		
		//正常启动
		 Button btn2 = (Button) findViewById(R.id.btn2);
	        btn2.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(ActivityC.this, ActivityD.class);
					startActivity(intent);
					finish();
				}
			});
	}
}
ActivityD.java 从D到C(无动画)

public class ActivityD extends Activity {
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_d);
        
        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActivityD.this, ActivityC.class);
				startActivity(intent);
				finish();
			}
		});
    }
}

Demo:http://download.csdn.net/detail/ss1168805219/9498731



你可能感兴趣的:(动画)