概述:利用AnimationSet使所有的动画效果同时显示
一、AnimationSet的使用
1、它是Animation的 一个子类
2、一个AnimationSet包含了一系列的Animation
3、针对AnimationSet设置一系列的属性,可以被应用在AnimationSet中的所有Animation
(1)如果在xml文件中实现动画,那么所有的动画效果都设在同一个xml文件中
xmlns:android=""
android:interpolator="@android:anim/accelerate_interpolator">
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
android:startOffset="500"
/>
android:fromDegrees="0"
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>
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"
/>
(2)如果在代码中同时实现所有动画效果
//定义一个AnimationSet
AnimationSet animationSet = new AnimationSet(true);
//定义几个Animation
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//将Animation添加到AnimationSet中
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
//为aAnimationSet添加效果,会应用在所有的Animation
animationSet.setDuration(5000);
animationSet.setStartOffset(3000);
//启动AnimationSet
imageView.startAnimation(animationSet);
二、什么是interpolator
1、在xml文件中的情况
xmlns:android=""
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
>
(1) 定义了动画变化的速率
android:interpolator="@android:anim/accelerate_interpolator"
accelerate_interpolator 动画开始变化速率慢,然后加速
decelerate_interpolator 动画开始变化速率快,然后减速
accelerate_decelerate_interpolator 动画开始和结束的变化速率慢,中间加速
cycle_interpolator 动画播放特定次数,变化速率沿着正弦曲线
linear_interpolator 动画匀速进行
(2) android:shareInterpolator="true"
shareInterpolator为true代表应用于所有的动画
如果是false,那么需要为每一个动画添加自己的变化速率
2、在代码中使用interpolator
(1)AnimationSet animationSet = new AnimationSet(true);
true代表所有的animation共享interpolator,如果是false就需要为每一个animation添加interpolator
(2)animationSet.setInterpolator(new AccelerateInterpolator());
创造一个interpolator
三、源代码
MainActivity.java 主程序
-
public class MainActivity extends Activity
-
{
-
private Button testButton = null;
-
private ImageView imageView = null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
//找到控件
-
testButton = (Button)findViewById(R.id.testBtn);;
-
imageView = (ImageView)findViewById(R.id.imageViewId);
-
//为控件添加事件
-
testButton.setOnClickListener(new btnListener());
-
}
-
-
class btnListener implements OnClickListener
-
{
-
-
@Override
-
public void onClick(View v)
-
{
-
// TODO Auto-generated method stub
-
//装载xml文件中的Animation
-
/*
-
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
-
//启动Animation,所有的效果同时发生
-
imageView.startAnimation(animation);
-
*/
-
//定义一个AnimationSet
-
AnimationSet animationSet = new AnimationSet(true);
-
animationSet.setInterpolator(new AccelerateInterpolator());
-
//定义几个Animation
-
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
-
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
-
Animation.RELATIVE_TO_SELF, 0.5f,
-
Animation.RELATIVE_TO_SELF, 0.5f);
-
//将Animation添加到AnimationSet中
-
animationSet.addAnimation(alphaAnimation);
-
animationSet.addAnimation(scaleAnimation);
-
//为aAnimationSet添加效果,会应用在所有的Animation
-
animationSet.setDuration(5000);
-
animationSet.setStartOffset(3000);
-
//启动AnimationSet
-
imageView.startAnimation(animationSet);
-
}
-
-
}
-
@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;
-
}
-
-
}
animation.xml animation动画文件
-
<?xml version="1.0" encoding="utf-8"?>
-
<set
-
xmlns:android=""
-
android:interpolator="@android:anim/accelerate_interpolator"
-
android:shareInterpolator="true"
-
>
-
-
<alpha
-
android:fromAlpha="0.1"
-
android:toAlpha="1.0"
-
android:duration="3000"
-
android:startOffset="500"
-
/>
-
<rotate
-
android:fromDegrees="0"
-
android:toDegrees="+350"
-
android:pivotX="50%"
-
android:pivotY="50%"
-
android:duration="3000"
-
/>
-
<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>
四、布局文件
activity_main.xml activity布局文件
-
<RelativeLayout xmlns:android=""
-
xmlns: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/testBtn"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:text="测试动画"
-
/>
-
/>
-
<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>
阅读(1180) | 评论(0) | 转发(0) |