The image goes down at the edge of the screen

If I want to move the position of my image to a location where part of the right side of the image will pass by the visible screen, the image tends to shrink and the whole image tends to stay on the edge of the screen, the same can be said if I lower the image to get through past the bottom of the screen, the image shrinks and never goes past the bottom of the screen,

but if i move the position of the image up or left the image size does not fade and the image goes past the screen the way i want it i am curious if anyone has any advice how can i fix this problem thankyou

    mainLayout = (ViewGroup)findViewById(R.id.id_layout);

    deviceScreenSize = new Point();

    thisContext = this;

    mainDisplay = getWindowManager().getDefaultDisplay();
    mainDisplay.getSize(deviceScreenSize);
    offsetX = (deviceScreenSize.x / 320.0f);
    offsetY = (deviceScreenSize.y / 568.0f);

    mainIMG = new ImageView(this);
    mainIMG.setImageDrawable(getResources().getDrawable(R.drawable.myimg));
    SetPos(0, 0, 320, 568);
    mainIMG.setLayoutParams(layoutPositioner);
    mainLayout.addView(mainIMG);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

public void SetPos(float x, float y, float width, float height) {
    layoutPositioner = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    layoutPositioner.topMargin = (int)(y * offsetY);
    layoutPositioner.leftMargin = (int)(x * offsetX);
    layoutPositioner.width = (int)(width * offsetX);
    layoutPositioner.height = (int)(height * offsetY);
}

public void SetPosWithoutOffset(float x, float y, float width, float height) {
    layoutPositioner = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    layoutPositioner.topMargin = (int)(y);
    layoutPositioner.leftMargin = (int)(x);
    layoutPositioner.width = (int)(width);
    layoutPositioner.height = (int)(height);
}

      

Here is a clear example of the problem descrip

+3


source to share


1 answer


This is because when positioned in a method SetPos()

:

you only handle left and top margins


layoutPositioner.topMargin = (int)(y * offsetY);
layoutPositioner.leftMargin = (int)(x * offsetX);

      

So, to solve this problem, you should handle all directions like this:

// first get screen width and height 
DisplayMetrics metrices = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrices);
int windowWidth = metrices.widthPixels;
int windowHeight = metrices.heightPixels;

//then set your margins 
int x_cord = (int)(x * offsetX);
int y_cord = (int)(y * offsetY);
layoutPositioner.topMargin = y_cord;
layoutPositioner.leftMargin = x_cord;
layoutPositioner.bottomMargin = windowHeight - (y_cord - your_image.getHeight());
layoutPositioner.rightMargin= windowWidth - (x_cord - your_image.getWidth());

      

+5


source







All Articles