androiddpipixel-density

What is the offset for dpi and float values in setX, setY methods?[android]


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);
}

I try position a new ImageView at the coordinates 10,100. I also tried position the ImageView a 2000,100 but the ImageView keeps appearing at the same place either way.

I suspect it has to do with pixel density. What is the relation between float pixels and dpi values?


Solution

  • Whatever issues you have they are unrelated to setX() or setY() and pixel density. Anyway setX() and setY() expect an amount of pixels. If you look at the source code of setX() and setY() you see this:

    /**
     * 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);
    }
    

    In other words they basically just call setTranslationX() and setTranslationY(). If your View is unaffected by calls to setX() and setY() I would first look for other causes. For example you may be trying to call setX() and setY() on the wrong View or another part of your code later on may overwrite your changes. I cannot give you a more detailed answer than that with the information you provided.