-1

我使用以下两个功能从互联网下载和图像。当在图像视图中显示下载图像时,它的大小(宽度和高度(与实际大小不同。为什么?因为我已将图像保存到可绘制文件夹并在图像视图中显示它看起来像它的大小一样好。

爪哇代码:

public InputStream OpenHttpConnection(String urlString) throws IOException{
    InputStream in = null;
    int respobse = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();
    if(!(conn instanceof HttpURLConnection))
        throw new IOException("It's not HTTP connection");
    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        respobse = httpConn.getResponseCode();

        enableHttpResponseCache();

        if(respobse == HttpURLConnection.HTTP_OK){
            in= httpConn.getInputStream();
            Log.d("HTTP connection","OK");
        }
    }catch (IOException e) {
        Log.e("HTTP ERROR",e.toString());
        throw new IOException();
    }
    return in;
}

public Bitmap DownloadImage(String URL){

    Bitmap bitmap = null;
    InputStream in = null;

    try{
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    }catch(IOException ex){

    }
    return bitmap;
}

包含图像视图的布局是:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/include21"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/round" >



    <ImageView
        android:id="@+id/imgRecibeImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round"
        android:scaleType="fitStart"
        android:src="@drawable/img1" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right" >

        <TextView
            android:id="@+id/txtRecibeName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#80000000"
            android:gravity="right"
            android:padding="8dp"
            android:text="Crispy Chicken and Sandwich"
            android:textColor="#ffffff"
            android:textSize="17sp" />


        <TextView
            android:id="@+id/tvLoader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/txtRecibeName"
            android:layout_alignBottom="@+id/txtRecibeName"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="40dp"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

</FrameLayout>
4

1 回答 1

0

您是否检查过位图是否适合密度?我的意思是,如果你使用 100x100px 的图像,它在高密度设备上会更小,而在低密度设备上会更大......

另外,您是否注意到您已将 imageView 的大小设置为其尺寸之一的填充父级,并使用了 fit-start ?

根据您设置的规则,这会使图像拉伸到它可以拉伸的程度。如果您为两个维度设置 wrap-content ,它会更可预测。

于 2012-09-08T21:55:09.237 回答