setX, setty方法中dpi和float值的偏移量是多少?


public void onClick(View v) {
    ImageView image = (ImageView) inflate.inflate(R.layout.ani_image_view, null); 
    mAllImageViews.add(image);    
    image.setX(10);
    image.setY(100);
}

我尝试在坐标10,100处定位一个新的ImageView。我还尝试将ImageView定位为2000,100,但ImageView始终出现在同一位置。

我怀疑这与像素密度有关。浮点像素和dpi值之间的关系是什么?

无论您有什么问题,它们都与setX()setY()和像素密度无关。无论如何,setX()setY()期望一定数量的像素。如果您查看setX()setY()的源代码,您会看到:

/**
 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
 * {@link #setTranslationX(float) translationX} property to be the difference between
 * the x value passed in and the current {@link #getLeft() left} property.
 *
 * @param x The visual x position of this view, in pixels.
 */
public void setX(float x) {
    setTranslationX(x - mLeft);
}
/**
 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
 * {@link #setTranslationY(float) translationY} property to be the difference between
 * the y value passed in and the current {@link #getTop() top} property.
 *
 * @param y The visual y position of this view, in pixels.
 */
public void setY(float y) {
    setTranslationY(y - mTop);
}

换句话说,它们基本上只调用setTranslationX()setTranslationY()。如果您的View不受setX()setY()调用的影响,我将首先寻找其他原因。例如,您可能试图在错误的View上调用setX()setY(),或者稍后您的代码的另一部分可能会覆盖您的更改。根据你提供的信息,我无法给你更详细的答复。

最新更新