gpt4 book ai didi

安卓/玻璃 : How to trigger surfaceDestroyed

转载 作者:行者123 更新时间:2023-11-30 03:09:09 33 4
gpt4 key购买 nike

大家好,我是 android/glass 开发的新手,所以如果我错了请纠正我。

我尝试创建一个很好的高频实时卡片。然而,当我关闭应用程序时,函数 surfaceDestroyed() 没有得到触发并且 thread.quit() 没有被调用。

我尝试查看示例项目秒表/计时器,显然它们也没有停止线程。根据文档,surfaceDestroyed 在表面被破坏之前被调用,但我的表面是什么?我还读过一些叫做surfaceview的东西,它们是同一回事吗? surfaceview 是我的 customview 还是 livecard?

https://developers.google.com/glass/develop/gdk/ui/live-cards#creating_high-frequency_live_cards

感谢任何形式的帮助!!!

/ ***
* LiveCardRender Class
*** /
public class LiveCardRender implements DirectRenderingCallback {
private static final long FRAME_TIME_MILLIS = 33;
private CustomView mCustomView;
private SurfaceHolder mHolder;
private boolean mPaused;
private RenderThread mRenderThread;

private class RenderThread extends Thread {
private boolean mShouldRun;

public RenderThread() {
mShouldRun = true;
}

private synchronized boolean shouldRun() {
return mShouldRun;
}

public synchronized void quit() {
mShouldRun = false;
}

@Override
public void run() {
while (shouldRun()) {
draw(mCustomView);
SystemClock.sleep(FRAME_TIME_MILLIS);
}
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
mHolder = holder;
updateRendering();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mHolder = null;
updateRendering();
}

@Override
public void renderingPaused(SurfaceHolder holder, boolean paused) {
mPaused = paused;
updateRendering();
}

private synchronized void updateRendering() {

boolean shouldRender = (mHolder != null) && !mPaused;
boolean rendering = mRenderThread != null;

if (shouldRender != rendering) {
if (shouldRender) {
mRenderThread = new RenderThread();
mRenderThread.start();
} else {
mRenderThread.quit();
mRenderThread = null;
}
}
}

private void draw(View view) {

Canvas canvas;
try {
canvas = mHolder.lockCanvas();
} catch (Exception e) {
return;
}
if (canvas != null) {
view.draw(canvas);
mHolder.unlockCanvasAndPost(canvas);
}
}
}

/ ***
* LaunchService Class that uses LiveCardRender to update the live card
*** /
public class LaunchService extends Service {
private static final String LIVE_CARD_TAG = "motion_card";
private TimelineManager mTimelineManager;
private LiveCard mLiveCard;
private LiveCardRender mLiveCardRender;

@Override
public void onCreate() {
mTimelineManager = TimelineManager.from(this);
}

public int onStartCommand(Intent intent, int flags, int startId) {
if (mLiveCard == null) {
mLiveCard = mTimelineManager.createLiveCard(LIVE_CARD_TAG);
mLiveCardRender = new LiveCardRender(this);
mLiveCard.setDirectRenderingEnabled(true);
mLiveCard.getSurfaceHolder().addCallback(mLiveCardRender);
mLiveCard.publish(PublishMode.REVEAL);
}
return START_STICKY;
}

@Override
public void onDestroy(){
if (mLiveCard != null && mLiveCard.isPublished()) {
if (mLiveCardRender != null) {
mLiveCard.getSurfaceHolder().removeCallback(mLiveCardRender);
}
mLiveCard.unpublish();
mLiveCard = null;
}
super.onDestroy();
}
}

最佳答案

看起来错误就在这里,在 onDestroy 中:

if (mLiveCard != null && mLiveCard.isPublished()) {
if (mLiveCardRender != null) {
mLiveCard.getSurfaceHolder().removeCallback(mLiveCardRender);
}
mLiveCard.unpublish();

通过在调用unpublish 方法之前移除回调,系统不再知道在卡片被移除时调用谁的surfaceDestroyed 方法。您实际上可以完全删除对 removeCallback 的调用;这是不必要的。

看起来这是我们在 API 发展过程中遗漏的一些示例中的错误。感谢您收下它,我们会尽快更新它们!

关于安卓/玻璃 : How to trigger surfaceDestroyed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286879/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com