0

我正在尝试在 android 中显示图像。图像需要从服务器获取。确保存储在服务器上的图像适合所有 android 设备屏幕的最佳方法应该是什么。在显示 XML 的图像中,我还需要显示一个文本视图(在图像下方),以提供有关图像的简要描述。我是否必须创建具有特定高度和宽度的图像,还是有其他方法?

4

2 回答 2

1

您应该在服务器上存储足够大的图像。

// Know the required width of the image
URL url = new URL(remotePath);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
inputStream = (InputStream) urlConnection.getContent();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, options);
int height = options.outHeight;
int width = options.outWidth;
int sampleSize = requiredWidth / width; // Calculate how you want to sample the images so you can keep the memory small
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, options);
imageView.setImageBitmap(bitmap);

希望这可以帮助。

于 2013-05-15T07:39:35.247 回答
0

If you want to display on that screen only the image and description below it, you could place the 2 component in a LinearLayout with the orientation vertical and use the layout_weight property of the LinearLayout, for example you could do something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleType="center" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

You could download the image from the server manually or you could use a library like UIL, or webImageLoader.

于 2013-05-15T07:43:59.200 回答