WindowManager laggy when updating multiple views

Jun 21, 2016
10
0
0
I'm not sure if this is the appropriate thread for this, I'm sorry if it's not.

I am trying to move multiple ImageViews attached to the WindowManager but after adding three views, the movement becomes very choppy.

After some investigation I think this is caused by calling updateViewLayout too many times and on different views.

The code below is a simplified version of what I am trying to do and illustrates the problem.

Code:
public class MainActivity extends AppCompatActivity {

    WindowManager windowManager;
    ArrayList<ImageView> imageViews = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        windowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
        addImageView();
        addImageView();
        addImageView();
    }

    private void addImageView() {
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.START;
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.mipmap.ic_launcher);
        imageView.setOnTouchListener(new ImageViewTouchListener());
        imageViews.add(imageView);
        windowManager.addView(imageView, params);
    }

    private class ImageViewTouchListener implements View.OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    for(ImageView imageView : imageViews) {
                        WindowManager.LayoutParams params = (WindowManager.LayoutParams)imageView.getLayoutParams();
                        params.x = (int)event.getRawX();
                        params.y = (int)event.getRawY();
                        windowManager.updateViewLayout(imageView, params);
                    }
                    return true;
            }
            return false;
        }
    }
}

As you can see, I am adding three ImageViews in onCreate, and each ImageView has a TouchListener that updates the layout of every bubble in ACTION_MOVE.

When adding one or two ImageViews, the movement is fluid, but three or more and it lags badly.

Anyone have any ideas? Thank you very much!
 

Forum statistics

Threads
955,995
Messages
6,966,354
Members
3,163,457
Latest member
dongapko1