Last active
May 27, 2018 19:56
-
-
Save libbkmz/0464f8985e28c42bf9bdc244a4b9f716 to your computer and use it in GitHub Desktop.
SurfaceView lockCanvas is very slow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Added this line to dependecies | |
api 'com.google.guava:guava:25.1-android' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.bkmz.surface_view_test; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
public class CustomSurfView extends SurfaceView implements SurfaceHolder.Callback { | |
private MyThread thread; | |
public CustomSurfView(Context context) { | |
super(context); | |
getHolder().addCallback(this); | |
setFocusable(true); | |
} | |
@Override | |
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
} | |
@Override | |
public void surfaceCreated(SurfaceHolder holder) { | |
thread = new MyThread(getHolder(), this); | |
thread.start(); | |
} | |
@Override | |
public void surfaceDestroyed(SurfaceHolder holder) { | |
} | |
public void update(){ | |
} | |
public void myDraw(Canvas canvas) { | |
super.draw(canvas); | |
canvas.drawColor(Color.WHITE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.bkmz.surface_view_test; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// setContentView(R.layout.activity_main); | |
CustomSurfView customSurfView = new CustomSurfView(this); | |
setContentView(customSurfView); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.bkmz.surface_view_test; | |
import android.graphics.Canvas; | |
import android.util.Log; | |
import android.view.SurfaceHolder; | |
import com.google.common.base.Stopwatch; | |
import java.util.concurrent.TimeUnit; | |
public class MyThread extends Thread { | |
private SurfaceHolder surfaceHolder; | |
private CustomSurfView customSurfView; | |
public MyThread(SurfaceHolder surfaceHolder, CustomSurfView customSurfView) { | |
super(); | |
this.surfaceHolder = surfaceHolder; | |
this.customSurfView = customSurfView; | |
} | |
@Override | |
public void run() { | |
Canvas canvas = null; | |
// StopWatch | |
Stopwatch stopwatch = Stopwatch.createUnstarted(); | |
while (true){ | |
try { | |
synchronized (surfaceHolder) { | |
stopwatch.reset(); | |
stopwatch.start(); | |
canvas = this.surfaceHolder.lockCanvas(); | |
stopwatch.stop(); | |
Log.d("MyThread", String.format("lockCanvas: %.1fms", stopwatch.elapsed(TimeUnit.MICROSECONDS)/1000f)); | |
customSurfView.update(); | |
customSurfView.myDraw(canvas); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (canvas != null){ | |
try { | |
surfaceHolder.unlockCanvasAndPost(canvas); | |
} catch (Exception e) { e.printStackTrace(); } | |
} | |
} | |
try { | |
Thread.sleep(16); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment