【问题标题】:Is There any way to Predict x and y location from one screen size to another?有什么方法可以将 x 和 y 位置从一种屏幕尺寸预测到另一种屏幕尺寸?
【发布时间】:2015-07-05 00:21:04
【问题描述】:

我正在开发一个 android 应用程序,关于连接点和制作图片。 到目前为止,我找不到一种方法来提取黑点的精确 x 和 y 坐标。所以我硬编码了 x 和 y 位置以在图像的精确黑点上绘制点。它在 1152*720 尺寸上效果很好,但是当我在 480*600 尺寸上测试它时出现问题,点从那里的确切位置错位,现在

我的问题是我是否写过类似的东西:

x = 100 , y = 200 (屏幕1152*720)

480*600 等不同屏幕尺寸的 x 和 y 值是多少,如何计算?我知道这是个愚蠢的问题,但我对这些东西很陌生。

【问题讨论】:

  • 1152 -480 = x;所以你的新位置将是 x = (x+100)-viewwidth; ..对你有好处吗?

标签: android image android-layout android-screen


【解决方案1】:

按照您的要求回答您的问题...

int oldScreenX // The current x coord
int newScreenX // The new x coord
...
float oldScreenSizeX = 1152.0f;
float newScreenSizeX = 600.0f;
newScreenX = (int)(oldScreenX / oldScreenSizeX) * newScreenSizeX; // Remember to cast the result back to an int

对 y 做同样的事情。


补充:

也许您应该重新考虑您的方法。
真正的问题是,如果 Image 以不同的大小绘制,您如何将点放在 Image 上的相同位置。所以忘记测量屏幕尺寸。改为测量图像尺寸。
例如,如果您在ImageView 中显示您的图像,您可以编写一个通用的缩放方法,如下所示:

public int scaleCoordinate(int unscaledImageSize, int scaledImageSize, int unscaledCoordinate) {
    scaledCoordinate = (int)(unscaledCoordinate / unscaledImageSize) * scaledImageSize; // Remember to cast the result back to an int
    return scaledCoordinate;
}

然后你可以在你的代码中使用它,比如:

ImageView imageView = (ImageView)findViewById(R.id.my_imageview);
Drawable drawable = image.getDrawable();

// Get the original size of the bitmap
int unscaledSizeX = drawable.getIntrinsicWidth();

// Get the current size it is being drawn at on the screen
int scaledSizeX = imageView.getWidth();

int scaledCoordinateX = scaleCoordinate(unscaledSizeX, scaledSizeX, unscaledCoordinateX);

注意:
ImageView需要在调用上述代码之前由系统进行测量和布局。如果你调用它太早imageView.getWidth() 将返回 0。
最好在 ImageView 实际显示在屏幕上后调用上述代码(来自您的onResume() 或更高版本)。

【讨论】:

  • 如何在代码中获取 android 中的 oldScreenSizeX 、 oldScreenSizeY 值?
【解决方案2】:

我是这样做的,在学习java时可能对你有帮助。

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;


public class DrawLineOnGivenLocations extends Applet implements          MouseListener, MouseMotionListener{


int x1 = 0, y1 = 0, x2 = 0, y2 = 0; 

public void init() {
    addMouseListener(this);
    addMouseMotionListener(this);
}

public void mouseMoved(MouseEvent me) {

    // show status
    showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
}

public void mouseClicked(MouseEvent me) {
    // save coordinates
    x1 = me.getX();
    y1 = me.getY();
    x2 = me.getX();
    y2 = me.getY();

    repaint();

}
public void paint(Graphics g){

    g.drawLine(x1,y1 ,x2, y2);

}

public void mouseEntered(MouseEvent arg0) {}
public void mouseDragged(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {
    //repaint();
}
public void mouseReleased(MouseEvent arg0) {}

}

【讨论】:

  • 这究竟如何回答 OP 的问题?问题是关于从屏幕尺寸到屏幕尺寸的 x/y 位置,无法单击并找到坐标。
猜你喜欢
  • 2015-12-21
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多