Android开发教程:简单的animation动画效果的实现过程
Android开发教程:简单的animation动画效果的实现过程
今天学习动画:Animations的分类:
1.TweenedAnimation :提供旋转、移动、伸展、和淡出的效果
Alpha:淡入淡出效果 Scale:缩放效果
Rotate:旋转效果 Translate: 移动效果
2.Frame-by-Frame Animation :创建Drawable的序列,可以按照指定的时间间隙一个个显示(1s中播放24张以上的图片就可以变成视频了)也可以做成动态广告的形式
3.res文件加下加入XMl文件,可以使用XML文件实现控制动画,XML文件中添加Set标签,标签中加入rotate、alpha、scale、或者translate 代码中实现AnimationUtil装载xml
文件并生成,Animation对象需要注意的是
<!--pivotX pivotY旋转时候 的坐标绝对位置的
50这种方法使用的是绝对位置50%相对于空间本身定位
"50%p"的意思是相对于空控件的父控件的定位 -->
4.接着咱们看看代码的实现过程
首先看看两种方法共同的布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:id="@+id/Alpha"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="淡入淡出" />
- <Button
- android:id="@+id/scale"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="缩放效果" />
- <Button
- android:id="@+id/rotate"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="旋转效果" />
- <Button
- android:id="@+id/translate"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="移动效果" />
- </LinearLayout>
- <ImageView
- android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/a" />
- </LinearLayout>
- package com.wang;
- import com.wang.R.id;
- import android.R.anim;
- import android.R.integer;
- import android.app.Activity;
- import android.os.Bundle;
- import android.provider.ContactsContract.CommonDataKinds.Im;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.AccelerateInterpolator;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.view.animation.AnimationSet;
- import android.view.animation.RotateAnimation;
- import android.view.animation.ScaleAnimation;
- import android.view.animation.Transformation;
- import android.view.animation.TranslateAnimation;
- import android.widget.Button;
- import android.widget.ImageView;
- public class AnimationTestActivity extends Activity {
- private ImageView image = null;
- private Button Alphabutton = null;
- private Button scalebutton = null;
- private Button rotatebutton = null;
- private Button translatebutton = null;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- image = (ImageView) findViewById(R.id.image);
- Alphabutton = (Button) findViewById(R.id.Alpha);
- scalebutton = (Button) findViewById(R.id.scale);
- rotatebutton = (Button) findViewById(R.id.rotate);
- translatebutton = (Button) findViewById(R.id.translate);
- // 淡入淡出按钮的监听时间
- Alphabutton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // 创建一个AnimationSet对象
- AnimationSet animationSet = new AnimationSet(true);
- // j加速播放
- animationSet.setInterpolator(new AccelerateInterpolator());
- // //创建一个AnimationSet对象淡出旋转二合一
- AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
- RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
- Animation.RELATIVE_TO_PARENT, 1f,// X轴
- Animation.RELATIVE_TO_PARENT, 0f);// y轴
- // 将alphaAnimation对象添加到animationSet中
- animationSet.addAnimation(alphaAnimation);
- animationSet.addAnimation(rotateAnimation);
- // 显示的时间为1s
- alphaAnimation.setDuration(3000);
- // 开始执行动画
- image.startAnimation(animationSet);
- // 设置重复的次数
- animationSet.setRepeatCount(4);
- }
- });
- // 缩放按钮的监听事件
- scalebutton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- AnimationSet animationSet = new AnimationSet(true);
- // 从1缩放的0.1
- ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
- 0.1f, Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0.5f);
- animationSet.addAnimation(scaleAnimation);
- // 执行前等待的时间
- animationSet.setStartOffset(1000);
- // dd动画执行之前和之后的状态
- animationSet.setFillAfter(true);
- animationSet.setFillBefore(false);
- animationSet.setDuration(5000);
- image.startAnimation(animationSet);
- }
- });
- // 旋转按钮的单击事件
- rotatebutton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- AnimationSet animationSet = new AnimationSet(true);
- // 创建对象 参数 从0度转到360度
- /*********
- **一个动画控制旋转的一个对象。这个旋转发生int xy平面。 您可以指定点使用的中心旋转,在那里(0,0)是左上角点。
- * 如果未指定,(0,0)是默认的旋转点
- **/
- RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
- Animation.RELATIVE_TO_PARENT, 1f,// X轴
- Animation.RELATIVE_TO_PARENT, 0f);// y轴
- rotateAnimation.setDuration(5000);
- animationSet.addAnimation(rotateAnimation);
- image.startAnimation(animationSet);
- }
- });
- // 移动按钮的点击事件
- translatebutton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- AnimationSet animationSet = new AnimationSet(true);
- TranslateAnimation translatebutton = new TranslateAnimation(
- Animation.RELATIVE_TO_SELF, 0f,// //X轴
- Animation.RELATIVE_TO_SELF, 0.5f,// y轴
- Animation.RELATIVE_TO_SELF, 0f,// X轴
- Animation.RELATIVE_TO_SELF, 1.0f);// y轴
- translatebutton.setDuration(5000);
- }
- });
- }
- }
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:duration="500"
- android:fromAlpha="1.0"
- android:startOffset="500"
- android:toAlpha="0.0" />
- </set>
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%p"
- android:pivotY="50%p"
- android:duration="3000" />
- <!--pivotX pivotY旋转时候 的坐标绝对位置的
- 50这种方法使用的是绝对位置
- 50%相对于空间本身定位
- "50%p"的意思是相对于空控件的父控件的定位
- -->
- </set>
sacle.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"
- android:toXScale="0.0"
- android:fromYScale="1.0"
- android:toYScale="0.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="3000"/>
- </set>
translate.xml 移动效果的文件
- <?xml version="1.0" encoding="UTF-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator" >
- <translate
- android:fromXDelta="50%"
- android:toXDelta="100%"
- android:fromYDelta="0%"
- android:toYDelta="100%"
- android:duration="3000" />
- </set>
7.接着看看第二种方法的Activity的实现过程
- package com.wang;
- import android.app.Activity;
- 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 AnimationXMLActivity extends Activity {
- private ImageView image;
- private Button alphabutton;
- private Button rotatebutton;
- private Button saclebutton;
- private Button translatebutton;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- image = (ImageView) findViewById(R.id.image);
- alphabutton = (Button) findViewById(R.id.alpha);
- rotatebutton = (Button) findViewById(R.id.roate);
- saclebutton = (Button) findViewById(R.id.scale);
- translatebutton = (Button) findViewById(R.id.translate);
- // 淡入淡出按钮的监听事件
- alphabutton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // s使用AnimationUtils装载动画设置文件
- Animation animation = AnimationUtils.loadAnimation(
- AnimationXMLActivity.this, R.anim.alpha);
- // 启动
- image.startAnimation(animation);
- }
- });
- // 旋转效果的监听事件
- rotatebutton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // s使用AnimationUtils装载动画设置文件
- Animation animation = AnimationUtils.loadAnimation(
- AnimationXMLActivity.this, R.anim.rotate);
- image.startAnimation(animation);// TODO Auto-generated method
- // stub
- }
- });
- // 缩放组件的效果监听
- saclebutton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // s使用AnimationUtils装载动画设置文件
- Animation animation = AnimationUtils.loadAnimation(
- AnimationXMLActivity.this, R.anim.sacle);
- image.startAnimation(animation); // TODO Auto-generated method
- // stub
- }
- });
- // 移动效果的监听事件
- translatebutton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // s使用AnimationUtils装载动画设置文件
- Animation animation = AnimationUtils.loadAnimation(
- AnimationXMLActivity.this, R.anim.translate);
- image.startAnimation(animation);
- }
- });
- }
- }
由于是动画效果,所以只能截取图片的一部分
,
评论暂时关闭