Tween Animation:通过预先定义一组指令,指令指定了图形变幻的类型、触发时间、持续时间,对场景里的对象不断平移、缩放、旋转等变幻来产生动画效果。
Animation是以XML格式定义的,定义好的XML文件放在"res/anim"目录中。
在xml文件中,Tween动画主要包括以下四种动画效果:(箭头后是与之对应的java代码方法)
Alpha:渐变透明-------------->AlphaAnimation
Scale:伸缩 --------------->ScaleAnimation
Translate:移动--------------->TranslateAnimation
Rotate:旋转 ---------------->RotateAnimation
实现步骤:
1.定义Animation对象
2.设置动画的属性
3.通过startAnimation()方法开始动画
例子:
1.java代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ /**定义Alpha动画*/ private Animation animAlpha=null; /**定义Scale动画*/ private Animation animScale=null; /**定义Translate动画*/ private Animation animTranslate=null; /**定义Rotate动画*/ private Animation animRotate=null; private ImageView img; private TextView alpha,scale,translate,rotate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img=findViewById(R.id.iv_animation); alpha=findViewById(R.id.alpha); scale=findViewById(R.id.scale); translate=findViewById(R.id.translate); rotate=findViewById(R.id.rotate); alpha.setOnClickListener(this); scale.setOnClickListener(this); translate.setOnClickListener(this); rotate.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.alpha: /**创建Alpha动画*/ animAlpha=new AlphaAnimation(0.1f,1.0f); /**设置动画时间*/ animAlpha.setDuration(3000); /**开始播放动画*/ img.startAnimation(animAlpha); break; case R.id.scale: /**创建Scale动画*/ animScale=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f, Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF); /**设置动画时间*/ animScale.setDuration(5000); /**开始播放动画*/ img.startAnimation(animScale); break; case R.id.translate: /**创建Translate动画*/ animTranslate=new TranslateAnimation(10,100,10,100); /**设置动画时间*/ animTranslate.setDuration(1000); /**开始播放动画*/ img.startAnimation(animTranslate); break; case R.id.rotate: /**创建Translate动画*/ animRotate=new RotateAnimation(0.0f,+360.0f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); /**设置动画时间*/ animRotate.setDuration(1000); /**开始播放动画*/ img.startAnimation(animRotate); break; } } } 效果: alpha: scale: translate: rotate: =======================================================================================
/** * Constructor to use when building an AlphaAnimation from code * * @param fromAlpha Starting alpha value for the animation, where 1.0 means * fully opaque and 0.0 means fully transparent. * @param toAlpha Ending alpha value for the animation. */ public AlphaAnimation(float fromAlpha, float toAlpha) { mFromAlpha = fromAlpha; mToAlpha = toAlpha; }=======================================================================================
/** * Constructor to use when building a ScaleAnimation from code * * @param fromX Horizontal scaling factor to apply at the start of the * animation * @param toX Horizontal scaling factor to apply at the end of the animation * @param fromY Vertical scaling factor to apply at the start of the * animation * @param toY Vertical scaling factor to apply at the end of the animation * @param pivotX The X coordinate of the point about which the object is * being scaled, specified as an absolute number where 0 is the left * edge. (This point remains fixed while the object changes size.) * @param pivotY The Y coordinate of the point about which the object is * being scaled, specified as an absolute number where 0 is the top * edge. (This point remains fixed while the object changes size.) */ public ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) { mResources = null; mFromX = fromX; mToX = toX; mFromY = fromY; mToY = toY; mPivotXType = ABSOLUTE; mPivotYType = ABSOLUTE; mPivotXValue = pivotX; mPivotYValue = pivotY; initializePivotPoint(); }=====================================================================================
/** * Constructor to use when building a TranslateAnimation from code * * @param fromXDelta Change in X coordinate to apply at the start of the * animation * @param toXDelta Change in X coordinate to apply at the end of the * animation * @param fromYDelta Change in Y coordinate to apply at the start of the * animation * @param toYDelta Change in Y coordinate to apply at the end of the * animation */ public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) { mFromXValue = fromXDelta; mToXValue = toXDelta; mFromYValue = fromYDelta; mToYValue = toYDelta; mFromXType = ABSOLUTE; mToXType = ABSOLUTE; mFromYType = ABSOLUTE; mToYType = ABSOLUTE; }========================================================================================
/** * Constructor to use when building a RotateAnimation from code * * @param fromDegrees Rotation offset to apply at the start of the * animation. * * @param toDegrees Rotation offset to apply at the end of the animation. * * @param pivotXType Specifies how pivotXValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param pivotXValue The X coordinate of the point about which the object * is being rotated, specified as an absolute number where 0 is the * left edge. This value can either be an absolute number if * pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) * otherwise. * @param pivotYType Specifies how pivotYValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param pivotYValue The Y coordinate of the point about which the object * is being rotated, specified as an absolute number where 0 is the * top edge. This value can either be an absolute number if * pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) * otherwise. */ public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mPivotXValue = pivotXValue; mPivotXType = pivotXType; mPivotYValue = pivotYValue; mPivotYType = pivotYType; initializePivotPoint(); }==========================================================
学习心得:1.必须要学好英语
2.不会用的类按CRL键单击去看这个类的方法如何去用,学英语还是重点的重点