Android-android如何通过触屏事件而不是随时间变化来播放动画[非逐帧动画]帧?

Android-android如何通过触屏事件而不是随时间变化来播放动画[非逐帧动画]帧?

晚风撩人 发布于 2017-02-22 字数 291 浏览 1196 回复 2

一般的动画都是通过将animation对象setduration设置好动画总播放时间,然后在用关联的view对象的startanimation方法来启动,动画结束时间由之前设好的播放时间来决定,中间过程是不能随意控制的,比如现在想通过滑屏事件向前或者先后跳一帧,动画的结束也可以做到由任意事件来控制。注意要求是非逐帧动画。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚风撩人 2017-10-20 2 楼

侦听 TOUCHMOVE 事件

MOVIE.PLAY()
侦听 TOUCHUP 或其他事件 MOVIE.STOP()

瑾兮 2017-05-10 1 楼

继承重写animation类,可以实现此功能。
动画的启动方式和原接口一样。不一样的地方在于问题提出来的地方,
可以调用函数控制动画的播放的位置。

public class ManualAnimation extends Animation {
protected float mInterpolation;
public final static int AUTO_MODE = 1;
public final static int TOUCH_MODE = 2;
protected int mMode = AUTO_MODE;
protected boolean mStarted = false;
protected boolean mEnded;
protected float mFromAlpha;
protected float mToAlpha;
protected float mStart;
protected float mEnd;
//调用这个接口控制动画的进度1表示结束了。
public void setInterpolation(float Interpolation){
mInterpolation = Interpolation;
}

public void setMode(int Mode){
mMode = Mode;
}

public void setStartAndEnd(float start, float end){
mStart = start;
mEnd = end;
if(mEnd != 0){
mToAlpha = 0.5f;
mFromAlpha = 1.0f;
}else{
mToAlpha = 1.0f;
mFromAlpha = 0.5f;
}
}

public boolean hasStarted() {
return mStarted;
}

public boolean hasEnded() {
return mEnded;
}

public void cancel(){
mEnded = true;
super.cancel();
}
}

public class Cube3dAnimation extends ManualAnimation {

 private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
private final float mTranslateX;
private final float mTranslateY;
private final boolean mReverse;
private final float mTranslateZ;
private Camera mCamera;
private final float mDegreeFactor = 90;

public Cube3dAnimation(float start, float end,
float centerX, float centerY, float translateX, float translateY, float translateZ, float depthZ, boolean reverse) {
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
mTranslateX = translateX;
mTranslateY = translateY;
mTranslateZ = translateZ;
setStartAndEnd(start, end);
}

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

//继承这个函数来自定义动画的变换和实现
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mStart;
float degrees = fromDegrees + ((mEnd - 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, 0.0f, mDepthZ);
} else {
camera.translate(0, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}

camera.rotateY(degrees);

if (mReverse) {
camera.translate(0, 0.0f, -mDepthZ );
} else {
camera.translate(0, 0.0f, -(mDepthZ * (1.0f - interpolatedTime)));
}

camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX + mTranslateX, centerY);

/*
final float alpha = mFromAlpha;
t.setAlpha(alpha + ((mToAlpha - alpha) * interpolatedTime));
*/

}
//继承这个基类函数,通过返回true表示动画继续,否则动画结束
public boolean getTransformation(long currentTime, Transformation outTransformation) {
if(mMode == AUTO_MODE){
return super.getTransformation(currentTime, outTransformation);
}
if(mInterpolation >= 1.0f || hasEnded()){
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = outTransformation.getMatrix();
camera.getMatrix(matrix);
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
return false;
//return super.getTransformation(Long.MAX_VALUE, outTransformation);
}
mStarted = true;
applyTransformation(mInterpolation, outTransformation);
return true;
}

public void setStartAndEnd(float start, float end){
super.setStartAndEnd(startmDegreeFactor, endmDegreeFactor);
}
}