Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 下载的图像看起来很模糊_Android - Fatal编程技术网

Android 下载的图像看起来很模糊

Android 下载的图像看起来很模糊,android,Android,我下载了一个图像,以使用下面的优秀文章。我自己使用的图像是一张300x400的谷歌地图静态图像。我已经摆弄了一些设置,并将其扩展到整个屏幕,但是它看起来非常模糊 <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_x="0dp" android:layout_y=

我下载了一个图像,以使用下面的优秀文章。我自己使用的图像是一张300x400的谷歌地图静态图像。我已经摆弄了一些设置,并将其扩展到整个屏幕,但是它看起来非常模糊

<ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_x="0dp"
    android:layout_y="0dp"
    android:adjustViewBounds="true"
    android:clickable="false"
    android:gravity="center_vertical"
    android:longClickable="false"
    android:scaleType="fitCenter" />

这就是我在屏幕xml布局中看到的内容。这是一个普遍的问题吗?我搜索得很好,找不到答案。300x400不是android的好分辨率吗

300x400不是android的好分辨率吗

它取决于原始图像的方向以及一些其他的东西,比如布局和设备显示。或者,您可以尝试以下代码下载图像,这对我来说很好:

别忘了加上

    <uses-permission  android:name="android.permission.INTERNET"></uses-permission>

我找到了它们低的原因,图像加载器类有一个内置的压缩:

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=70;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

清晰的图像

请记住,大多数手机屏幕至少有480x800像素(720x1280像素已经变得相当普遍)。即使将像素密度考虑在内,你的图像也可能在放大。我用一张高分辨率的图片试过了,结果也失真了。我试过了,结果得到了一个力接近。你有设置图像的代码吗。我正在使用:Bitmap Bitmap=getBitmapFromURL(“);img=(ImageView)findViewById(R.id.image);img.setImageBitmap(位图);我更新了我的答案。请注意,设置OnClickListener通常在onResume()中完成方法,上面的代码只是一个基本示例这在多个图像上给出以下错误使用…….4147200字节外部分配对于这个过程来说太大了。回答非常糟糕。你已经删除了下采样,就像@the Ray of Hope所说的,你现在冒着OOM问题的风险。
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=70;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){

  try {
    return BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
return null;

}