Android:Animation的简单学习
Android:Animation的简单学习
Animation动画效果。提供了一系列的动画效果,可以应用大多数 的控件。
一、Animations从总体上来说可以分为两大类:
1、TweenedAnimations:该类提供了旋转,移动,伸展,和淡出竺效果;
2、Frame-by-FrameAmimations:这一类可以创建一个Drawable序列:这些Drawable可以按照指定的时间间歇一个一个的显示。
二、TwenedAnimations的分类
a) Alpha:淡入淡出效果
b) Scale:缩放效果
c) Rotate:旋转效果
d) Translate:移动效果
这四种动画效果对应四个不同的类,都有不同的参数,但是这四个不同的动画效果有共同的参数:下面简单介绍下面几种:
setDuration(long durationMills)设置动画持续时间,单位毫秒 ;
setFillAfter(boolean fillAfter)如果为true的话,动画执行后,控件停留在执行结束的状态;
setFillBefore(boolean fillBefore)
setStartOffSet(long startOffSet)设置动画执行之前等待时间;
setRepeatCount(int repeatCount)设置动画重复的次数
下面简单介绍一下,TewnedAnimation的使用步骤:1、创建一个AnimationSet,存放动画集合,也可以只有一个动画。
2、根据需要创建对应的Animation
3、根据动画的需求,为Animation创建相对应的数据。
4、将Animation对象添加到AnimationSet
5、使用控件开始执行Animation:textView.startAnimation(AnimateionSet)当然里面的参数也可以是Animate,由于Animation是AnimationSet的父类,所以不会出错
我们来做一个简单的例子,了解Animation的用法 :
1、 在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"
- >
- <ImageView android:id="@+id/image"
- android:src="@drawable/icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="100dip"/>
- <Button android:id="@+id/alpha"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/alpha"/>
- <Button android:id="@+id/scale"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/scale"/>
- <Button android:id="@+id/rotate"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/rotate"/>
- <Button android:id="@+id/translate"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/translate"/>
- </LinearLayout>
- public class HelloAnimationActivity extends Activity {
- /** Called when the activity is first created. */
- Button b_alpha;
- Button b_scale;
- Button b_rotate;
- Button b_translate;
- ImageView iv;
- AnimationSet animationSet;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- iv = (ImageView)findViewById(R.id.image);
- b_alpha = (Button)findViewById(R.id.alpha);
- b_scale = (Button)findViewById(R.id.scale);
- b_rotate = (Button)findViewById(R.id.rotate);
- b_translate = (Button)findViewById(R.id.translate);
- b_alpha.setOnClickListener(new AlpahListener());
- b_scale.setOnClickListener(new ScaleListener());
- b_rotate.setOnClickListener(new RotateListener());
- b_translate.setOnClickListener(new TranslateListener());
- }
- class AlpahListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- //创建一个AnimationSet对象
- AnimationSet animationSet = new AnimationSet(true);
- //创建一个AlphaAnimation对象
- AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
- //设置动画执行的时间(单位:毫秒)
- alphaAnimation.setDuration(1000);
- //将AlphaAnimation对象添加到AnimationSet当中
- animationSet.addAnimation(alphaAnimation);
- //使用ImageView的startAnimation方法开始执行动画
- iv.startAnimation(animationSet);
- }
- }
- class ScaleListener implements OnClickListener{
- /**
- * 从大到小的动画,x轴1到0.1,y轴1到0.1。中心为图片的中心(0.5f,0.5f)
- */
- @Override
- public void onClick(View arg0) {
- AnimationSet animationSet = new AnimationSet(true);
- 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);
- animationSet.setFillAfter(true);
- animationSet.setFillBefore(false);
- animationSet.setDuration(2000);
- iv.startAnimation(animationSet);
- }
- }
- class RotateListener implements OnClickListener{
- /**
- * 以图片中心旋转360度;(0.5f,0.5f)表示图片的中心
- */
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- animationSet = new AnimationSet(true);
- RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0.5f);
- rotateAnimation.setDuration(5000);
- animationSet.addAnimation(rotateAnimation);
- iv.setAnimation(rotateAnimation);
- }
- }
- class TranslateListener implements OnClickListener{
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- animationSet = new AnimationSet(true);
- TranslateAnimation translateAnimation = new TranslateAnimation(
- Animation.RELATIVE_TO_SELF, 0f,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0f,
- Animation.RELATIVE_TO_SELF, 1.0f);
- animationSet.addAnimation(translateAnimation);
- iv.setAnimation(animationSet);
- }
- }
- }
|
评论暂时关闭