【问题标题】:Android ImageView size in different screensAndroid ImageView 在不同屏幕上的大小
【发布时间】:2014-07-08 04:28:52
【问题描述】:

我正在尝试以编程方式添加 imageView,但不要在不同的屏幕上设置相同的尺寸。我尝试了很多比例代码,但没有任何好的结果。

屏幕:screens' image

这是我的代码:

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;
  }

}

【问题讨论】:

  • 那么你的问题是什么?
  • 请点击屏幕链接并聚焦红色箭头。

标签: android imageview


【解决方案1】:

看起来问题不是ImageView,而是您添加的图像的大小。您正确地观察到相同的图像将根据显示它的屏幕的属性以不同的方式显示。然而,目标是正确显示或缩放图像位图本身,与ImageView 的大小无关。

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

这个属性也可以是set programatically:

imageView.setScaleType(ImageView.SCALE_TYPE);

您应该考虑研究不同的体重秤类型,看看是否适合您的需求。如果没有,在设置 ImageView 之前还有其他方法可以create thumbnail 或替换大小的图像

【讨论】:

  • 感谢您的回复。我仍然无法修复。我尝试了所有 setScaleType 类型,但没有任何结果。如何修复我的代码?
  • 我会先摆脱您在图像视图上进行的手动缩放。什么是 UrlImageViewHelper?你能告诉我那个方法的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 2020-02-09
  • 2011-10-02
  • 1970-01-01
相关资源
最近更新 更多