詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無(wú)法接收到Touch事件的問(wèn)題
詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無(wú)法接收到Touch事件的問(wèn)題
今天在項(xiàng)目中實(shí)現(xiàn)了一個(gè)效果,主要是畫(huà)一個(gè)圓。為了后續(xù)使用方便,將這個(gè)圓封裝在一個(gè)自定義Actor(CircleActot)中,后續(xù)想顯示一個(gè)圓的時(shí)候,只要?jiǎng)?chuàng)建一個(gè)CircleActor中即可。 部分代碼如下所示:
package com.ef.smallstar.unitmap.widget; import android.content.res.Resources; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.scenes.scene2d.Actor; import com.ef.smallstar.EFApplication; import com.ef.smallstar.R; /** * Created by ext.danny.jiang on 17/4/17. * * A Widget currently used in the UnitMap, shown as a CIRCLE shape * if text not null, there would be a text drawn in the center of the circle */ public class CircleActor extends Actor { private float centerX; private float centerY; private String text; private float radius; private ShapeRenderer sr; private BitmapFont bitmapFont; public CircleActor(float x, float y, float radius) { this(x, y, radius, null); } public CircleActor(float x, float y, float radius, String text) { this.centerX = x; this.centerY = y; this.radius = radius; this.text = text; sr = new ShapeRenderer(); } @Override public void act(float delta) { super.act(delta); } @Override public void draw(Batch batch, float parentAlpha) { ... batch.end(); sr.setProjectionMatrix(batch.getProjectionMatrix()); sr.setTransformMatrix(batch.getTransformMatrix()); sr.begin(ShapeRenderer.ShapeType.Filled); sr.circle(centerX, centerY, radius); sr.end(); batch.begin(); ... }
然后創(chuàng)建一個(gè)Stage對(duì)象,并將CircleActor對(duì)象添加到Stage中即可顯示。 但是無(wú)法給此CircleActor對(duì)象添加一個(gè)ClickLitener監(jiān)聽(tīng)。
例如如下代碼:
Stage stage = new Stage(); CircleActor ca = new CircleActor(100, 100, 50, "Hello World"); ca.addListener(new ClickListener(){ public void click(){ Gdx.app.log("TAG", "ca is clicked"); } }) stage.add(ca);
上述代碼中的click方法永遠(yuǎn)無(wú)法被調(diào)用! 后續(xù)調(diào)了大半天之后終于弄清楚了原因:雖然在CircleActor的draw方法中通過(guò)ShapeRenderer.circle方法將一個(gè)圓畫(huà)到了屏幕上的某一位置,但是此ShapeRenderer其實(shí)和Actor之間并沒(méi)有太多的聯(lián)系。唯一的聯(lián)系就是以下兩句代碼, 意思應(yīng)該是將ShapeRenderer的camera和Actor對(duì)象一致。
sr.setProjectionMatrix(batch.getProjectionMatrix()); sr.setTransformMatrix(batch.getTransformMatrix());
但是此時(shí),CircleActor并沒(méi)有設(shè)置真正的大小與位置, 因此解決上述問(wèn)題,需要在構(gòu)造器中將CircleActor的大小和位置與ShapeRenderer做到一致 !!
如下代碼所示,只要添加兩行代碼即可:
public EfCircle(float x, float y, float radius, String text) { this.centerX = x; this.centerY = y; this.radius = radius; this.text = text; //解決ShapeRenderer無(wú)法獲取Touch事件 setPosition(centerX - radius, centerY - radius); setSize(radius * 2, radius * 2); sr = new ShapeRenderer(); }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 詳解Android Libgdx中ScrollPane和Actor事件沖突問(wèn)題的解決辦法
- Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動(dòng)的方法
- Android 游戲引擎libgdx 資源加載進(jìn)度百分比顯示案例分析
- Android drawable微技巧,你不知道的drawable細(xì)節(jié)
- Android指紋識(shí)別API講解,一種更快更好的用戶體驗(yàn)
- Android在Kotlin中更好地使用LitePal
- Android Studio輕松構(gòu)建自定義模板的步驟記錄
- 詳解Android 檢測(cè)權(quán)限的三種寫(xiě)法
- Android最簡(jiǎn)單的狀態(tài)切換布局實(shí)現(xiàn)教程
- android自定義環(huán)形對(duì)比圖效果
- Libgdx解決部分Android機(jī)型鎖屏崩潰的方法
相關(guān)文章
Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼
這篇文章主要介紹了Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼,調(diào)節(jié)屏幕亮度時(shí),先設(shè)置當(dāng)前activity亮度,再并保存為系統(tǒng)亮度即可,本文分別給出兩個(gè)步驟的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05Android實(shí)現(xiàn)為L(zhǎng)istView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景
這篇文章主要介紹了Android實(shí)現(xiàn)為L(zhǎng)istView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景,以實(shí)例形式較為詳細(xì)的分析了界面元素與功能的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02Android自定義時(shí)間軸的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了Android自定義時(shí)間軸的實(shí)現(xiàn)過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android listView 繪制表格實(shí)例詳解
這篇文章主要介紹了Android listView 繪制表格實(shí)例詳解的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,利用listView 繪制表格提供實(shí)現(xiàn)思路,需要的朋友可以參考下2017-01-01Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時(shí)出現(xiàn)空指針(NullPointerException)問(wèn)題的解決
這篇文章主要介紹了Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時(shí)出現(xiàn)空指針(NullPointerException)問(wèn)題的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Android應(yīng)用 坐標(biāo)系詳細(xì)介紹
這篇文章主要介紹了 Android 坐標(biāo)系的相關(guān)資料,本文對(duì)Android 坐標(biāo)系的知識(shí)做了詳細(xì)解讀,需要的朋友可以參考下2016-11-11Android實(shí)現(xiàn)仿慕課網(wǎng)下拉加載動(dòng)畫(huà)
這篇文章是我在做動(dòng)畫(huà)的項(xiàng)目中整理出來(lái)的,在eoe看了篇帖子,然后仿慕課網(wǎng)做了一個(gè)下拉加載動(dòng)畫(huà)。此功能實(shí)現(xiàn)方法是AnimationDrawable類進(jìn)行 Animation-list中item的循環(huán)遍歷圖片,類似于flash里的幀幀動(dòng)畫(huà),需要的朋友可以參考下2015-07-07