请问应该怎么样正确的在 surfaceview 上画图?如何解决闪屏问题?

2018-01-09 23:39:05 +08:00
 yeelone

需要用到 surfaceview 画图,遇到闪屏的问题,具体就是我循环画两个方块,期待的情况下应该是同一帧上会同时画出两个方块,可是因为 surfaceview 的缓存机制,出现了第一帧画了一个方块,第二帧画了不同的方块,如图:

我查到的解决方法是,每次画图时,都先为 canvas 制作一份副本,然后在这份副上继续画画,最后再投到屏幕上去。代码如下:

https://gist.github.com/93572c9f2af1f7f3441a5080515f25eb

这个解决方案在别人那里似乎是有效的,在我这里却没有效果,不知道是我错在哪里?求解救

还有一个问题,看图:

我发现在我循环画图时,总是很奇怪的会画到最后就出现白色背景,我并没设置白色背景,默认应该是黑色的吧?搜不到相关的解释,求解?

再加入一个 layout 代码吧,不知道是不是两个 layout 会互相影响:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"
    tools:context=".ui.activities.MainActivity">

    <include layout="@layout/toolbar"/>

    <com.elone.android.etest.ui.activities.MySurfaceView1
        android:id="@+id/mySurfaceView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="100dp"
        android:layout_marginTop="50dp"/>

    <com.elone.android.etest.ui.activities.MySurfaceView2
        android:id="@+id/mySurfaceView2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:layout_constraintTop_toBottomOf="@+id/mySurfaceView1"/>


</android.support.constraint.ConstraintLayout>
5277 次点击
所在节点    Android
3 条回复
nicevar
2018-01-10 13:36:27 +08:00
你是做游戏吗?为啥要用 SurfaceView
双缓冲你应该知道吧,SurfaceView 里面有 front 和 back,我看你的代码 firstFrame 交替的,刚好切来切去,原因应该就在这里了
yeelone
2018-01-10 17:41:39 +08:00
@nicevar 不是游戏,只是学习
yeelone
2018-01-10 17:42:08 +08:00
我知道我犯了什么错误了,简直低级啊

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/421524

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX