最近在做一下項目,是領取紅包的,我們這裡仿照的微信的紅包領取界面,點選按鈕之後立體旋轉,然後打開紅包:
我這裡用的是旋轉動畫,然後沿着Y軸去旋轉,說了這麼多,下面來看一下效果:
現在清楚多了把 ,我用自定義了一個動畫,找到控件的中心,然後讓中心沿着Y軸去旋轉,不多說了看代碼吧:
package com.lixuce.myapplication;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
/**
* Created by lixuce on 2017/4/10.
*/
public class MyAnimation extends Animation {
int centerX, centerY;
Camera camera = new Camera();
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
//擷取中心點坐标
centerX = width/ 2;
centerY = height / 2;
//動畫執行時間 自行定義
setDuration(2500L);
setInterpolator(new AccelerateInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final Matrix matrix = t.getMatrix();
camera.save();
//中心是繞Y軸旋轉 這裡可以自行設定X軸 Y軸 Z軸
camera.rotateY(1080 * interpolatedTime);
//把我們的攝像頭加在變換矩陣上
camera.getMatrix(matrix);
//設定翻轉中心點
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
camera.restore();
}
}
用法:
MyAnimation mAnimation = new MyAnimation();
ivxuanzhuan.startAnimation(mAnimation);
這樣是不是很容易就實作了動畫了效果了。希望能對大家有所幫助