【问题标题】:Android - Image coming from web service has different size on different devicesAndroid - 来自网络服务的图像在不同的设备上具有不同的大小
【发布时间】:2017-01-16 15:18:32
【问题描述】:

我知道关于这个话题有很多问题。但我仍然需要帮助。我想在我的TableLayout 中显示我的ImageView 具有相等的widthheight。但在不同的设备上我无法做到这一点。

这是我在AsyncTask 中使用URL 获取图像,并在onPostExecute 方法上设置ImageView

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];

    Bitmap mIcon11 = null;
    try {

        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);

    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    return mIcon11;

}

protected void onPostExecute(Bitmap result) {
    // set the imageview 
    bmImage.setImageBitmap(result);
}

这是我的 ImageView 所在的 XML 文件 (news.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:paddingBottom="10dp"
android:id="@+id/mylayout"
>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/image"
    android:clickable="true"
    android:background="@drawable/border"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:maxLines="4"
    android:id="@+id/text"
    android:layout_below="@+id/image"
    android:layout_alignEnd="@+id/image"
    android:layout_alignRight="@+id/image"

    android:clickable="true"
    android:textSize="18sp"
    android:background="#e3e3e3"
    android:paddingTop="10dp"
    android:gravity="center_vertical"
    />


</RelativeLayout>

我认为我可以创建一个TableLayout,并在TableRow 中找到View(上图),并排2 视图。(视图在上面)

为此,我获取屏幕宽度并减去 paddings 并除以 2 并将该宽度赋予 ImageView 以便能够为所有图像视图获得相同的大小。

这是我的代码,

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    screenWidth = metrics.widthPixels;

    tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);


    Log.e("screenwidth",":"+screenWidth);
    // here is my paddings converted to pixel and will be subtracted from screen width
    int pixValue = (int) (20 * Resources.getSystem().getDisplayMetrics().density);
    int imageWidthPix = ( screenWidth - pixValue ) / 2;
    Log.e("imageWidthPix ",":"+imageWidthPix );

    for (int i = 0; i < categoryNews.get(myString).size(); i++) {

        if (i % 2 == 0) {
            tr = new TableRow(getActivity());
            tableLayout.addView(tr);
        }

        //here is where my ImageView layout (news.xml)
        View oneNews = inflater.inflate(R.layout.news, null);

        ImageView imageView = (ImageView) oneNews.findViewById(R.id.image);
        TextView textView = (TextView) oneNews.findViewById(R.id.text);

        //here I set the width as I want
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(imageWidthPix, RelativeLayout.LayoutParams.WRAP_CONTENT));

        String imagePath = "http://my.url.com/" + categoryNews.get(myString).get(i).getImagePath();

        new DownloadImageTask(imageView,getActivity().getApplicationContext())
                .execute(imagePath);

我在手机上得到了完美的结果。但在屏幕尺寸不同的设备上,ImageView 的顶部和底部都有空格。我尝试转换九个补丁块并将其转换为再次位图,但没有区别。如果我无法从 Web 服务获取图像,我会将其他分辨率的图像放入 mipmap-hpdimipmap-mdpi 等文件夹中,不会出现错误。但是我正在动态获取图像,那么如何实现ImageView 具有一半的屏幕宽度? 提前致谢。

【问题讨论】:

  • 你的布局中只有两件事吗?
  • 是的,在我的 news.xml 中有一个图像视图和一个文本视图在相对布局内,如您在问题中看到的那样

标签: android imageview resolution tablelayout nine-patch


【解决方案1】:

这里我已经编辑了你的 xml

尝试使用权重的线性布局,根据需要更改权重,更改 layout_weight 以使您的图像视图和文本视图更小或更大。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:orientation="vertical"
    android:id="@+id/mylayout"
    android:weightSum="10">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:id="@+id/image"
        android:src="@drawable/sd"
        android:clickable="true"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:maxLines="4"
        android:id="@+id/text"
        android:clickable="true"
        android:textSize="18sp"
        android:background="#e3e3e3"
        android:paddingTop="10dp"
        android:gravity="center_vertical"
        />


</LinearLayout>

【讨论】:

  • 静止图像视图的顶部和底部有很多空间
  • 尝试使用你的 bitmap.createScaledBitmap();和图像视图中的 android:fitsSystemWindows="true"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 2013-05-02
  • 2014-01-05
  • 1970-01-01
  • 2018-06-22
  • 2021-04-18
相关资源
最近更新 更多