小編給大家分享一下如何解決Android Libgdx中ScrollPane和Actor事件沖突問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)公司主營沂水網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),沂水h5成都微信小程序搭建,沂水網(wǎng)站營銷推廣歡迎沂水等地區(qū)企業(yè)咨詢
在Libgdx的使用過程中,經(jīng)常會用到ScrollPane這個widget,來實現(xiàn)滑動效果, 如下所示:
但是如果想在上面的效果上添加一點擴展,比如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, float x, float y, 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, float x, float y, int pointer, int button) { Log.e("DANNY", "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 x, 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, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } }
實現(xiàn)效果如下:
以上是“如何解決Android Libgdx中ScrollPane和Actor事件沖突問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:如何解決AndroidLibgdx中ScrollPane和Actor事件沖突問題
網(wǎng)頁URL:http://jinyejixie.com/article18/jjpsdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站營銷、全網(wǎng)營銷推廣、定制開發(fā)、用戶體驗、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)