详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法

详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法,第1张

概述详解AndroidLibgdx中ScrollPane和Actor事件冲突问题的解决办法在Libgdx的使用过程中,经常会用到ScrollPane这个widget,来实现滑动效果,如下所示:

详解AndroID libgdx中ScrollPane和Actor事件冲突问题的解决办法

在libgdx的使用过程中,经常会用到ScrollPane这个Widget,来实现滑动效果, 如下所示:

但是如果想在上面的效果上添加一点扩展,比如ScrollPane中的Actor可以从ScrollPane中移出来,并添加到Stage中,则需要添加额外的逻辑

具体代码参考如下:

/** * Created by Danny.姜 on 17/7/26. */public class TestAdapter extends ApplicationAdapter {  Stage stage;  ScrollPane scrollPane;  table table;  private float lastStageX;  private float lastStageY;  ActorGestureListener actorGestureListener;  private inputListener inputListener = new inputListener() {    @OverrIDe    public boolean touchDown(inputEvent event,float x,float y,int pointer,int button) {      Log.e("DANNY","actor touch down");      if (pointer == 0) {        lastStageX = event.getStageX();        lastStageY = event.getStageY();        inStage = false;      }      return true;    }    @OverrIDe    public voID touchDragged(inputEvent event,int pointer) {      Log.e("DANNY","actor touch dragging");      Actor actor = event.getListenerActor();      if (canceltouchFocus) {        float currentX = event.getStageX();        float currentY = event.getStageY();        actor.moveBy(currentX - lastStageX,currentY - lastStageY);        lastStageX = currentX;        lastStageY = currentY;        if (!inStage) {          Vector2 vector2 = actor.localToStageCoordinates(new Vector2());          actor.setposition(vector2.x,vector2.y);          stage.addActor(actor);          stage.canceltouchFocusExcept(this,actor);          inStage = true;        }      }    }    @OverrIDe    public voID touchUp(inputEvent event,"actor touch up");      canceltouchFocus = false;    }  };  @OverrIDe  public voID create() {    super.create();    stage = new Stage();    stage.setDebugall(true);    Gdx.input.setinputProcessor(stage);    actorGestureListener = new ActorGestureListener() {      @OverrIDe      public boolean longPress(Actor actor,float y) {        Log.e("DANNY","actor gesture Listener long pressed");        canceltouchFocus = true;        return super.longPress(actor,x,y);      }    };    table = new table();    scrollPane = new ScrollPane(table,new ScrollPane.ScrollPanestyle());    scrollPane.setSize(stage.getWIDth(),500);    scrollPane.setScrollingDisabled(false,true);    scrollPane.setCanceltouchFocus(false);    scrollPane.setSmoothScrolling(true);    scrollPane.setFlingTime(0);    stage.addActor(scrollPane);    inittable();  }  private boolean canceltouchFocus = false;  private boolean inStage = false;  private voID inittable() {    for (int i = 0; i < 10; i++) {      BaseGdxActor actor = new BaseGdxActor(new TextureRegion(          new Texture("badlogic.jpg")));      table.add(actor).wIDth(actor.getWIDth()).pad(10);      actor.addListener(actorGestureListener);      actor.addListener(inputListener);    }  }  @OverrIDe  public voID render() {    super.render();    Gdx.gl.glClearcolor(1,1,1);    Gdx.gl.glClear(GL20.GL_color_BUFFER_BIT);    stage.act();    stage.draw();  }}

实现效果如下:

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法全部内容,希望文章能够帮你解决详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144561.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存