简体   繁体   中英

Coordinate X& Y on Screen different size

I have a view and inside a viewpager with images. When I touch the view I display an image where I'm touching. In the same time I send X and Y position in Firebase. In another device, I get back those coordinates and display an image at those coordinates.

But the screen size of the tablets are differents, how can I show my image at the same position ? Because if I touch the middle screen of the first tablet, it showing an image en the right bottom of the other one

UPDATE

For user 1, when he touch the view a pointer/cursor is showing

 v.animate()
 .x(Math.round(event.getX()))
 .y(Math.round(event.getY()))
 .setDuration(0)
 .start();

In the same time he sends coordinates to Firebase and the width and height of his screen.

    Display display = getActivity().getWindowManager().getDefaultDisplay();
         Point size = new Point();
         display.getSize(size);
         int width = size.x;
         int height = size.y;

        Map<String, Object> map = new HashMap<>();
        map.put("x", Math.round(event.getX()));
        map.put("y", Math.round(event.getY()));
        map.put("sizeX", width);
        map.put("sizeY", height);
        mRefMyPos.updateChildren(map);

The user 2 has a listener and get back those data to display an imageView at the coordinates.

 listenerRefCoordonate = mRefCoordonate.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            CoordonateModel coordinateModel = dataSnapshot.getValue(CoordonateModel.class);

            if (coordinateModel == null){
                //do something
                mImageView.setVisibility(View.GONE);
            } else {

                final Float mScale = Math.min(1.0f * width / coordinateModel.getSizeX(), 1.0f * height / coordinateModel.getSizeY());

                if (coordinateModel.getX() != null){
                    mImageView.setVisibility(View.VISIBLE);
                    mImageView.animate()
                            .x(Math.round(coordinateModel.getX() * mScale))
                            .y(Math.round(coordinateModel.getY() * mScale))
                            .setDuration(0)
                            .start();
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError firebaseError) {
        }
    });

Seems to be incorrect for example if I have an imageview with ABCDEFGHI.... (match_parent), if I try to show C, it showing E on the other device. Maybe my scale is incorrect ??

I tried to test if the scale is correct. When I touch the screen without displaying imageview where I'm touching but I'm listening what I'm writting on FireBase and displaying imageview with coordinates/with and height of screen device and do the scale and the when I'm displaying the imageview it showing where I'm touching so I guess the scale is correct

I will try to re explain my problem:

Tablet n1, 1024/768 Tablet n2, 1920/1200

Both are on a view full_screen with the same Imageview scale FitXY and match_parent On the first tablet I'm touching the tail of the pig (Imageview), I'm sending X,Y position of the screen touch (Math.round(event.getX(), Math.round(event.getY()) and the width/height screen size of the tablet. The second tablet displaying (a cursor) around the tail of the pig but not at the exact position....

Can someone help me ?

User tablet 1 touching his screen (RED POINT where he's touching) 用户平板电脑 1 触摸他的屏幕(他触摸的地方是红点)

User tablet 2 listening X,Y position and draw where the user 1 is touching (RED POINT)

在此处输入图片说明

UPDATE 2

I changed and I calculate like this one on this post stackoverflow.com/a/21588683/5845928 I have what you said : tablet 1 the point is (258,230) and tablet 2 (484, 359). the problem seems to be the imageview. How can I solve it ?

** FIXED **

TABLET 2 receive x,y position and width and height of tablet 1.

hisWidth = coordinateModel.getSizeX();
hisHeight = coordinateModel.getSizeY();

float percent_width = Float.valueOf(coordinateModel.getX()) / hisWidth;
float percent_height = Float.valueOf(coordinateModel.getY()) / hisHeight;
float x = percent_width * mWidth;
float y = percent_height * mHeight;

尝试发送设备宽度和高度,以便您可以使用它们来计算其他设备中的确切 (X,Y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM