0

在这篇优秀的文章之后,我下载了一张图片以供使用。我自己使用的图像是 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 来说不是很好的分辨率吗?

4

2 回答 2

1

300x400 对 android 来说不是很好的分辨率吗?

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

不要忘记添加

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

到你的清单

    public class MainActivity extends Activity {

private Button get;
private ImageView pic;
private static final String SRC = "http://www.allindiaflorist.com/imgs/arrangemen4.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    get = (Button)findViewById(R.id.button1);
    pic = (ImageView)findViewById(R.id.imageView1);

    get.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            pic.setImageBitmap(getBitmapFromURL(SRC));

        }
    });

}

 public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);

            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("getBmpFromUrl error: ", e.getMessage().toString());
            return null;
        }
    }

    }

在此处输入图像描述

于 2013-02-18T22:00:39.517 回答
1

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

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

}

清晰的图像。

于 2013-02-18T22:48:11.863 回答