可以使用补间进行缩放吗?-LibGdx
Is scaling possible with tween ?-LibGdx
有什么方法可以用补间动画缩放演员吗?
我有这样一张演员图
image1 = new Image(texture1);
stage.addActor(image1);
想让它缩放一段时间然后恢复到原来的大小。
可以用补间吗?
是 tween engine.
是可能的
根据 Aurelien Ribon 的博客:
补间引擎的唯一限制是您的想象力。
题外话
为什么 universal-tween-engine
和 LibGDX 如果你使用的是 scene2d 框架
内置 Action API 用于补间。
编辑
public class ActorAccessor implements TweenAccessor<Actor> {
public static final int POS_XY = 1;
public static final int SCALE_XY = 2;
@Override
public int getValues(Actor target, int tweenType, float[] returnValues) {
switch (tweenType) {
case POS_XY:
returnValues[0] = target.getX();
returnValues[1] = target.getY();
return 2;
case SCALE_XY:
returnValues[0] = target.getScaleX();
returnValues[1] = target.getScaleY();
return 2;
default: assert false; return -1;
}
}
@Override
public void setValues(Actor target, int tweenType, float[] newValues) {
switch (tweenType) {
case POS_XY: target.setPosition(newValues[0], newValues[1]); break;
case SCALE_XY: target.setScale(newValues[0], newValues[1]); break;
default: assert false;
}
}
}
通过您的 Actor 的注册和补间缩放值将此 ActorAccessor
与 Actor
绑定。
编辑 2
TweenManager tweenManager=new TweenManager();
Tween.registerAccessor(Actor.class,new ActorAccessor());
Image image=new Image(new Texture("badlogic.jpg"));
image.setOrigin(image.getWidth()/2,image.getHeight()/2); // required for scaling from center
image.setPosition(200,100);
stage.addActor(image);
float scale_up_dur=0.4f;
float scale_factor=2 ; // 200%
float pause_after_scale=3;
float scale_down_dur= 0.4f;
Timeline.createSequence().delay(2f).push(Tween.to(image, ActorAccessor.SCALE_XY,scale_up_dur).target(scale_factor, scale_factor)).pushPause(pause_after_scale).push(Tween.to(image, ActorAccessor.SCALE_XY, scale_down_dur).target(1, 1)).start(tweenManager);
在render()
方法中
tweenManager.update(Gdx.graphics.getDeltaTime());
在 libgdx 中使用补间缩放 actor。
package com.vector.science11;
import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
public class Test3Dline extends ApplicationAdapter {
private SpriteBatch batch;
private OrthographicCamera orthoCamera;
private TweenManager tweenManager;
public Stage stage;
private Color bgColor;
SpriteDrawable spriteDrawable;
@Override
public void create() {
orthoCamera = new OrthographicCamera(960, 540);
orthoCamera.position.set(960 / 2, 540 / 2, 0);
orthoCamera.update();
bgColor = Color.valueOf("ffffff");
tweenManager = new TweenManager();
stage = new Stage();
stage.getViewport().setCamera(orthoCamera);
batch = new SpriteBatch();
Tween.registerAccessor(Actor.class, new ActorAccessors());
Image image1 = new Image(createTexture(50, 30, Color.RED, 1));
image1.setPosition(100, 100);
stage.addActor(image1);
Gdx.input.setInputProcessor(stage);
Timeline.createSequence()
.delay(2f)
.push(Tween.to(image1, ActorAccessors.SCALE_XY, 0.4f).target(2,
2))
.pushPause(3f)
.push(Tween.to(image1, ActorAccessors.SCALE_XY, 0.4f).target(1,
1)).start(tweenManager);
}
@Override
public void render() {
tweenManager.update(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(orthoCamera.combined);
stage.draw();
stage.act(Gdx.graphics.getDeltaTime());
// Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
}
public static Texture createTexture(int width, int height, Color col,
float alfa) {
Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
Color color = col;
pixmap.setColor(color.r, color.g, color.b, alfa);
pixmap.fillRectangle(0, 0, width, height);
Texture pixmaptexture = new Texture(pixmap);
return pixmaptexture;
}
}
并使用 Scene2D API,操作方法:
package com.vector.science11;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
public class Test3Dline extends ApplicationAdapter {
private SpriteBatch batch;
private OrthographicCamera orthoCamera;
public Stage stage;
private Color bgColor;
SpriteDrawable spriteDrawable;
@Override
public void create() {
orthoCamera = new OrthographicCamera(960, 540);
orthoCamera.position.set(960 / 2, 540 / 2, 0);
orthoCamera.update();
bgColor = Color.valueOf("ffffff");
stage = new Stage();
stage.getViewport().setCamera(orthoCamera);
batch = new SpriteBatch();
Image image1 = new Image(createTexture(50, 30, Color.RED, 1));
image1.setPosition(100, 100);
stage.addActor(image1);
image1.addAction(Actions.sequence(Actions.scaleTo(2, 2, 2), // scale to
// 2 times
// of actual
// size
Actions.scaleTo(1, 1, 2) // come to original size
));
}
@Override
public void render() {
Gdx.gl.glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(orthoCamera.combined);
stage.draw();
stage.act(Gdx.graphics.getDeltaTime());
}
public static Texture createTexture(int width, int height, Color col,
float alfa) {
Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
Color color = col;
pixmap.setColor(color.r, color.g, color.b, alfa);
pixmap.fillRectangle(0, 0, width, height);
Texture pixmaptexture = new Texture(pixmap);
return pixmaptexture;
}
}
我知道这不是你要问的,但我可能会继续向你展示使用 Actions 是多么容易,也许你会重新考虑使用 tween
image1 = new Image(texture1);
stage.addActor(image1);
image1.addAction(
Actions.sequence(
Actions.sizeTo(scaledWidth, scaledHeight, durationInSecs),
Actions.sizeTo(originalWidth, originalHeight, durationInSecs)
)
);
或
image1 = new Image(texture1);
stage.addActor(image1);
image1.setOrigin(Align.center);
image1.addAction(
Actions.sequence(
Actions.scaleTo(2,2,durationInSecs), //Scale to 200% here
Actions.scaleTo(1,1,durationInSecs) //Scale to 100% (original size)
)
);
工作示例:
public class MainClass extends ApplicationAdapter {
private Texture tex;
private Image image;
private Stage stage;
@Override
public void create () {
stage = new Stage();
tex = new Texture("badlogic.jpg");
image = new Image(tex);
image.setSize(100,100);
image.setPosition(0,0);
float durationInSecs = 1;
image.addAction(
Actions.sequence(
Actions.sizeTo(200,200,durationInSecs),
Actions.sizeTo(100,100,durationInSecs)
)
);
stage.addActor(image);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void dispose () {
tex.dispose();
stage.dispose();
}
}
有什么方法可以用补间动画缩放演员吗?
我有这样一张演员图
image1 = new Image(texture1);
stage.addActor(image1);
想让它缩放一段时间然后恢复到原来的大小。
可以用补间吗?
是 tween engine.
是可能的根据 Aurelien Ribon 的博客: 补间引擎的唯一限制是您的想象力。
题外话
为什么 universal-tween-engine
和 LibGDX 如果你使用的是 scene2d 框架
内置 Action API 用于补间。
编辑
public class ActorAccessor implements TweenAccessor<Actor> {
public static final int POS_XY = 1;
public static final int SCALE_XY = 2;
@Override
public int getValues(Actor target, int tweenType, float[] returnValues) {
switch (tweenType) {
case POS_XY:
returnValues[0] = target.getX();
returnValues[1] = target.getY();
return 2;
case SCALE_XY:
returnValues[0] = target.getScaleX();
returnValues[1] = target.getScaleY();
return 2;
default: assert false; return -1;
}
}
@Override
public void setValues(Actor target, int tweenType, float[] newValues) {
switch (tweenType) {
case POS_XY: target.setPosition(newValues[0], newValues[1]); break;
case SCALE_XY: target.setScale(newValues[0], newValues[1]); break;
default: assert false;
}
}
}
通过您的 Actor 的注册和补间缩放值将此 ActorAccessor
与 Actor
绑定。
编辑 2
TweenManager tweenManager=new TweenManager();
Tween.registerAccessor(Actor.class,new ActorAccessor());
Image image=new Image(new Texture("badlogic.jpg"));
image.setOrigin(image.getWidth()/2,image.getHeight()/2); // required for scaling from center
image.setPosition(200,100);
stage.addActor(image);
float scale_up_dur=0.4f;
float scale_factor=2 ; // 200%
float pause_after_scale=3;
float scale_down_dur= 0.4f;
Timeline.createSequence().delay(2f).push(Tween.to(image, ActorAccessor.SCALE_XY,scale_up_dur).target(scale_factor, scale_factor)).pushPause(pause_after_scale).push(Tween.to(image, ActorAccessor.SCALE_XY, scale_down_dur).target(1, 1)).start(tweenManager);
在render()
方法中
tweenManager.update(Gdx.graphics.getDeltaTime());
在 libgdx 中使用补间缩放 actor。
package com.vector.science11;
import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
public class Test3Dline extends ApplicationAdapter {
private SpriteBatch batch;
private OrthographicCamera orthoCamera;
private TweenManager tweenManager;
public Stage stage;
private Color bgColor;
SpriteDrawable spriteDrawable;
@Override
public void create() {
orthoCamera = new OrthographicCamera(960, 540);
orthoCamera.position.set(960 / 2, 540 / 2, 0);
orthoCamera.update();
bgColor = Color.valueOf("ffffff");
tweenManager = new TweenManager();
stage = new Stage();
stage.getViewport().setCamera(orthoCamera);
batch = new SpriteBatch();
Tween.registerAccessor(Actor.class, new ActorAccessors());
Image image1 = new Image(createTexture(50, 30, Color.RED, 1));
image1.setPosition(100, 100);
stage.addActor(image1);
Gdx.input.setInputProcessor(stage);
Timeline.createSequence()
.delay(2f)
.push(Tween.to(image1, ActorAccessors.SCALE_XY, 0.4f).target(2,
2))
.pushPause(3f)
.push(Tween.to(image1, ActorAccessors.SCALE_XY, 0.4f).target(1,
1)).start(tweenManager);
}
@Override
public void render() {
tweenManager.update(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(orthoCamera.combined);
stage.draw();
stage.act(Gdx.graphics.getDeltaTime());
// Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
}
public static Texture createTexture(int width, int height, Color col,
float alfa) {
Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
Color color = col;
pixmap.setColor(color.r, color.g, color.b, alfa);
pixmap.fillRectangle(0, 0, width, height);
Texture pixmaptexture = new Texture(pixmap);
return pixmaptexture;
}
}
并使用 Scene2D API,操作方法:
package com.vector.science11;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
public class Test3Dline extends ApplicationAdapter {
private SpriteBatch batch;
private OrthographicCamera orthoCamera;
public Stage stage;
private Color bgColor;
SpriteDrawable spriteDrawable;
@Override
public void create() {
orthoCamera = new OrthographicCamera(960, 540);
orthoCamera.position.set(960 / 2, 540 / 2, 0);
orthoCamera.update();
bgColor = Color.valueOf("ffffff");
stage = new Stage();
stage.getViewport().setCamera(orthoCamera);
batch = new SpriteBatch();
Image image1 = new Image(createTexture(50, 30, Color.RED, 1));
image1.setPosition(100, 100);
stage.addActor(image1);
image1.addAction(Actions.sequence(Actions.scaleTo(2, 2, 2), // scale to
// 2 times
// of actual
// size
Actions.scaleTo(1, 1, 2) // come to original size
));
}
@Override
public void render() {
Gdx.gl.glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(orthoCamera.combined);
stage.draw();
stage.act(Gdx.graphics.getDeltaTime());
}
public static Texture createTexture(int width, int height, Color col,
float alfa) {
Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
Color color = col;
pixmap.setColor(color.r, color.g, color.b, alfa);
pixmap.fillRectangle(0, 0, width, height);
Texture pixmaptexture = new Texture(pixmap);
return pixmaptexture;
}
}
我知道这不是你要问的,但我可能会继续向你展示使用 Actions 是多么容易,也许你会重新考虑使用 tween
image1 = new Image(texture1);
stage.addActor(image1);
image1.addAction(
Actions.sequence(
Actions.sizeTo(scaledWidth, scaledHeight, durationInSecs),
Actions.sizeTo(originalWidth, originalHeight, durationInSecs)
)
);
或
image1 = new Image(texture1);
stage.addActor(image1);
image1.setOrigin(Align.center);
image1.addAction(
Actions.sequence(
Actions.scaleTo(2,2,durationInSecs), //Scale to 200% here
Actions.scaleTo(1,1,durationInSecs) //Scale to 100% (original size)
)
);
工作示例:
public class MainClass extends ApplicationAdapter {
private Texture tex;
private Image image;
private Stage stage;
@Override
public void create () {
stage = new Stage();
tex = new Texture("badlogic.jpg");
image = new Image(tex);
image.setSize(100,100);
image.setPosition(0,0);
float durationInSecs = 1;
image.addAction(
Actions.sequence(
Actions.sizeTo(200,200,durationInSecs),
Actions.sizeTo(100,100,durationInSecs)
)
);
stage.addActor(image);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void dispose () {
tex.dispose();
stage.dispose();
}
}