Issue
I have a scrollview and nested several custom SurfaceView within. As I scroll, the SurfaceView background begins to flicker black. I have researched this for at least a day now and am unable to find a solution. I have tried to implement the solution of this thread: http://android-coding.blogspot.ca/2012/01/flickering-problems-due-to-double.html but found no success.
With my SurfaceView, I am trying to draw text and a downloaded image of an emoji. Initially, I used a custom view but was blocked after attempting to download a bitmap in the UI thread and recieved a NetworkOnMainThreadException. So I tried to use an AsyncTask for the entire onDraw() portion of the view. However, the View remained blank, and I was unable to update it after the AsyncTask finished. I tried to call invalidate() on the view after the AsyncTask finished but it resulted in an endless loop of onDraw(). Then I created a boolean variable to stop this loop and render the view only once, but it ended the same way I started: a blank view.
Now I am attempting to use a SurfaceView because I read
One of the purposes of this class is to provide a surface in which a secondary thread can render into the screen
and thought I should use it because I am using a seperate thread to download a bitmap.
Now that I implemented a SurfaceView, it does not work the way I want it to as it flickers black as I scroll. I have recorded it here: https://youtu.be/o5j9s4XF65E
Pastebin of faulty code here: https://pastebin.com/g61aSNM2
The questions I am asking:
Should I be using a
SurfaceViewfor this purpose? If not, then what would be the proper way to implement this?Should I be using a
Threadinstead ofAsyncTaskto download a bitmap from url?Why is SurfaceView flickering and what can I do about it?
Solution
Ok, I spent some time rethinking the structure of my View and realized I should not be using SurfaceView.
I made my CustomView, TextMessage, extend ImageView and during onDraw() I called my custom AsyncTask<TextMessage, Void, Bitmap> which simply creates a Bitmap of what the View should be. After AsyncTask finishes, during the onPostExecute() method I called setImageBitmap(myBitmap) on the TextMessage which I first passed as the parameter.
This is so simple I complete overthought my problem, I never should have used SurfaceView in the first place.
Answered By - woodenwaffles
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.