您的位置:首页 > 其它

Tween动画(四种)以及Activity间跳转动画

2012-11-29 15:46 344 查看
main.xml如下:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="60dip"
        android:layout_height="90dip"
        android:scaleType="fitXY"
        android:layout_marginTop="80dip"
        android:src="@drawable/haha"
        android:layout_centerHorizontal="true"
     />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/imageView"
        android:layout_marginTop="35dip"
    >
        <Button 
            android:id="@+id/alphaButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="alpha"       
        />
         <Button 
            android:id="@+id/scaleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="scale"
            android:layout_marginLeft="15dip"
        />
          <Button 
            android:id="@+id/rotateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="rotate"
            android:layout_marginLeft="15dip"
        />
           <Button 
            android:id="@+id/translateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="translate"
            android:layout_marginLeft="15dip"
        />
    </LinearLayout>
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="270dip"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

    <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Jump To 1"
        android:layout_below="@id/textView"
        android:layout_marginTop="20dip"
        android:layout_alignParentLeft="true"
       
    />
    
     <Button 
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Jump To 2"
        android:layout_below="@id/textView"
        android:layout_marginTop="20dip"
        android:layout_alignParentRight="true"
       
    />
</RelativeLayout>


secondActivity布局如下:

<?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:orientation="horizontal" >
    <TextView 
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:text="The second Activity"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="120dip"
     />

</LinearLayout>


mainActivity如下:

package com.example.testactivityjumpanimation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
    private Button button1;
    private Button button2;
    private Button alphaButton;
    private Button scaleButton;
    private Button rotateButton;
    private Button translateButton;
    private Animation animation;
    private ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Activity跳转间动画1
        button1=(Button) findViewById(R.id.button1);
        button1.setOnClickListener(new ButtonClickListener());
        //Activity跳转间动画1
        button2=(Button) findViewById(R.id.button2);
        button2.setOnClickListener(new ButtonClickListener());
        //展示动画的ImageView
        imageView=(ImageView) findViewById(R.id.imageView);
        //测试四个动画的按钮
        alphaButton=(Button) findViewById(R.id.alphaButton);
        scaleButton=(Button) findViewById(R.id.scaleButton);
        rotateButton=(Button) findViewById(R.id.rotateButton);
        translateButton=(Button) findViewById(R.id.translateButton);
        
        alphaButton.setOnClickListener(new ButtonClickListener());
        scaleButton.setOnClickListener(new ButtonClickListener());
        rotateButton.setOnClickListener(new ButtonClickListener());
        translateButton.setOnClickListener(new ButtonClickListener());   
    }
 
    private class ButtonClickListener implements OnClickListener{
		public void onClick(View v) {
			switch (v.getId()) {
			//Activity之间跳转的动画1
			case R.id.button1:
				Intent intent1=new Intent(MainActivity.this, SecondActicity.class);
				startActivity(intent1);
				//注意参数的解释:
				//第一个参数 enterAnim
				//A resource ID of the animation resource to use for the incoming activity.
				//Use 0 for no animation.
				//第二个参数 exitAnim
				//A resource ID of the animation resource to use for the outgoing activity. 
				//Use 0 for no animation.
				//注意该方法应该紧挨着startActivity()或者finish()后调用
				overridePendingTransition(R.anim.in1, R.anim.out1);
				break;
			case R.id.button2:
				Intent intent2=new Intent(MainActivity.this, SecondActicity.class);
				startActivity(intent2);
				overridePendingTransition(R.anim.in2, R.anim.out2);
				break;
			//透明度动画
			case R.id.alphaButton:
				animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alphaanimation);
				imageView.startAnimation(animation);
				break;
			//大小变化动画
           case R.id.scaleButton:
        	   animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanimation);
        	   imageView.startAnimation(animation);
				break;
			//旋转动画
           case R.id.rotateButton:
        	   animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotateanimation);
        	   imageView.startAnimation(animation);
				break;
			//位移动画
           case R.id.translateButton:
        	   animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.translateanimation);
        	   imageView.startAnimation(animation);
				break;			
			default:
				break;
			}						
		}    	
    }
}


secondActivity如下:

package com.example.testactivityjumpanimation;
import android.app.Activity;
import android.os.Bundle;
public class SecondActicity extends Activity {
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.second);
	       
	    }
}


以下为动画xml文件

alphaanimation.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- Alpha的值界于0.0与1.0之间 -->
<!-- Alpha=0的时候不可见 -->
<!-- Alpha=1的时候完全可见 -->
<!-- repeatCount设置了动画重复的次数 -->
<!-- repeatCount=1,那么动画一共会执行2次 -->
<!-- repeatCount的意思是除了原本的执行以后还要执行几次 -->
<!-- android:repeatMode有两种,测试一下即明白 -->

<!-- 
android:fillAfter和android:fillBefore只能在set中设置!!!
android:fillAfter="true"
表示动画结束后,当前画面就为动画结束后的效果
android:fillBefore="true"
表示动画结束后,当前画面就为动画开始前的效果
-->
<!--
 android:startOffset也是在set中设置才有效果
-->
<!--   android:interpolator用于控制动画执行过程中的速度 -->

<set 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillBefore="true"
    >
    <alpha 
        android:fromAlpha="1.0"
        android:toAlpha="0.1"
        android:duration="10000"
        android:repeatCount="1" 
    />
</set>


scaleanimation.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- fromX(Y)Scale和toX(Y)Scale的值: -->
<!-- 0.0表示收缩到没有 -->
<!-- 1.0表示正常无收缩 -->
<!-- 值小于1.0表示收缩 -->
<!-- 值大于1.0表示放大 -->

<!-- 注意: -->
<!-- 应该同时设置X和Y的from和to,否则无效果-->

<!-- pivot本意是枢轴,中心点的意思 -->
<!-- pivotX(Y)表示动画相对于物件的X,Y坐标的开始位置 -->
<!-- pivotX(Y)取值范围为0%到100% -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale 
        android:fromXScale="0.4"
        android:toXScale="2.0"
        android:fromYScale="0.4"
        android:toYScale="2.0"
        android:pivotX="100%"
        android:pivotY="50%"
        android:duration="3000" 
     />
</set>


rotateanimation.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- android:fromDegrees表示动画开始时相对于原物件的角度 -->
<!-- android:toDegrees表示动画结束时相对于原物件旋转的角度-->
<!-- 此处使用到的角度:正数表示顺时针旋转 -->
<!-- 此处使用到的角度:负数表示逆时针旋转 -->

<!-- 以下说法不准确 -->
<!-- from为负数,to为正数,顺时针旋转 -->
<!-- from为负数,to为负数,逆时针旋转 -->
<!-- from为正数,to为正数,顺时针旋转 -->
<!-- from为正数,to为负数,逆时针旋转 -->

<!-- 应该是to减去from=结果 -->
<!-- 结果大于0,顺时针旋转 -->
<!-- 结果小于0,逆时针旋转 -->
<!-- 和前面描述from和to是一样的: -->
<!-- 正数表示顺时针旋转 -->
<!-- 负数表示逆时针旋转 -->

<!-- 更准确应该这么理解 -->
<!-- 应该是from减去to=结果 -->
<!-- 结果大于0,逆时针旋转 -->
<!-- 结果小于0,正时针旋转 -->
<!-- 这样符合一贯的理解:顺时针为负,逆时针为正 -->

<!-- pivot本意是枢轴,中心点的意思 -->
<!-- pivotX(Y)表示动画开始的时候相对于原来物件的X,Y坐标-->
<!-- pivotX(Y)取值范围为0%到100% -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <rotate 
       android:fromDegrees="90"
       android:toDegrees="-90"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="4000" 
   /> 
</set>


translateanimation.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 
    fromXDelta  表示动画开始的点的X离当前View X坐标上的差值
  toXDelta    表示动画结束的点的X离当前View X坐标上的差值
  fromYDelta  表示动画开始的点的Y离当前View Y坐标上的差值
  toYDelta    表示动画开始的点的Y离当前View Y坐标上的差值
 -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="100"
        android:toXDelta="0"
        android:fromYDelta="30"
        android:toYDelta="0"
        android:duration="3000"     
    />
</set>



in1.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 作用于即将出现的Activity -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator" >

    <scale
        android:duration="3000"
        android:fromXScale="2.0"
        android:toXScale="1.0"
        android:fromYScale="2.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        />

</set>


out1.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 作用于即将消失的Activity -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:zAdjustment="top" >

    <scale
        android:duration="5000"
        android:fromXScale="1.0"
        android:toXScale="0.5"
        android:fromYScale="1.0"
        android:toYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"
        />

    <alpha
        android:duration="5000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>


in2.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="320"
        android:toXDelta="0"
        android:duration="300"
    />
</set>


out2.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 
设置android:fromXDelta="0"且android:toXDelta="0"作用:
防止在Activity页面跳转的时候,出现黑屏.
且注意:out2中的android:duration和in2中的保持一致
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="0"
        android:duration="300"
    />
</set>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: