Android 动画效果
Android 动画效果
本来以为动画都会了,谁知道今天写代码的时候忘记了,所以把动画总结了一遍,在res目录下新建一个anim文件夹,然后把动画的xml文件写在这个文件夹下面。直接上图上代码
CartoonActivity.java
- package rw.cartoon;
- 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 CartoonActivity extends Activity {
- /** Called when the activity is first created. */
- private Button TraslateButton,RoteButton,ScaleButton,AlphaButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TraslateButton=(Button)findViewById(R.id.translatebutton3);
- RoteButton=(Button)findViewById(R.id.rotatebutton4);
- ScaleButton=(Button)findViewById(R.id.scalebutton2);
- AlphaButton=(Button)findViewById(R.id.alplebutton1);
- TraslateButton.setOnClickListener(new ButtonListener());
- RoteButton.setOnClickListener(new ButtonListener());
- AlphaButton.setOnClickListener(new ButtonListener());
- ScaleButton.setOnClickListener(new ButtonListener());
- }
- private class ButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- switch (v.getId()) {
- case R.id.alplebutton1:
- intent.setClass(getApplicationContext(), alpha.class);
- CartoonActivity.this.startActivity(intent);
- break;
- case R.id.rotatebutton4:
- intent.setClass(getApplicationContext(), rotate.class);
- CartoonActivity.this.startActivity(intent);
- break;
- case R.id.scalebutton2:
- intent.setClass(getApplicationContext(), sacle.class);
- CartoonActivity.this.startActivity(intent);
- break;
- case R.id.translatebutton3:
- intent.setClass(getApplicationContext(), translate.class);
- CartoonActivity.this.startActivity(intent);
- break;
- default:
- break;
- }
- }
- }
- }
alpha.java
- package rw.cartoon;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.ImageView;
- public class alpha extends Activity {
- /** Called when the activity is first created. */
- ImageView imageVie;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test);
- imageVie=(ImageView)findViewById(R.id.imageView1);
- Animation animation=AnimationUtils.loadAnimation(alpha.this, R.anim.alpha);
- imageVie.startAnimation(animation);
- }
- }
rotate.java
- package rw.cartoon;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.ImageView;
- public class rotate extends Activity {
- ImageView imageVie;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test);
- imageVie=(ImageView)findViewById(R.id.imageView1);
- Animation animation=AnimationUtils.loadAnimation(rotate.this, R.anim.rotate);
- imageVie.startAnimation(animation);
- }
- }
scale.java
- package rw.cartoon;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.ImageView;
- public class sacle extends Activity {
- ImageView imageVie;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test);
- imageVie=(ImageView)findViewById(R.id.imageView1);
- Animation animation=AnimationUtils.loadAnimation(sacle.this, R.anim.scale);
- imageVie.startAnimation(animation);
- }
- }
translate.java
- package rw.cartoon;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.ImageView;
- public class translate extends Activity {ImageView imageVie;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test);
- imageVie=(ImageView)findViewById(R.id.imageView1);
- Animation animation=AnimationUtils.loadAnimation(translate.this, R.anim.translate);
- imageVie.startAnimation(animation);
- }}
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="@string/hello"
- />
- <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/alplebutton1" android:text="alpha 渐变透明度"></Button>
- <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/scalebutton2" android:text="scale 渐变尺寸伸缩"></Button>
- <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/translatebutton3" android:text="translate 画面转换位置移动"></Button>
- <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rotatebutton4" android:text="rotate 画面转移旋转动"></Button>
- </LinearLayout>
test.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent" android:weightSum="1">
- <ImageView android:src="@drawable/ttt2" android:id="@+id/imageView1" android:layout_width="218dp" android:layout_height="219dp"></ImageView>
- </LinearLayout>
alpha.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0.1"
- android:duration="3000"
- />
- <!-- 透明度控制动画效果 alpha
- 浮点型值:
- fromAlpha 属性为动画起始时透明度
- toAlpha 属性为动画结束时透明度
- 说明:
- 0.0表示完全透明
- 1.0表示完全不透明
- 以上值取0.0-1.0之间的float数据类型的数字
- 长整型值:
- duration 属性为动画持续时间
- 说明:
- 时间以毫秒为单位
- -->
- </set>
rotate.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <rotate
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromDegrees="0"
- android:toDegrees="+350"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="3000" />
- <!-- rotate 旋转动画效果
- 属性:interpolator 指定一个动画的插入器
- 在我试验过程中,使用android.res.anim中的资源时候发现
- 有三种动画插入器:
- accelerate_decelerate_interpolator 加速-减速 动画插入器
- accelerate_interpolator 加速-动画插入器
- decelerate_interpolator 减速- 动画插入器
- 其他的属于特定的动画效果
- 浮点数型值:
- fromDegrees 属性为动画起始时物件的角度
- toDegrees 属性为动画结束时物件旋转的角度 可以大于360度
- 说明:
- 当角度为负数——表示逆时针旋转
- 当角度为正数——表示顺时针旋转
- (负数from——to正数:顺时针旋转)
- (负数from——to负数:逆时针旋转)
- (正数from——to正数:顺时针旋转)
- (正数from——to负数:逆时针旋转)
- pivotX 属性为动画相对于物件的X坐标的开始位置
- pivotY 属性为动画相对于物件的Y坐标的开始位置
- 说明: 以上两个属性值 从0%-100%中取值
- 50%为物件的X或Y方向坐标上的中点位置
- 长整型值:
- duration 属性为动画持续时间
- 说明: 时间以毫秒为单位
- -->
- </set>
scale.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <scale
- android:interpolator=
- "@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="0.0"
- android:toXScale="1.4"
- android:fromYScale="0.0"
- android:toYScale="1.4"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillAfter="false"
- android:duration="700" />
- </set>
- <!-- 尺寸伸缩动画效果 scale
- 属性:interpolator 指定一个动画的插入器
- 在我试验过程中,使用android.res.anim中的资源时候发现
- 有三种动画插入器:
- accelerate_decelerate_interpolator 加速-减速 动画插入器
- accelerate_interpolator 加速-动画插入器
- decelerate_interpolator 减速- 动画插入器
- 其他的属于特定的动画效果
- 浮点型值:
- fromXScale 属性为动画起始时 X坐标上的伸缩尺寸
- toXScale 属性为动画结束时 X坐标上的伸缩尺寸
- fromYScale 属性为动画起始时Y坐标上的伸缩尺寸
- toYScale 属性为动画结束时Y坐标上的伸缩尺寸
- 说明:
- 以上四种属性值
- 0.0表示收缩到没有
- 1.0表示正常无伸缩
- 值小于1.0表示收缩
- 值大于1.0表示放大
- pivotX 属性为动画相对于物件的X坐标的开始位置
- pivotY 属性为动画相对于物件的Y坐标的开始位置
- 说明:
- 以上两个属性值 从0%-100%中取值
- 50%为物件的X或Y方向坐标上的中点位置
- 长整型值:
- duration 属性为动画持续时间
- 说明: 时间以毫秒为单位
- 布尔型值:
- fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
- -->
translate.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="30"
- android:toXDelta="-80"
- android:fromYDelta="30"
- android:toYDelta="300"
- android:duration="2000"
- />
- <!-- translate 位置转移动画效果
- 整型值:
- fromXDelta 属性为动画起始时 X坐标上的位置
- toXDelta 属性为动画结束时 X坐标上的位置
- fromYDelta 属性为动画起始时 Y坐标上的位置
- toYDelta 属性为动画结束时 Y坐标上的位置
- 注意:
- 没有指定fromXType toXType fromYType toYType 时候,
- 默认是以自己为相对参照物
- 长整型值:
- duration 属性为动画持续时间
- 说明: 时间以毫秒为单位
- -->
- </set>
评论暂时关闭