gpt4 book ai didi

java - 如何使 ImageView 大小为屏幕分辨率的两倍

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:32 27 4
gpt4 key购买 nike

我试图将 ImageView 的高度和宽度设置为屏幕高度和宽度的两倍,这显然会根据使用的设备而变化。这就是我尝试过的(但 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);
}

最佳答案

您尝试在 View 尚未呈现的 onCreate() 中执行此操作,尝试将代码移至 onResume() 或更好地这样做

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

}
});

将代码移入其中,它将起作用。

关于java - 如何使 ImageView 大小为屏幕分辨率的两倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48358606/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com