如果我想将我的imageview的位置移动到图像右侧的一部分将延伸超过可见屏幕的位置,图像趋于收缩,并且所有图像都倾向于保持在屏幕的边缘内,同样可以如果我降低图像以延伸超出屏幕底部,图像会缩小并且不会延伸超过屏幕的下边缘,
然而,如果我向上移动图像的位置或向左移动图像大小没有缩小并且图像像我想要的那样延伸到屏幕之外,我很好奇是否有任何建议我如何解决这个问题而不是你
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);
}
这是问题的视觉示例
解决方法:
这是因为在方法SetPos()中定位时只处理左边距和上边距:
layoutPositioner.topMargin = (int)(y * offsetY);
layoutPositioner.leftMargin = (int)(x * offsetX);
因此,要解决此类问题,您必须按以下方式处理所有方向:
// 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());