如何在ImageView中显示其实际宽度和高度的位图

时间:2012-09-08 21:10:25

标签: android layout bitmap imageview httpurlconnection

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

java代码:

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

包含imageview的布局是:

<?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>

1 个答案:

答案 0 :(得分:0)

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

另外,您是否注意到您已将imageView的大小设置为其某个维度的fill-parent,并使用了fit-start?

根据你设定的规则,这会使图像延伸到可以的范围。如果你为两个维度设置wrap-content,那就更容易预测了。