Android - 来自网络服务的图片在不同的设备上有不同的尺寸

时间:2017-01-16 06:45:13

标签: android imageview resolution tablelayout nine-patch

我知道这个话题有很多问题。但我仍然需要帮助。我想在我的ImageView中显示我的width平等heightTableLayout。但是在不同的设备上,我无法做到这一点。

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

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,并在View内找到TableRow(上方),并排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在顶部和底部都有空格。我试图转换九个补丁块并将其转换为位图,但没有区别。如果我无法从网络服务获取图像,我会将其他分辨率图像放入mipmap-hpdimipmap-mdpi等文件夹中,并且不会出现错误。但是我动态地获取图像,那么如何实现ImageView屏幕宽度的一半? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

这里我编辑了你的xml

尝试使用权重进行线性布局,根据需要更改权重,更改layout_weight以使图像视图和textview更小或更大。

<?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>
相关问题