Android开发动画篇(一) View动画

View动画特点 :

    使用简单,点击事件的位置不会跟随移动

分类和属性(Animation): 

共用属性 : 

         duration --------- 动画持续时间

         fillAffter --------- 动画结束后  是否停留在结束位置


   1. TranslateAnimation(平移)

     

          android: formXDelta = "float"

          android: toXDelta = "float"

         android: formYDelta = "float"

         android: toYDelta = "float"

      />


   2. ScaleAnimation(缩放)

       

         android: formXDelta = "float"

         android: toXDelta = "float"

         android: formYDelta = "float"

        android: toYDelta = "float"

        android: pivotX = "float"

        android: pivotY = "float"

       />


   3. RotateAnimation(旋转)

     

        android: fromDegrees = "float"

        android:toDegrees = "float"

        android: pivotX = "float"

        android: pivotY = "float"

/>


  4. AlpleAnimation(透明度) 

     

          android: fromAlple = "float"

         android:toAlple = "float"

    />

使用方式:

Button button = findViewById(R.id.btn);

button.startAnimation(AnimationUtils.loadAnimation(this,R.anim.translate););


自定义View动画 

  继承Animation ,重写 initialize 和 applyTransformation , initialize 中进行初始化操作,applyTransformation 中进行矩阵变换操作,矩阵变换使用Camera简化实现

public classRotate3dAnimationextendsAnimation {

  private final floatmFromDegress;

  private final floatmToDegress;

  private final floatmCenterX;

  private final floatmCenterY;

  private final floatmDepthZ;

  private final booleanmReverse;

  private Camera camera;

 public Rotate3dAnimation (floatmFromDegress, floatmToDegress, floatmCenterX,                                                                 floatmCenterY, floatmDepthZ, booleanmReverse) {

         this.mFromDegress= mFromDegress;

         this.mToDegress= mToDegress;

         this.mCenterX= mCenterX;

        this.mCenterY= mCenterY;

       this.mDepthZ= mDepthZ;

       this.mReverse= mReverse;

  }

@Override

public voidinitialize(intwidth, intheight, intparentWidth, intparentHeight) {

super.initialize(width,height,parentWidth,parentHeight);

     camera=newCamera();

}

@Override

protected voidapplyTransformation(floatinterpolatedTime,Transformation t) {

final floatfromDegress  =mFromDegress;

floatdegrees = fromDegress + ((mToDegress- fromDegress) * interpolatedTime);

final floatcenterX =mCenterX;

final floatcenterY =mCenterY;

finalCamera camera =this.camera;

finalMatrix matrix = t.getMatrix();

camera.save();

if(mReverse){

camera.translate(0.0f,0.0f,mDepthZ*interpolatedTime);

}else{

camera.translate(0.0f,0.0f,mDepthZ*(1.0f- interpolatedTime));

}

camera.rotateY(degrees);

camera.getMatrix(matrix);

camera.restore();

matrix.preTranslate(-centerX,-centerY);

matrix.postTranslate(centerX,centerY);

}

}


帧动画

 

/>

使用方式:

View view = new View;

view.setBackgroundResource(R.drawable.animation);

AnimationDrawable drawable = view.getBackground();

drawable.start();

你可能感兴趣的:(Android开发动画篇(一) View动画)