Android(安卓)Property动画——ObjectAnimator使用
Only the road is right, do not be afraid of it is short or distant, because you will eventually arrive.
只要路是对的,就不要害怕它是短暂还是遥远,因为你终究会到达。
1. 概述
1.1 前言
ValueAnimator有个缺点,只能对数值对动画计算。想要对哪个控件操作,需要监听动画过程,在监听中对控件操作。这样使用起来相比补间动画比较麻烦。
ObjectAnimator是派生自ValueAnimator的,所以ValueAnimator中所能使用的方法,在ObjectAnimator中都可以正常使用。
ObjectAnimator重写了几个方法,例如ofInt()、ofFloat等。
示例:
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"alpha",1,0,1);animator.setDuration(2000);animator.start();
效果如下:
ObjectAnimator的ofFloat函数声明如下:
public static ObjectAnimator ofFloat(Object target, String propertyName, float... values)
参数:
第一个参数用于指定这个动画要操作的是哪个控件
第二个参数用于指定这个动画要操作这个控件的哪个属性
第三个参数是可变长参数,值这个参数从哪变到哪。
对于ObjectAnimator,只需要改变ofFloat()等的第二个参数就可以实现对应的动画。
1.2 setter函数
ObjectAnimator做动画,并不是根据控件的xml中属性来改变的,而是通过指定属性所对应的set方法来改变的。在View中有关动画,总共有以下几组set方法:
//1、透明度:alphapublic void setAlpha(float alpha) //2、旋转度数:rotation、rotationX、rotationYpublic void setRotation(float rotation)//表示围绕Z轴旋转,rotation表示旋转度数public void setRotationX(float rotationX)//表示围绕X轴旋转,rotationX表示旋转度数public void setRotationY(float rotationY)//表示围绕Y轴旋转,rotationY表示旋转度数 //3、平移:translationX、translationYpublic void setTranslationX(float translationX) //表示在X轴上的平移距离,以当前控件为原点,向右为正方向,参数translationX表示移动的距离public void setTranslationY(float translationY)//表示在Y轴上的平移距离,以当前控件为原点,向右为正方向,参数translationY表示移动的距离 //缩放:scaleX、scaleYpublic void setScaleX(float scaleX)//在X轴上缩放,scaleX表示缩放倍数public void setScaleY(float scaleY)//在Y轴上缩放,scaleY表示缩放倍数
- 要使用ObjectAnimator来构造对画,要操作的控件中,必须存在对应的属性的set方法
- setter 方法的命名必须以骆驼拼写法命名,即set后每个单词首字母大写,其余字母小写,即类似于setPropertyName所对应的属性为propertyName
1.3 ObjectAnimator动画原理
ObjectAnimator的动画设置流程:ObjectAnimator需要指定操作的控件对象,在开始动画时,到控件类中去寻找设置属性所对应的set函数,然后把动画中间值作为参数传给这个set函数并执行它。
示例:
-
保存圆形信息类——Point
定义一个类(Point.java)
public class Point { private int mRadius; public Point(int radius){ mRadius = radius; } public int getRadius() { return mRadius; } public void setRadius(int radius) { mRadius = radius; }}
-
自定义控件——MyPointView
public class MyPointView extends View { private Point mPoint = new Point(100); public MyPointView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { if (mPoint != null){ Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.RED); paint.setStyle(Paint.Style.FILL); canvas.drawCircle(300,300,mPoint.getRadius(),paint); } super.onDraw(canvas); } void setPointRadius(int radius){ mPoint.setRadius(radius); invalidate(); } }
代码中set函数对应的属性应该是pointRadius或PointRadius
在setPointRadius中,先将当前动画传过来的值保存到mPoint中,做为当前圆形的半径,然后强制界面刷新。
-
在布局中添加MyPointView的使用(main.xml)
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:padding="10dp" android:text="start anim" /> <Button android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="10dp" android:text="cancel anim" /> <TextView android:id="@+id/tv" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:gravity="center" android:padding="10dp" android:background="#ffff00" android:text="Hello qijian"/> <com.example.BlogObjectAnimator1.MyPointView android:id="@+id/pointview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/tv"/> RelativeLayout>
-
主代码:
public class MyActivity extends Activity { private Button btnStart; private MyPointView mPointView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnStart = (Button) findViewById(R.id.btn); mPointView = (MyPointView)findViewById(R.id.pointview); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doPointViewAnimation(); } }); } …………}
private void doPointViewAnimation(){ ObjectAnimator animator = ObjectAnimator.ofInt(mPointView, "pointRadius", 0, 300, 100); animator.setDuration(2000); animator.start();}
1.4 何时实现对应属性的get函数
public static ObjectAnimator ofFloat(Object target, String propertyName, float... values)public static ObjectAnimator ofInt(Object target, String propertyName, int... values)public static ObjectAnimator ofObject(Object target, String propertyName,TypeEvaluator evaluator, Object... values)
三个构造方法中最后一个参数都是可变长参数,如果只定义一个值:
ObjectAnimator animator = ObjectAnimator.ofInt(mPointView, "pointRadius",100);
它从哪里开始变化?当且仅当我们只给动画设置一个值,程序才会调用属性对应的get函数来得到动画的初始值。无初始值则使用系统默认值。
1.5 示例
详细代码参见Github
更多相关文章
- android sqlite query用法和参数含义
- Android(安卓)实现按钮在没有触摸事件时几秒钟之后自动隐藏
- Android,一个函数实现计步器(软计步,硬计步)
- Android(安卓)View.onMeasure方法的理解
- 浅谈Android中常用的五种布局方式
- android错误之ListView设置setOnItemClickListener失效
- 一起学android之自定义控件一起制作自定义标签(39)
- Android(安卓)自定义控件-SnakeLayout (仿gallery)
- Android入门:4种布局