下载的图像调整大小

时间:2011-08-25 16:38:49

标签: android android-layout

我从互联网上下载的图片有以下尺寸:128px x 128px 下载后,图像用作ImageButton的背景,但是当显示按钮时,图像比下载之前的图像小。对于下载,我使用AsyncTask ...这是我的代码:

public class ImageDownloaderButton extends AsyncTask<String, Void, Bitmap> {

    private String url;
    private final WeakReference<ImageButton> imageButtonReference;

    public ImageDownloaderButton(ImageButton imageButton) {
        imageButtonReference = new WeakReference<ImageButton>(imageButton);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        url = params[0];
        try {
            if (url.equals("")) {
                return null;
            }
            return BitmapFactory.decodeStream(new URL(url).openStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (isCancelled()) {
            result = null;
        }
        if (imageButtonReference != null) {
            ImageButton imageButton = imageButtonReference.get();
            if (imageButton != null) {
                if (result != null) {
                    imageButton.setImageBitmap(result);
                } else {
                    imageButton.setImageResource(R.drawable.noimageavailable);
                }
            }
        }
    }

    @Override
    protected void onPreExecute() {
        if (imageButtonReference != null) {
            ImageButton imageButton = imageButtonReference.get();
            if (imageButton != null) {
                imageButton.setImageResource(R.drawable.noimageavailable);
            }
        }
    }
}

下载首发的代码段:

ImageButton imageButton = (ImageButton) view.findViewById(R.id.icon_image);
ImageDownloaderButton downloader = new ImageDownloaderButton(imageButton);
downloader.execute(supp.getPicture()); //the picture is the url
imageButton.setOnClickListener(createOnClickListener(supp.getId()));

最后这里是包含ImageButton

的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/icon_with_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageButton
    android:id="@+id/icon_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:layout_gravity="center"
    android:onClick="myClickHandler"/>
<TextView
    android:id="@+id/icon_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textColorHighlight="#656565"/>
</LinearLayout>

有人可以向我解释为什么调整图像大小? AsyncTask正在下载时显示的图像(noavailableimage)是128x128px,而且这个图像的大小正确。

截图:

aphotheke baecker

仿真器:

mainmenu supplier

3 个答案:

答案 0 :(得分:1)

在设置位图之前尝试此操作:

imageButton.setScaleType(ImageView.ScaleType.CENTER);

答案 1 :(得分:1)

之前我遇到过这个问题,而不是使用BitmapFactories解码方法 - 尝试使用Drawable类,例如。

URL url = new URL("some address");
URLConnection connection = url.openConnection();
Drawable d = Drawable.createFromStream(connection.getInputStream(),"src");

希望这有帮助!

答案 2 :(得分:0)

如果您事先知道要设置的图片尺寸为128x128,请将layout_width的{​​{1}}和layout_height设置为128px。

相关问题