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.

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!

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/c7ee22f5-ee38-4f6d-b3eb-2fcf18c3115b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to