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

时间:2018-01-20 16:43:22

标签: java android android-layout android-studio

我正在尝试将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);
}

1 个答案:

答案 0 :(得分:0)

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

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

            }
        });

将代码移到此处,它将起作用。