Android 在布局中调整 ImageView 的大小



我不知道为什么这不能正常工作。我正在布局容器中添加图像。我希望图像为 768x1024,容器为 500x500。我这样做是为了我可以在游戏的图像周围滑动,但我无法使图像大于容器。图像将调整为容器的大小。

          containerView = new RelativeLayout(this);
     RelativeLayout.LayoutParams layoutForContainer = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
     layoutForContainer.height = 500;
     layoutForContainer.width = 500;
     layoutForContainer.addRule(RelativeLayout.CENTER_IN_PARENT);
     containerView.setLayoutParams(layoutForContainer);  
     layout.addView(containerView); 
     myImageView = new ImageView(this);
     myImageView.setImageResource(R.drawable.myImage); 
     RelativeLayout.LayoutParams layoutForLargeImage = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
     layoutForLargeImage.height = 768;
     layoutForLargeImage.width = 1024;
     myImageView.setLayoutParams(layoutForLargeImage); 
     containerView.addView(myImageView);

使用以下代码:

myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myImageView.setAdjustViewBounds(true);

最新更新