V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yeelone
V2EX  ›  Android

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

  •  
  •   yeelone · 2018-01-09 23:39:05 +08:00 · 4653 次点击
    这是一个创建于 2269 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需要用到 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>
    
    3 条回复    2018-01-10 17:42:08 +08:00
    nicevar
        1
    nicevar  
       2018-01-10 13:36:27 +08:00
    你是做游戏吗?为啥要用 SurfaceView
    双缓冲你应该知道吧,SurfaceView 里面有 front 和 back,我看你的代码 firstFrame 交替的,刚好切来切去,原因应该就在这里了
    yeelone
        2
    yeelone  
    OP
       2018-01-10 17:41:39 +08:00 via iPhone
    @nicevar 不是游戏,只是学习
    yeelone
        3
    yeelone  
    OP
       2018-01-10 17:42:08 +08:00 via iPhone
    我知道我犯了什么错误了,简直低级啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3151 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 12:20 · PVG 20:20 · LAX 05:20 · JFK 08:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.