title:¶¯»
¶¯»
1.²¹¼ä¶¯»
1.Animation.ABSOLUTE:ÓÃÓÚÖ¸¶¨ºóÃæµÄÊýÖµÊǾø¶ÔÖµµÄÏñËØ¡£
2.Animation.RELATIVETOSELF£º ÓÃÓÚÖ¸¶¨ºóÃæµÄÊýÖµÊÇ×Ô¼º¿í¸ßµÄ±¶Êý
3.Animation.RELATIVETOPARENT : ÓÃÓÚÖ¸¶¨ºóÃæµÄÊýÖµÊÇ×Ô¼ºµÄ¸¸¿Ø¼þ¿í¸ßµÄ±¶Êý
4.Animation.RESTART²¥·ÅÖØÐ»ص½Ôµã
5.Animation.REVERSE²¥·Å»Ø½Ó×Å,·´Ïò²¥·Å
2.Æ½ÒÆ
fromXType ˮƽ²¥·ÅµÄÀàÐÍ
fromXValue ´ÓÄÄ¿ªÊ¼,0±íʾÏÖÔÚµÄλÖÃ
toXType, µ½Ê²Ã´Î»ÖõÄÀàÐÍ
toXValue µ½Ê²Ã´×ø±ê
fromYType ´¹Ö±²¥·ÅµÄÀàÐÍ
fromYValue ´ÓÄÄ¿ªÊ¼,0±íʾÏÖÔÚµÄλÖÃ
toYType µ½Ê²Ã´Î»ÖõÄÀàÐÍ
toYValue µ½Ê²Ã´×ø±ê
TranslateAnimation translate = new TranslateAnimation( Animation.RELATIVE_TO_SELF, -2, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1); //ÉèÖÃÏÔʾʱ¼ä translate.setDuration(3000); //²¥·Å×ÊԴΪÎÞÏÞÑ»· translate.setRepeatCount(Animation.INFINITE); //²¥·Åģʽ,·´×ª²¥·Å,ÏÈ˳×Ų¥Íê,¾Í·´×Ų¥·Å translate.setRepeatMode(Animation.REVERSE); //Äĸö×é¼þÔÚ²¥·Å,ÉèÖà imageView.setAnimation(translate); //¿ªÊ¼²¥·Å translate.start(); |
3.Ðýת
/* * fromDegrees, ´Óʲô½Ç¶È¿ªÊ¼ 0 ´ÓÏÖÔÚµÄ toDegrees, 270¶È pivotXType, XÖÐÐĵãÀàÐÍ * Animation.RELATIVE_TO_SELF×ÔÉí pivotXValue, ÖÐÐĵãÖµ 0.5±íʾÕâ¸öͼƬµÄÒ»°ëÖà * pivotYType, YÖÐÐĵãÀàÐÍAnimation.RELATIVE_TO_SELF×ÔÉí * pivotYValue 0.5f */ RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 3); //²¥·ÅÏÔʾʱ¼ä rotate.setDuration(5000); rotate.setRepeatCount(Animation.INFINITE); rotate.setRepeatMode(Animation.RESTART);
imageView.setAnimation(rotate);
rotate.start(); |
4.Ëõ·Å
/* * fromX, toX, ´Ó×ø±êʲôµØÖ·¿ªÊ¼µ½Ê²Ã´×ø±ê,0,±íʾµ±Ç° fromY, toY, * pivotXType, ÐýתµÄÖÐÐĵã * pivotXValue, ÀàÐÍ, pivotYType, pivotYValue */ ScaleAnimation scale = new ScaleAnimation(1, 2, 1, -2, Animation.RELATIVE_TO_SELF, -2, Animation.RELATIVE_TO_SELF, 3);
// ²¥·ÅÏÔʾʱ¼ä
scale.setDuration(5000);
scale.setRepeatCount(Animation.INFINITE);
scale.setRepeatMode(Animation.RESTART);
imageView.setAnimation(scale);
scale.start(); |
5.͸Ã÷¶È
/* * fromAlpha, ´Óʲô͸Ã÷¶È¿ªÊ¼ * toAlpha µ½Ê²Ã´Í¸Ã÷¶È½áÊø */ AlphaAnimation alpha = new AlphaAnimation(1, 0);
// ²¥·ÅÏÔʾʱ¼ä
alpha.setDuration(2000);
alpha.setRepeatCount(Animation.INFINITE);
alpha.setRepeatMode(Animation.REVERSE);
imageView.setAnimation(alpha);
alpha.start();
} |
6.¼¯ºÏ
¾ÍÊǽ¨Á¢Ò»¸ö
AnimationSet set = new AnimationSet(); set.addAnimation(scale); set.addAnimation(rotate); set.addAnimation(translate); set.start(); |
¾Í¿ÉÒÔ²¥·Å¶à¸ö¶¯»
7. XMLÅäÖÃ
ÔÚres/Ŀ¼Ï½¨Á¢animÎļþĿ¼
<!-- Èç¹ûÆ½ÒÆµÄÊýÖµÊÇÊý×Ö£¬ÄÇô±íʾµÄÊǾø¶ÔÖµ Èç¹ûÊǰٷֱȣ¬ÄÇô±íÃ÷ÊÇ×Ô¼ºµÄ¿í¸ßµÄ±¶Êý Èç¹ûÊǰٷֱȼÓÉÏ p£¬ÄÇô±íÃ÷ÊÇ×Ô¼ºµÄ¸¸ÔªËصĿí¸ßµÄ±¶Êý --> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:fromXDelta="200%"
android:toXDelta="200%"
android:fromYDelta="-100%"
android:toYDelta="200%"
android:duration="4000"
android:repeatCount="infinite"
android:repeatMode="reverse"/>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
android:repeatCount="infinite"
android:repeatMode="reverse"/>
</set> |
¼ÓÔØÊ¹ÓÃAnimationUtils.load·½·¨¼ÓÔØ
Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_set);
imageView.startAnimation(animation)
|
8.ÊôÐÔ¶¯»
Ò»°ã¶¼ÓôúÂë¿ØÖÆ
/** * ˮƽ * * @param v */ public void translate(View v) { /** * ²ÎÊýÒ»£º ËÈ¥²¥·ÅÕâ¸ö¶¯» ²ÎÊý¶þ£º ÊôÐÔÃû×Ö ÏëÒª¸Ä±äx·½ÏòµÄÒÆ¶¯ ²ÎÊýÈý£º¶¯»Ö´ÐеÄÊýÖµ */ ObjectAnimator objectAnimatrX = ObjectAnimator.ofFloat(imageView, "translationX", -50, 50); ObjectAnimator objectAnimatrY = ObjectAnimator.ofFloat(imageView, "translationY", 0, 60);
objectAnimatrX.setDuration(2000);
objectAnimatrX.setRepeatCount(ObjectAnimator.INFINITE);
objectAnimatrX.setRepeatMode(ObjectAnimator.REVERSE);
objectAnimatrX.start();
}
/**
* ת»»
*
* @param v
*/
public void rotate(View v) {
ObjectAnimator rotateX = ObjectAnimator.ofFloat(imageView,
"rotationX", 0,360);
ObjectAnimator rotateY = ObjectAnimator.ofFloat(imageView,
"rotationY", 0,360);
rotateX.setDuration(3000);
rotateX.setRepeatMode(Animation.REVERSE);
//rotateX.setRepeatCount(Animation.INFINITE);
rotateY.setDuration(3000);
rotateY.setRepeatMode(Animation.REVERSE);
//rotateY.setRepeatCount(Animation.INFINITE);
AnimatorSet set = new AnimatorSet();
set.playSequentially(rotateX,rotateY);
set.start();
}
/**
* ÕÀ·Å
*
* @param v
*/
public void scale(View v) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView,
"scaleX", 2.0f,5f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView,
"scaleY", 2.0f,5f);
scaleX.setDuration(3000);
scaleX.setRepeatMode(Animation.REVERSE);
scaleX.setRepeatCount(Animation.INFINITE);
scaleY.setDuration(3000);
scaleY.setRepeatMode(Animation.REVERSE);
scaleY.setRepeatCount(Animation.INFINITE);
AnimatorSet set = new AnimatorSet();
set.playTogether(scaleX,scaleY);
set.start();
}
/**
* ͸Ã÷
*
* @param v
*/
public void alpha(View v) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView,
"alpha", 1.0f,0f);
alpha.setDuration(3000);
alpha.setRepeatMode(Animation.REVERSE);
alpha.setRepeatCount(Animation.INFINITE);
alpha.start();
}
/**
* ¼¯ºÏ
*
* @param v
*/
public void set(View v) {
/**
* ²ÎÊýÒ»£º ËÈ¥²¥·ÅÕâ¸ö¶¯» ²ÎÊý¶þ£º ÊôÐÔÃû×Ö ÏëÒª¸Ä±äx·½ÏòµÄÒÆ¶¯ ²ÎÊýÈý£º¶¯»Ö´ÐеÄÊýÖµ
*/
ObjectAnimator objectAnimatrX = ObjectAnimator.ofFloat(imageView,
"translationX", -50, 100);
ObjectAnimator objectAnimatrY = ObjectAnimator.ofFloat(imageView,
"translationY", -70, 100);
objectAnimatrX.setDuration(2000);
//objectAnimatrX.setRepeatCount(ObjectAnimator.INFINITE);
objectAnimatrX.setRepeatMode(ObjectAnimator.REVERSE);
objectAnimatrY.setDuration(2000);
//objectAnimatrY.setRepeatCount(ObjectAnimator.INFINITE);
objectAnimatrY.setRepeatMode(ObjectAnimator.REVERSE);
AnimatorSet set = new AnimatorSet();
//set.playTogether(objectAnimatrX,objectAnimatrY);
set.playSequentially(objectAnimatrX,objectAnimatrY);
set.start();
}
|
Èç¹ûÐèÒª ÓÃXML¿ØÖÆ¿ÉÒÔÔÚres\Ͻ¨Á¢ animator,¼´¿É, ÔÚÕâ¸öĿ¼½¨Á¢XMLÎļþÈç¹û Ï·½·¨µ¼Èë
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.anim.property_animator); set.setTarget(myObject); set.start(); |
|