在xml中实现animation
一 、在res文件夹下新建一个名为anim的文件夹
二 、创建xml文件,首先加入set标签
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
>
三 、在set标签中加入rotate、translate、alpha、scale标签,每一个动画效果占一个xml文件
四、在代码中使用AnimationUtils装载xml文件,并生成Animation对象
Animation animation_alpha = AnimationUtils.loadAnimation(MainActivity.this , R.anim.alpha);
imageView.startAnimation(animation_alpha);
五 、Animation的实现
xml文件
1、alpha.xml
2、scalse.xml
3、rotate.xml
4、translate.xml
布局文件
一 、在res文件夹下新建一个名为anim的文件夹
二 、创建xml文件,首先加入set标签
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
>
三 、在set标签中加入rotate、translate、alpha、scale标签,每一个动画效果占一个xml文件
点击(此处)折叠或打开
- (1)alpha.xml:
- ?xml version="1.0" encoding="utf-8"?>
- set
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
- alpha ?透明效果?>
- android:fromAlpha="0.1" ?开始透明度10%?>
- android:toAlpha="1.0" ?结束透明度?>
- android:duration="3000" ?设置动画执行时间?>
- android:startOffset="500" ?设置动画执行前的等待时间?>
- />
- /set>
-
- (2)rotate.xml:
- ?xml version="1.0" encoding="utf-8"?>
- set
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
- rotate ?旋转效果?>
- android:fromDegrees="0" ?起始角度?>
- android:toDegrees="+350" ?结束角度?>
- android:pivotX="50%" ?旋转点的x坐标 “50”代表绝对位置,“50%”相对于控件本身,“50%p”相对于父控件?>
- android:pivotY="50%" ?旋转点的y坐标?>
- android:duration="3000" ?动画执行的时间?>
- />
- /set>
- (3)scale.xml
- ?xml version="1.0" encoding="utf-8"?>
- set
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
- scale ?缩放效果?>
- android:fromXScale="1.0" ?起始x坐标?>
- android:toXScale="0.1" ?结束x坐标?>
- android:fromYScale="1.0" ?起始y坐标?>
- android:toYScale="0.1" ?结束y坐标?>
- android:pivotX="50%" ?缩放原点x坐标?>
- android:pivotY="50%" ?缩放原点y坐标?>
- android:duration="3000" ?动画持续时间?>
- />
- /set>
Animation animation_alpha = AnimationUtils.loadAnimation(MainActivity.this , R.anim.alpha);
imageView.startAnimation(animation_alpha);
五 、Animation的实现
点击(此处)折叠或打开
- public class MainActivity extends Activity
- {
- private Button alphaButton = null;
- private Button scaleButton = null;
- private Button rotateButton = null;
- private Button translateButton = null;
- private ImageView imageView = null;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //找到控件
- alphaButton = (Button)findViewById(R.id.alphaBtn);
- scaleButton = (Button)findViewById(R.id.scaleBtn);
- rotateButton = (Button)findViewById(R.id.rotateBtn);
- translateButton= (Button)findViewById(R.id.translateBtn);
- imageView = (ImageView)findViewById(R.id.imageViewId);
- //为控件添加事件
- alphaButton.setOnClickListener(new btnListener());
- scaleButton.setOnClickListener(new btnListener());
- rotateButton.setOnClickListener(new btnListener());
- translateButton.setOnClickListener(new btnListener());
- }
-
- class btnListener implements OnClickListener
- {
-
- @Override
- public void onClick(View v)
- {
- // TODO Auto-generated method stub
- switch(v.getId())
- {
- case R.id.alphaBtn:
- //透明效果
- Animation animation_alpha = AnimationUtils.loadAnimation(MainActivity.this , R.anim.alpha);
- imageView.startAnimation(animation_alpha);
- break;
- case R.id.scaleBtn:
- //缩放效果
- Animation animation_scale = AnimationUtils.loadAnimation(MainActivity.this , R.anim.scale);
- imageView.startAnimation(animation_scale);
- break;
- case R.id.rotateBtn:
- //旋转效果
- Animation animation_rotate = AnimationUtils.loadAnimation(MainActivity.this , R.anim.rotate);
- imageView.startAnimation(animation_rotate);
- break;
- case R.id.translateBtn:
- //移动效果
- Animation animation_translate = AnimationUtils.loadAnimation(MainActivity.this , R.anim.translate);
- imageView.startAnimation(animation_translate);
- break;
- default:
- break;
- }
- }
-
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- }
1、alpha.xml
点击(此处)折叠或打开
- ?xml version="1.0" encoding="utf-8"?>
- set
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
-
- alpha
- android:fromAlpha="0.1"
- android:toAlpha="1.0"
- android:duration="3000"
- android:startOffset="500"
- />
- /set>
点击(此处)折叠或打开
- ?xml version="1.0" encoding="utf-8"?>
- set
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
-
- scale
- android:fromXScale="1.0"
- android:toXScale="0.1"
- android:fromYScale="1.0"
- android:toYScale="0.1"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="3000"
- />
-
- /set>
点击(此处)折叠或打开
- ?xml version="1.0" encoding="utf-8"?>
- set
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
-
- rotate
- android:fromDegrees="0"
- android:toDegrees="+350"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="3000"
- />
-
- /set>
点击(此处)折叠或打开
- ?xml version="1.0" encoding="utf-8"?>
- set
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- >
- translate
-
- />
-
- /set>
点击(此处)折叠或打开
- RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
-
- TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- Button
- android:id="@+id/alphaBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:text="alpha动画"
- />
- Button
- android:id="@+id/scaleBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@id/alphaBtn"
- android:text="scale动画"
- />
- Button
- android:id="@+id/rotateBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@id/scaleBtn"
- android:text="rotate动画"
- />
- Button
- android:id="@+id/translateBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@id/rotateBtn"
- android:text="translate动画"
- />
- ImageView
- android:id="@+id/imageViewId"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_centerInParent="true"
- android:layout_marginTop="100dip"
- android:src="@drawable/ic_launcher"
- />
- /RelativeLayout>