android 自訂動畫,android自訂

來源:互聯網
上載者:User

android 自訂動畫,android自訂

android自訂動畫注意是繼承Animation,重寫裡面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransformation方法進行相應的軌陣變化即可!

直接上demo!

public class Rotate3dAnimation extends Animation {
// 開始角度
private final float mFromDegrees;
// 結束角度
private final float mToDegrees;
// 中心點
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
// 是否需要扭曲
private final boolean mReverse;
// 網路攝影機
private Camera mCamera;

public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX,
float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}

@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}

// 產生Transformation
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
// 產生中間角度
float degrees = fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);

final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;

final Matrix matrix = t.getMatrix();

camera.save();
if (mReverse) {
//camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
camera.rotateY(mCenterY);
} else {
//camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
camera.rotateY(-mCenterY);
}
camera.rotateY(degrees);
// 取得變換後的矩陣
camera.getMatrix(matrix);
camera.restore();

matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

這是一個模仿3D旋轉動畫的效果,下面是調用方法,mSecondMenuLayout指的是要開始動畫的view

/**
*
* @param start 開始角度
* @param end 結束角度
*/
private void applyRotation(float start, float end) {

// 計算中心點
final float centerX = mSecondMenuLayout.getWidth() / 2.0f;
//final float centerY = mSecondMenuLayout.getHeight() /2.0f;
final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,
centerX, 360, 310.0f, true);
//設定動畫的期間
rotation.setDuration(1000);
//設定動畫完成後是否保留原樣
rotation.setFillAfter(false);
//設定動畫插補器
rotation.setInterpolator(new AccelerateDecelerateInterpolator());
//設定動畫監聽
rotation.setAnimationListener(mAnimListener);
mSecondMenuLayout.startAnimation(rotation);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.