Flickering while using surface view
If you call lockCanvas()
, you need to draw on every pixel in the dirty rect. Since you're calling it without a dirty rect, that means updating every pixel on the Canvas.
I believe the problem with your code is that, when mTouched
is false
, you're not drawing anything at all. Because the Surface is double- or triple-buffered, you're re-displaying the contents of a previous frame, which is going to cause a vibration effect.
I think all you need to do is move the test for mTouched
before the lockCanvas()
call, so you don't flip the buffers if you're not going to draw anything.
You may want to look through the SurfaceView lifecycle appendix in the graphics architecture doc if you haven't seen it before, as the thread management sometimes yields surprises.
SurfaceView blinks the screen for first time - Android
This is common issue with android surface view.
Window was destroyed then re-created when the SurfaceView was adding, and the Window's pixel format was changed mean while, that guided me to the answer, the pixel format of the SurfaceView and the Activity was different so Window Manager forced the re-created.
To resolve it, just added one line in onCreate() to set pixel format, as below:
getWindow().setFormat(PixelFormat.TRANSLUCENT);
Surface view shows black screen
That's because you trying to instantiate new SnakeEngine
, but you should find an instance of it by id because you already add it to XML
file:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// snakeEngine = new SnakeEngine(this);
setContentView(R.layout.activity_main);
snakeEngine = findViewById(R.id.surfaceView);
}
Then, when you will fix it, you should move all your initiating logic from public SnakeEngine(Context context)
constructor to separate method and call it from each constructor of SnakeEngine
:
public SnakeEngine(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SnakeEngine(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public SnakeEngine(Context context) {
super(context);
init(context);
}
private void init(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = null;
if (wm != null) {
display = wm.getDefaultDisplay();
}
Point size = new Point();
if (display != null) {
display.getSize(size);
}
screenX = size.x;
screenY = size.y;
surfaceHolder = getHolder();
setOnTouchListener(this);
paint = new Paint();
paint.setColor(Color.GREEN);
snakeXs = new int[200];
snakeYs = new int[200];
}
Related Topics
How to Check If "Android.Permission.Package_Usage_Stats" Permission Is Given
Android Studio Getslotfrombufferlocked: Unknown Buffer Error
Adding Your Own SQLite Database to an Android Application
Parse JSON Array Response Using Retrofit & Gson
How to Launch Front Camera with Intent
Using Camera in the Android Emulator
Run Task Before Compilation Using Android Gradle Plugin
Android Webview Click Open Within Webview Not a Default Browser
Bring Application to Front After User Clicks on Home Button
Continually Running Background Service
Android Searchview Filter Listview
How to Set the Airplane_Mode_On to "True" or On
How Could I Filter the Listview Using Baseadapter
Memory Analyzer Tool in Android
Sqlite Exception No Such Column When Trying to Select