Imageview在屏幕边缘缩小

时间:2014-11-30 22:27:10

标签: android imageview

如果我想将我的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);
}

以下是问题的直观示例 descrip

1 个答案:

答案 0 :(得分:6)

这是因为在方法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());