您的位置:首页 > 移动开发 > Android开发

Android中属性动画Property Animation使用示例(一)

2014-04-10 14:57 561 查看
MainActivity如下:

package cc.cn;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.Animator.AnimatorListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* Demo描述:
* Android中属性动画Property Animation使用示例(一)
*
* 参考资料:
* 1 http://blog.csdn.net/linmiansheng/article/details/18676845 *   说明了关于View动画的缺陷,即引入Property Animation的必要性
* 2 关于属性动画的中文文档,请参见:
*   http://blog.csdn.net/think_soft/article/details/7703684 *   http://wiki.eoeandroid.com/Property_Animation *   Thank you very much
*
*/
public class MainActivity extends Activity {
private Button mScaleXButton;
private Button mScaleXYButton;
private Button mTranslateXButton;
private Button mAlphaButton;
private Button mAnimatorSetByCodesButton;
private Button mPropertyValuesHolderButton;
private Button mViewPropertyAnimatorButton;
private ObjectAnimator mObjectAnimator1;
private ObjectAnimator mObjectAnimator2;
private ObjectAnimator mObjectAnimator3;
private AnimatorSet mAnimatorSet1;
private AnimatorSet mAnimatorSet2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}

private void init(){
//--------->以下为在X方向上进行缩放的属性动画
mScaleXButton=(Button) findViewById(R.id.scaleXButton);
mScaleXButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mObjectAnimator1.start();

}
});
mObjectAnimator1=(ObjectAnimator)AnimatorInflater.loadAnimator(this, R.animator.scalexanimator);
mObjectAnimator1.setTarget(mScaleXButton);

//--------->以下为在X和Y方向上进行缩放的属性动画
mScaleXYButton=(Button) findViewById(R.id.scaleXYButton);
mScaleXYButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mAnimatorSet1.start();

}
});
mAnimatorSet1=(AnimatorSet)AnimatorInflater.loadAnimator(this, R.animator.setanimator);
mAnimatorSet1.setTarget(mScaleXYButton);

//--------->以下为在X方向上进行位移的属性动画
mTranslateXButton=(Button) findViewById(R.id.translateXButton);
mTranslateXButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mObjectAnimator2.start();

}
});
mObjectAnimator2=(ObjectAnimator)AnimatorInflater.loadAnimator(this, R.animator.translatexanimator);
mObjectAnimator2.setTarget(mTranslateXButton);

//--------->以下为透明度变化的属性动画
mAlphaButton=(Button) findViewById(R.id.alphaButton);
mAlphaButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mObjectAnimator3.start();

}
});
mObjectAnimator3=(ObjectAnimator)AnimatorInflater.loadAnimator(this, R.animator.alphaanimator);
mObjectAnimator3.setTarget(mAlphaButton);

//--------->以下为在代码中实现AnimatorSet
mAnimatorSetByCodesButton=(Button) findViewById(R.id.animatorSetByCodesButton);
mAnimatorSetByCodesButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mAnimatorSet2.start();

}
});
mAnimatorSet2=new AnimatorSet();
//设置动画
mAnimatorSet2.playTogether(
ObjectAnimator.ofFloat(mAnimatorSetByCodesButton, "alpha", 1,0,1),
ObjectAnimator.ofFloat(mAnimatorSetByCodesButton, "translationX", 0f,400f,0f),
ObjectAnimator.ofFloat(mAnimatorSetByCodesButton, "translationY", 0f,400f,0f),
ObjectAnimator.ofFloat(mAnimatorSetByCodesButton, "rotation", 0,180,360)
);
//设置动画时间
mAnimatorSet2.setDuration(2000);
//设置动画监听
mAnimatorSet2.addListener(new AnimatorListener() {

@Override
public void onAnimationStart(Animator animator) {
System.out.println("---> onAnimationStart");
}

@Override
public void onAnimationRepeat(Animator animator) {
System.out.println("---> onAnimationRepeat");
}

@Override
public void onAnimationEnd(Animator animator) {
System.out.println("---> onAnimationEnd");
}

@Override
public void onAnimationCancel(Animator animator) {
System.out.println("---> onAnimationCancel");
}
});

//在代码中要实现一个对象不同属性的动画效果,除了AnimatorSet外
//还可利用PropertyValuesHolder和ViewPropertyAnimator对象来实现
//--------->以下为在代码中利用PropertyValuesHolder实现类似AnimatorSet的效果
mPropertyValuesHolderButton=(Button) findViewById(R.id.propertyValuesHolderButton);
PropertyValuesHolder propertyValuesHolderX=PropertyValuesHolder.ofFloat("translationX", 0f,200f);
PropertyValuesHolder propertyValuesHolderY=PropertyValuesHolder.ofFloat("translationY", 0f,200f);
final ObjectAnimator objectAnimator
=ObjectAnimator.ofPropertyValuesHolder(mPropertyValuesHolderButton, propertyValuesHolderX, propertyValuesHolderY);
objectAnimator.setDuration(2000);
mPropertyValuesHolderButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
objectAnimator.start();
}
});

//在代码中要实现一个对象不同属性的动画效果,除了AnimatorSet外
//还可利用PropertyValuesHolder和ViewPropertyAnimator对象来实现
//--------->以下为在代码中利用ViewPropertyAnimator实现类似AnimatorSet的效果
mViewPropertyAnimatorButton=(Button) findViewById(R.id.viewPropertyAnimatorButton);
mViewPropertyAnimatorButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
ViewPropertyAnimator viewPropertyAnimator=mViewPropertyAnimatorButton.animate();
//位移
viewPropertyAnimator.translationX(100f);
//透明度
viewPropertyAnimator.alpha(0.5f);
//监听
viewPropertyAnimator.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
}

@Override
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
}
@Override
public void onAnimationEnd(Animator animator){
//动画结束后将其透明度和位置还原
v.animate().alpha(1).translationX(0);
}
});
viewPropertyAnimator.start();

}
});

}
}


main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/scaleXButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:text="scaleXButton" />

<Button
android:id="@+id/scaleXYButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:text="scaleXYButton" />

<Button
android:id="@+id/translateXButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:text="translateXButton" />

<Button
android:id="@+id/alphaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:text="alphaButton" />

<Button
android:id="@+id/animatorSetByCodesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:text="AnimatorSetByCodesButton" />

<Button
android:id="@+id/propertyValuesHolderButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:text="PropertyValuesHolderButton" />

<Button
android:id="@+id/viewPropertyAnimatorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:text="ViewPropertyAnimatorButton" />

</LinearLayout>


alphaanimator.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="alpha"
android:duration="2500"
android:valueFrom="1.0"
android:valueTo="0"
android:repeatCount="1"
android:repeatMode="reverse">
</objectAnimator>


scalexanimator.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="scaleX"
android:duration="3000"
android:valueFrom="1.0"
android:valueTo="2.0"
android:repeatCount="1"
android:repeatMode="reverse"
>

</objectAnimator>


setanimator.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together" >

<objectAnimator
android:duration="3000"
android:propertyName="scaleX"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="1.0"
android:valueTo="2.0" >
</objectAnimator>

<objectAnimator
android:duration="3000"
android:propertyName="scaleY"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="1.0"
android:valueTo="2.0" >
</objectAnimator>

</set>


translatexanimator.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="translationX"
android:duration="2000"
android:valueFrom="0"
android:valueTo="150"
android:repeatCount="1"
android:repeatMode="reverse">
</objectAnimator>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: