Android属性动画赏析
2016-01-31 01:00
393 查看
为什么有Animation了,还要有Animator呢?
Animation有局限性,看下面代码.
XML布局:
MainActicity文件
可以发现Animation只是改变了位置,看似改变.实际上并没有改变,不适合交互;于是有了Animator;
package com.zhong.animatordemo;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
}
“`
Animation有局限性,看下面代码.
XML布局:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.zhong.tutu.MainActivity"> <android.support.v7.widget.AppCompatImageView android:id="@+id/image_view" android:onClick="click" android:background="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:text="点击" android:onClick="move" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/> </RelativeLayout>
MainActicity文件
package com.zhong.tutu; import android.animation.ObjectAnimator; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ Toast.makeText(this,"我的土司",Toast.LENGTH_SHORT).show(); } public void move(View view){ // TranslateAnimation trans = new TranslateAnimation(0,200,0,100); // trans.setDuration(3000); // trans.setFillAfter(true); ImageView image_view =(ImageView) findViewById(R.id.image_view); // image_view.startAnimation(trans); ObjectAnimator.ofFloat(image_view,"translationX",0f,200f).setDuration(1000).start(); } }
可以发现Animation只是改变了位置,看似改变.实际上并没有改变,不适合交互;于是有了Animator;
package com.zhong.tutu; import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ Toast.makeText(this,"我是imageview",Toast.LENGTH_SHORT).show(); } public void move(View view){ // TranslateAnimation transA = new TranslateAnimation(0,200,0,0); // transA.setDuration(1000); // transA.setFillAfter(true); ImageView imageView = (ImageView) findViewById(R.id.image_view); // imageView.startAnimation(transA); //属性动画 // ObjectAnimator.ofFloat(imageView,"translationX",0f,200f).setDuration(1000).start(); // ObjectAnimator.ofFloat(imageView,"translationY",0f,200f).setDuration(1000).start(); // ObjectAnimator.ofFloat(imageView,"rotation",0f,200f).setDuration(400).start(); /* //同时作用于3个动画 PropertyValuesHolder对动画进行优化 PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation", 0f, 200f); PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX", 0f, 200f); PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY", 0f, 200f); ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();*/ ObjectAnimator t1 = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f); ObjectAnimator t2 =ObjectAnimator.ofFloat(imageView,"translationY",0f,200f); ObjectAnimator t3 = ObjectAnimator.ofFloat(imageView,"rotation",0f,200f); AnimatorSet set = new AnimatorSet(); set.play(t1).with(t2); set.play(t3).after(t2); // set.playSequentially(t1,t2,t3); set.setDuration(1000); set.start(); } } 动画的监听事件
package com.zhong.animatordemo;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"alpha",0f,1f); objectAnimator.setDuration(1000); objectAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); Toast.makeText(MainActivity.this," onAnimationEnd",Toast.LENGTH_SHORT).show(); } }); /* objectAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { Toast.makeText(MainActivity.this," onAnimationEnd",Toast.LENGTH_SHORT).show(); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } });*/ objectAnimator.start(); }
}
“`
相关文章推荐
- Android Tip : Android 获取当前Activity对象的方式:
- ubuntu需要额外安装的包用于支持android sdk的运行
- Android动画三部曲之一 View Animation & LayoutAnimation
- 自定义Dialog
- Android编程技巧之获取全局 Context 对象
- android源码(网页)
- Gesture控制图片缩放&Canvas: trying to use a recycled bitmap android.graphics.Bitmap异常
- android service重启
- 【Android】【笔记】《Android 入门经典》part 14 Service
- 【Android】【笔记】《Android 入门经典》part 12 线程与消息处理
- 【Android】【笔记】《Android 入门经典》part 11 ContentProvider 数据共享
- 【Android】【笔记】《Android 入门经典》part 10 数据存储
- 【Android】【笔记】《Android 入门经典》part 9 事件处理
- 【Android】【笔记】《Android 入门经典》part 8 Activity
- Android Material Design控件学习(二)——NavigationView的学习和使用
- 【Android】【笔记】《Android 入门经典》part 7 Intent
- 【Android】【笔记】《Android 入门经典》part 6 Activity
- 【Android】【笔记】《Android 入门经典》part 5 程序调试
- 【Android】【笔记】《Android 入门经典》part 4 Android布局管理
- 【Android】【笔记】《Android 入门经典》part 3 安卓开发组件