自定义动画,实现矩形切割
1.使用
Image image = new Image(Asset.getAsset().getTexture("1_75_43.png"));
addActor(image);
ClipAnimation animation = new ClipAnimation(image.getHeight(),0,10,Interpolation.bounce);
image.addAction(animation);
自定义代码
public class ClipAnimation extends TemporalAction {
private Group srcParentGroup;
private Group tempParentGroup;
private float middleValue = 0;
private float startValue = 0;
private float endValue = 0;
public ClipAnimation(float startValue,float endValue,float duration,Interpolation interpolation){
setStartValue(startValue);
setEndValue(endValue);
setDuration(duration);
setInterpolation(interpolation);
}
@Override
public void setActor(Actor actor) {
super.setActor(actor);
if (actor==null)return;
srcParentGroup = target.getParent();
tempParentGroup = new Group(){
@Override
public void draw(Batch batch, float parentAlpha) {
batch.flush();
if (clipBegin(0,0, tempParentGroup.getWidth(), tempParentGroup.getHeight()- middleValue)) {
super.draw(batch, parentAlpha);
batch.flush();
clipEnd();
}
}
};
tempParentGroup.addActor(actor);
srcParentGroup.addActor(tempParentGroup);
tempParentGroup.setSize(actor.getWidth(),actor.getHeight());
tempParentGroup.setPosition(actor.getX(Align.center),actor.getY(Align.center),Align.center);
actor.setPosition(tempParentGroup.getWidth()/2, tempParentGroup.getHeight()/2,Align.center);
tempParentGroup.setDebug(true);
}
@Override
public boolean act(float delta) {
boolean act = super.act(delta);
if (act){
srcParentGroup.addActor(actor);
actor.setPosition(tempParentGroup.getX(Align.center), tempParentGroup.getY(Align.center),Align.center);
}
return act;
}
public void setStartValue(float startValue) {
this.startValue = startValue;
}
public void setEndValue(float endValue) {
this.endValue = endValue;
}
@Override
protected void update(float v) {
float v1 = endValue - startValue;
middleValue = startValue + v1 * v;
}
}