Android切换Activity时的淡入动画和缩小动画

Android切换Activity时的淡入动画和缩小动画

实现在Activity切换过程中的淡入和缩小的动画效果。先创建一个主Activty布局。主Activity有两个按钮一个是实现切换的淡入效果,一个是实现缩小效果。

main.xml布局如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="本程序实现在Activity切换过程中的淡入和放大的动画效果!" /> <Button android:id="@+id/fadein" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Fade in" /> <Button android:id="@+id/zoomin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Zoom in" /> </LinearLayout>

主Activity内容:

package com.xiaochun91103; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { /** Called when the activity is first created. */ private Button fadeIn,zoomOut; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); fadeIn = (Button)findViewById(R.id.fadein); fadeIn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(MainActivity.this,ShowActivity.class)); overridePendingTransition(R.anim.fade, R.anim.hold);//切换Activity的过渡动画 } }); zoomOut = (Button)findViewById(R.id.zoomout); zoomOut.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(MainActivity.this,ShowActivity.class)); overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit); } }); } }

动画效果的xml如下,fade.xml:

<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="@android:integer/config_longAnimTime" />

 

hold.xml:

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromXDelta="0" android:toXDelta="0" android:duration="@android:integer/config_longAnimTime" />

 

zoom_enter.xml:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> <scale android:fromXScale="2.0" android:toXScale="1.0" android:fromYScale="2.0" android:toYScale="1.0" android:pivotX="50%p" android:pivotY="50%p" android:duration="@android:integer/config_mediumAnimTime" /> </set>

 

zoom_exit.xml:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:zAdjustment="top"> <scale android:fromXScale="1.0" android:toXScale=".5" android:fromYScale="1.0" android:toYScale=".5" android:pivotX="50%p" android:pivotY="50%p" android:duration="@android:integer/config_mediumAnimTime" /> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="@android:integer/config_mediumAnimTime"/> </set>

 

另外showActivity仅仅做一个界面不做任何操作。所以用户可以随便编一个界面。用来在Activity的切换中显示过渡的动画。

Android切换Activity时的淡入动画和缩小动画_第1张图片

你可能感兴趣的:(android,layout,Class,import,button,encoding)