简体   繁体   English

如何使ImageView变为屏幕分辨率的两倍

[英]How do I Make an ImageView twice the size of screen Resolution

I am trying to set the height and width of an imageview to be twice the size of the height and width of the screen, which obviously changes depending on what device is used. 我正在尝试将imageview的高度和宽度设置为屏幕高度和宽度的两倍,这显然取决于所使用的设备而变化。 This is what I attempted (but the imageview was just invisible): 这是我尝试过的(但是imageview只是不可见的):

public class MainActivity extends AppCompatActivity {
private ImageView Geoline;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Geoline = findViewById(R.id.Geoline);

//Fetching the Device Resolution
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int scrWidth = size.x;
    int scrHeight = size.y;

 //Creating variables that are twice the size of screen resolutions
    int scalex = 2 * scrWidth;
    int scaley = 2 * scrHeight;


 //Setting the screen width and height to equal that of variables "scalex" and "scaley"
    Geoline.setScaleX(scalex);
    Geoline.setScaleY(scaley);
}

You are trying to do this in onCreate() where View is not yet rendered, trying moving your code to onResume() or even better do like this 您正在尝试在尚未渲染View的onCreate()中执行此操作,尝试将代码移至onResume(),甚至最好这样做

mBinding.parentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

            }
        });

Move your code inside this, it will work. 在其中移动您的代码,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM