Get position of ImageView relative to screen programmatically

后端 未结 3 1540
猫巷女王i
猫巷女王i 2020-12-29 08:14

I want to get the position of my ImageView programmatically. By position, I mean the distance in pixel from top of the screen to that ImageView. I

相关标签:
3条回答
  • 2020-12-29 08:56
    Glide.with(this)
                    .asBitmap()
                    .apply(requestOptions)
                    .load(url)
                    .placeholder(R.mipmap.img_place_holder)
                    .apply(new RequestOptions().override(myImage.getMeasuredWidth(), myImage.getMeasuredHeight()))
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap bitmap,
                                                    Transition<? super Bitmap> transition) {
    
    //                        TouchImageView   myImage1 = findViewById(R.id.image_zoom_poll_card);
                            img_zoomIn.setVisibility(View.VISIBLE);
    
                            myImage.setImageBitmap(bitmap);
    
            getBitmapPositionInsideImageView(myImage, img_zoomIn);
    
                        }
                    });
    
    0 讨论(0)
  • 2020-12-29 09:09

    You need to wait for the callback when the layout has been placed with children views. This is the listener you need to add to your root view in order to calculate the coordinates for your image. Otherwise it will return 0.

    getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            getViewTreeObserver().removeGlobalOnLayoutListener(this);
    
            int[] locations = new int[2];
            image.getLocationOnScreen(locations);
            int x = locations[0];
            int y = locations[1];
        }
    });
    
    0 讨论(0)
  • 2020-12-29 09:10

    Use View.getLocationOnScreen() or getLocationInWindow() BUT ONLY after the view has been layout. getTop(), getLeft(), getBottom() and getRight() return the position relative to its parent.

    0 讨论(0)
提交回复
热议问题