前面的博客 Android 动画效果(一): 四种动画基础(Alpha、Translate、Rotate、Scale) 和 Android 动画效果(二):四种基础动画的 **动态设置、动画监听、组合动画 中已经介绍了普通的动画Animation,通过Animation已经可以实现不少动画效果,但是实际上Android还为我们提供了一种动画——Animatior(属性动画),为什么还要为我们再提供一种动画呢?我们来看下Animator相比Animation的优势在哪里。
  现在我们就来比较一下二者的区别。

Animation效果演示

1、布局
关于布局我就不多说了,大家都看的懂,只是这里我们给ImageView设置了一个点击事件也就是这句代码android:onClick="imageclick"

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >    <Button  android:id="@+id/btn_ad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始移动" />    <ImageView  android:id="@+id/imageview_ad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="imageclick" android:src="@drawable/hi"/></LinearLayout>

2、Animation动画
我们先使用animation动画。当点击Button时开始动画

public void onClick(View v) {        TranslateAnimation animation =new TranslateAnimation(0,300,0,300);        animation.setDuration(2000);        //记录动画的结束位置,停留在那里        animation.setFillAfter(true);      imageview.startAnimation(animation);    }    //ImageView的点击事件监听    public void imageclick(View view){        Toast.makeText(MainActivity_advatage.this, "当前图片被点击了", Toast.LENGTH_SHORT).show();    }

3、效果

通过图片展示可以看出动画完成后,当我点击动画时,动画的点击事件并未响应(没有弹出Toast),当我点击动画原来的位置时弹出了Toast,显然这并不合乎常理。

Animator效果演示

1、将动画进行修改为属性动画。

@Override    public void onClick(View v) {    //  imageview.setTranslationX(translationX)    //像这种有set、get方法的属性才能被ObjectAnimator进行设置          ObjectAnimator.ofFloat(imageview,"translationX", 0,300).setDuration(2000).start();          ObjectAnimator.ofFloat(imageview,"translationY", 0,300).setDuration(2000).start();    }

2、效果展示

通过上面的效果图可以看出,这次,我们点击图片,图片的点击事件进行了响应,而点击图片的原来位置将没有反应。

二者区别

  通过上面两个例子的对比,相信你已经清楚了二者的区别。Animation只是对我们的界面进行了重新的绘制,并不能响应图片的属性,而Animator则可以进行属性的响应,在后面的博客中将对属性动画的使用进行进一步的讲解。

更多相关文章

  1. [置顶] Android(安卓)animation动画
  2. Android(安卓)玩转Glide4---Transformation篇
  3. Android之SlidingDrawer抽屉效果
  4. (转)修改Android的开关机铃声、Android开关机画面与动画(附代码流程
  5. Android水波纹点击效果
  6. Android(1.5) 开机图片/文字/动画 修改
  7. Android(安卓)Animation---总揽
  8. android主流UI布局
  9. Android移动view动画问题 关于view的位置移动了,但view里面绑定的

随机推荐

  1. Android的NDK开发
  2. Android(安卓)动画整理(3.0以上)
  3. 丢失Android系统库或者Conversion to Dal
  4. WebView---Android与js交互实例
  5. Android4.2增加新键值
  6. android与js的交互
  7. android静默安装的实现(turn)
  8. Android培训班(38)
  9. flutter集成推送功能-小米推送集成
  10. Android requires compiler compliance l