0

I have a grid of pixels being displayed using a RecyclerView (as shown in image). When the button is pushed, most of the pixels will be changing colors. I want this update to process each pixels color change more uniformly, even if it takes a very long time to complete (there may be hundreds of thousands of pixels) without blocking the main thread. How is this possible?

If possible I'd like a smooth flow as individual pixels change. The most important thing here is not completing the action quickly but smoothly. I'm not attached to RecyclerView if this isn't the best choice - I was just lead to believe it would be better for performance. I don't need listeners of any kind for the cells. Eventually the cells will become more complex, each housing other cells (Layered Views with background colors). I will be using an SQL database and the Room library for data storage and edit.

The process should be happening over a background thread so as not to halt the UI. Currently I am updating my RecyclerView via myRecyclerAdapter.notifyItemChanged(i); on the onClickListener of the button and my UI will halt while it completes.

screenshot

lostScriptie
  • 347
  • 1
  • 3
  • 11
  • I don't think RecyclerView is a good choice here. Since in RecyclerView you cannot control the order item refresh once a change is made to the dataset. – Itamar Kerbel Oct 30 '18 at 06:22
  • Even if I can't control the order of refresh and they refresh "randomly" as I update my cells in sequence that's fine. What I care about is a smooth display of changes that might be happening regularly. – lostScriptie Oct 30 '18 at 06:25
  • I'll give you an answer - but its a good one :) – Itamar Kerbel Oct 30 '18 at 06:33

2 Answers2

1

Try the CustomView,there are very good reasons to use CustomView for creating views that are not provided by the standard widgets. You shoud be able to use a background thread to implement the time sync or delay between each transition,to make it smooth.And use runOnUiThread() method to change the ui i.e pixel colors in your case. for example- you can try stuff like this-

 Runnable onAnimationStarted=new Runnable() {
            @Override
            public void run() {
            //change ui
            }
        };
Runnable onAnimationStopped=new Runnable() {
            @Override
            public void run() {
            //change ui
            }
        };
       Thread thread = new Thread(){
                @Override
                public void run() {
    runOnUiThread(onAnimationStarted);
              long start= System.currentTimeMillis();;
              while(System.currentTimeMillis()-start<1000) //waits one second
            {

            };
    runOnUiThread(onAnimationStopped);                 
        }

        };
            thread.start();
because_im_batman
  • 975
  • 10
  • 26
0

What you can do is one by one update the cells you want to update. Then call notifyDataSetChanged. An example can be found here.

If you encounter rendering problems try requestLayout and forceLayout.

I recommend reading this answer to better understand what going on in the background.

Itamar Kerbel
  • 2,508
  • 1
  • 22
  • 29
  • This is precisely what I am already doing. After I notifyDataSetChanged() I get like a 30s delay on my emulator with the 2500 cells shown in the image. Perhaps the problem is that I am calling from my Activity? (although I'm not using request/forceLayout yet... I'll read up on those).. – lostScriptie Oct 30 '18 at 06:41
  • don't think so. I think its just too many cells that are in view and the RecyclerView just takes a long time to update. you can use the profiler in AndroidStudio to see whats going on. – Itamar Kerbel Oct 30 '18 at 06:45
  • Is there a better way to display this many pixels? My finished project will likely have tens of thousands of cells per display, each cell having several different colors being shown per pixel for information. – lostScriptie Oct 30 '18 at 06:47
  • 1
    I think you should have a look at [CustomView](https://developer.android.com/guide/topics/ui/custom-components). As you need to have control over every aspect of the drawing and refreshing process. – Itamar Kerbel Oct 30 '18 at 20:22