Android笔记之属性动画

前言、动画分类

例如以下图所看到的,Android的动画主要分为三种:

 


以下首先说说 属性动画

所谓属性动画—— 就是指对象的属性值发生了变化,如控件位置和透明度等。

举例,如今要实现一个按键先下移。再右移的动画。

1)编写动画xml

由于新建androidproject的时候,在res以下并没有专门放置动画xml的目录。因此,我们新建一个animator名称的目录。建议不要起别的名字,由于ADTres的目录命名有检索功能,如起animator这个名字的时候。ADT就能依据名称识别出这个是动画xml目录,在你新建xml的时候。会给对应的根元素选择。

例如以下图所看到的:


动画XML的代码例如以下——

<?xml version="1.0" encoding="utf-8"?

> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" > <objectAnimator android:duration="2000" android:propertyName="y" android:valueTo="500" android:valueType="intType"></objectAnimator> <objectAnimator android:duration="2000" android:propertyName="a" android:valueTo="300" android:valueType="intType"></objectAnimator> </set>



对动画xml的说明例如以下:  

  •  android:ordering说明一系列动画动作的运行顺序,有两个选择。sequentially together,顺序运行还是一起运行;
  • objectAnimator 是设定动画实施的对象,duration是该动画动作运行从開始到结束所用的时间,属性是y(后面在Activity会讲到,这是自定义的属性,用来指button的在屏幕中的y坐标值。),属性值是500,类型是整型。

2Activity代码

public class PropertyActivity extends Activity {

	public final static String TAG = "PropertyActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_property);
		
		final Button moveButton = (Button)findViewById(R.id.move_btn);
		final Move move = new Move(moveButton);
		moveButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 装载属性动画资源
				AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(PropertyActivity.this,
					R.animator.property_anim);
				// 设置要控制的对象
				set.setTarget(move);
				// 開始动画
				set.start();
				Log.d(TAG, "getWidth:"+moveButton.getWidth());
				Log.d(TAG, "Top:"+moveButton.getTop());
				Log.d(TAG, "getMeasuredWidth:"+moveButton.getMeasuredWidth());
				Log.d(TAG, "getBottom:"+moveButton.getBottom());
			}
		});

	}
	
	private class Move{
		 private int a;
		 private int y;
		 private View view;
		 public Move(View view) {
			 this.view = view;
		}
		 
		 public int getY()
			{  
				return y;
			}
			
			public void setY(int y)
			{
				this.y = y;
				view.layout(view.getLeft(), y, view.getRight(),
					y + view.getMeasuredHeight());
			}
			
			public int getA()
			{
				return a;
			}
			
			public void setA(int a)
			{
				this.a = a;
				
				Log.d(TAG, "End_getWidth:"+view.getWidth());
				Log.d(TAG, "End_Top:"+view.getTop());
				Log.d(TAG, "End_getMeasuredWidth:"+view.getMeasuredWidth());
				Log.d(TAG, "End_getBottom:"+view.getBottom());
				view.layout(a, view.getTop(), a + view.getMeasuredWidth(),
					view.getBottom());
			}
	 }
	

}

我们在程序中Logcat打印出对button位置的详细值。

Log.d(TAG,"getWidth:"+moveButton.getWidth());

Log.d(TAG,"Top:"+moveButton.getTop());

Log.d(TAG,"getMeasuredWidth:"+moveButton.getMeasuredWidth());

Log.d(TAG,"getBottom:"+moveButton.getBottom());

结果例如以下:


上下高度确实是从0变化到500,实现了移动。

 

上面代码中的属性x,y都是自己随意取的变量值,在set方法中设置了详细view的高度和宽度,因此,变量名称是什么不重要,仅仅要xml与这里java代码相符合即可。

  get()方法没必要的,而set方法是必须的,由于   

AnimatorSet动画设定类中就须要调用对象的属性设定方法。通过动画来改变对应属性。


以上仅仅是简单地演示了属性动画,实际上这个是从Android 3.0后才出来的。3.0之前有一个非常著名的动画开源项目,名字叫做 Nine Old Androids ,该项目开发了非常多属性动画, 例如以下图所看到的,不好意思,不会做gif动态图。大家能够直接在博文后面的链接下。里面有APK和源码。值得一读。









原文地址:https://www.cnblogs.com/zfyouxi/p/5400862.html