【问题标题】:How to display bitmap in ImgaeView with its actual width and height如何在 ImageView 中显示位图的实际宽度和高度
【发布时间】:2012-09-08 21:10:24
【问题描述】:

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

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

包含图像视图的布局是:

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

【问题讨论】:

    标签: android layout bitmap imageview httpurlconnection


    【解决方案1】:

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

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

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

    【讨论】:

    • >> 宽度正常..但看起来像是被压缩了
    • 请检查输入图像的纵横比是否与显示的图像相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    相关资源
    最近更新 更多