不同屏幕中的Android ImageView大小

不同屏幕中的Android ImageView大小,android,imageview,Android,Imageview,我尝试以编程方式添加imageView,但不要在不同的屏幕中使用相同的大小。我试过很多刻度代码,但没有任何好结果 屏幕: 这是我的密码: public class Explore extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View vi

我尝试以编程方式添加imageView,但不要在不同的屏幕中使用相同的大小。我试过很多刻度代码,但没有任何好结果

屏幕:

这是我的密码:

public class Explore extends Fragment {

  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                           Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.tab, container, false);

    LinearLayout linearLayout= new LinearLayout(getActivity());
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    linearLayout.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));

    final float scale = view.getContext().getResources().getDisplayMetrics().density;

    int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, getResources().getDisplayMetrics());
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 82, getResources().getDisplayMetrics());

    int lastWidth = (int) (width * scale + 0.5f);
    int lastHeight = (int) (height * scale + 0.5f);

    ImageView imageView = new ImageView(getActivity());
    UrlImageViewHelper.setUrlDrawable(imageView, "https://example.com/example.jpg");

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lastWidth, lastHeight);
    imageView.setLayoutParams(lp);


    linearLayout.addView(imageView);

    return linearLayout;
  }

}

问题似乎不是
ImageView
,而是要添加的图像的大小。您可以正确地观察到,同一图像将根据其显示屏幕的属性显示不同。但是,目标是正确显示或缩放图像位图本身,而不受
ImageView
大小的影响

例如,如果要使用XML添加此
ImageView
,则有一个名为的属性,用于控制图像在
ImageView
中的显示方式。根据此属性的设置方式,
ImageView
将保留所需的大小,但会缩放内容以适应其范围

此属性也可以是:


你应该考虑一下不同规模的类型,看看是否符合你的需要。如果没有,在设置
ImageView

之前,还有其他方法可以设置或替换大小的图像。那么您的问题是什么?请单击屏幕链接并聚焦红色箭头。谢谢您的回答。我还是修不好。我尝试了所有的setScaleType类型,但没有任何结果。我如何修复我的代码?我会先摆脱你在图像视图上进行的手动缩放。什么是UrlImageViewHelper?你能告诉我那个方法的代码吗。
imageView.setScaleType(ImageView.SCALE_TYPE);