Android动画之属性动画
2015-11-02 17:06
465 查看
Android动画之属性动画
概述
补间动画,只是一个动画效果,组件其实还在原来的位置上,xy没有改变,属性动画则反之Demo
MainActivity.java
import android.animation.Animator; import android.animation.AnimatorInflater; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } // 平移 public void translate(View v){ /** * 参数说明: * Object target : 动画作用于哪个组件 * String propertyName : 指定要改变组件的哪个属性 * float... values : 传入0,代表x起始坐标:当前x + 0 * 传入100,代表x终点坐标:当前x + 100 */ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 0, 100); // ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 50, 200); oa.setDuration(2000); oa.setRepeatCount(1); oa.setRepeatMode(ValueAnimator.REVERSE); oa.start(); } // 缩放 public void scale(View v){ /** * 参数说明: * Object target : 动画作用于哪个组件 * String propertyName : 指定要改变组件的哪个属性 * float... values : 缩放比例,2表示本身高度2倍,放大到本身高度的3倍 */ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleY", 2, 3f); oa.setDuration(2000); oa.start(); } // 透明 public void alpha(View v){ /** * 参数说明: * Object target : 动画作用于哪个组件 * String propertyName : 指定要改变组件的哪个属性 * float... values : 透明度变化 参数0--1表示从透明到不透明 */ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 1f); oa.setDuration(2000); oa.start(); } // 旋转 public void rotate(View v){ /** * 参数说明: * Object target : 动画作用于哪个组件 * String propertyName : 指定要改变组件的哪个属性 * float... values : 透明度变化 参数0--1表示从透明到不透明 */ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationX", 0, 360); oa.setDuration(2000); oa.setRepeatCount(1); oa.setRepeatMode(ValueAnimator.REVERSE); oa.start(); } // 播放动画集合 public void fly(View v){ AnimatorSet set = new AnimatorSet(); ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100); oa1.setDuration(2000); oa1.setRepeatCount(1); oa1.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY", 10, 70, 20, 100); oa2.setDuration(2000); oa2.setRepeatCount(1); oa2.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2); oa3.setDuration(2000); oa3.setRepeatCount(1); oa3.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360); oa4.setDuration(2000); oa4.setRepeatCount(1); oa4.setRepeatMode(ValueAnimator.REVERSE); // 设置动画挨个进行 // set.playSequentially(oa1, oa2, oa3, oa4); // 设置动画同时进行 set.playTogether(oa1, oa2, oa3, oa4); set.start(); } // 加载xml配置文件来加载动画 public void xml(View v){ Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator); // 设置作用于哪个组件 at.setTarget(iv); at.start(); } }
activity_main.xml
<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=".MainActivity" > <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="平移" android:onClick="translate" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放" android:onClick="scale" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明" android:onClick="alpha" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转" android:onClick="rotate" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一起飞" android:onClick="fly" /> </LinearLayout> <Button android:layout_below="@id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="xml定义的属性动画" android:onClick="xml" /> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:layout_centerInParent="true" /> </RelativeLayout>
objanimator.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <objectAnimator android:propertyName="translationX" android:duration="200" android:repeatCount="1" android:repeatMode="reverse" android:valueFrom="-100" android:valueTo="100" > </objectAnimator> </set>
相关文章推荐
- 实现QQ登录第三方APP(Android)
- Android 关于轻量级存储PreferencesUtils
- Android事件总线纷发库EventBus的使用方法
- Android Studio进行单元测试
- Android 对Canvas的translate方法总结
- 定制Android系统开发之八——实现从JNI到Java的回调
- Android学习路线
- Android程序开发:简单电话拨号器
- Android移动view动画问题
- Android的分类ListView
- android学习之Service
- Android程序开发:简单电话拨号器
- Android 中文API —— Spinner
- 获取android系统服务
- Android 源码分析01_AsyncTask
- android studio 快捷键设置
- Material Design 中文版
- Java Android SSL 双向认证代码
- android 使用contentobserver监听数据库内容变化
- android总结之ScrollView与RelativeLayout和LinearLayout同时使用时问题总结