Imageview在屏幕边缘缩小

use*_*368 3 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);
}
Run Code Online (Sandbox Code Playgroud)

这是问题的视觉示例 记述

Muh*_*aat 6

这是因为在方法中定位时只处理左边距和边距SetPos():

layoutPositioner.topMargin = (int)(y * offsetY);
layoutPositioner.leftMargin = (int)(x * offsetX);
Run Code Online (Sandbox Code Playgroud)

因此,要解决此类问题,您必须按以下方式处理所有方向:

// 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());
Run Code Online (Sandbox Code Playgroud)