首页 / 操作系统 / Linux / Android属性动画之ObjectAnimator
相信对于Android初学者,对于Android中的动画效果一定很感兴趣,今天为大家总结一下刚刚学到的属性动画案例。首先和一般的Android应用一样,我们先建一个工程,为了方便,我们的布局文件中就只添加一个ImageView和button按钮,代码如下:<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/ic_launcher"
android:onClick="imgClick"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动画"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="86dp"
android:onClick="buttonClick"/>
</RelativeLayout>下面是我们action,为了便于大家学习,我将代码分享如下:public class MainActivity extends Activity {
public ImageView imageView;
public Button button;
static int x = 0, xx = 0, y = 0, yy = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
button = (Button)findViewById(R.id.button);
} public void imgClick(View view){
Toast.makeText(this, "ImageView", Toast.LENGTH_SHORT).show();
}
public void buttonClick(View view){// xx += 20;
// TranslateAnimation ta = new TranslateAnimation(x, xx, y, yy);//设置动画的偏移位移
// x += 20;
// ta.setDuration(1000);//设置动画的时长
// ta.setFillAfter(true);//设置动画结束后停留在该位置
// imageView.startAnimation(ta); //属性动画调用start()方法后是一个异步操作
// ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F).setDuration(1000).start();//X轴平移旋转
// ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F).setDuration(1000).start();//Y轴平移旋转
// ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(1000).start();//360度旋转 //同步动画设计
// PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0, 360F);
// PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY", 0, 360F);
// PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation", 0, 360F);
// ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2 ,p3).setDuration(1000).start(); //通过AnimatiorSet来设计同步执行的多个属性动画
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F);//X轴平移旋转
ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F);//Y轴平移旋转
ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);//360度旋转
AnimatorSet set = new AnimatorSet();
//set.playSequentially(animator1, animator2, animator3);//分步执行
//set.playTogether(animator1, animator2, animator3);//同步执行 //属性动画的执行顺序控制
// 先同步执行动画animator2和animator3,然后再执行animator1
set.play(animator3).with(animator1);
set.play(animator2).after(animator3); set.setDuration(1000);
set.start(); }
}对于关键位置,我已经进行了详细的注释,大家可以拷贝到自己的项目中进行测试,相信大家一定可以掌握Android中的属性动画的知识。Android 4.4.4 发布下载 http://www.linuxidc.com/Linux/2014-06/103467.htm最简单的Ubuntu Touch & Android 双系统安装方式 http://www.linuxidc.com/Linux/2014-01/94881.htm在Nexus上实现Ubuntu和Android 4.4.2 双启动 http://www.linuxidc.com/Linux/2014-05/101849.htmUbuntu 14.04 配置 Android SDK 开发环境 http://www.linuxidc.com/Linux/2014-05/101039.htm64位Ubuntu 11.10下Android开发环境的搭建(JDK+Eclipse+ADT+Android SDK详细) http://www.linuxidc.com/Linux/2013-06/85303.htmUbuntu 14.04 x64配置Android 4.4 kitkat编译环境的方法 http://www.linuxidc.com/Linux/2014-04/101148.htmUbuntu 12.10 x64 安装 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-02/113536.htm