下载的图像被调整

下载的图像被调整

本文介绍了下载的图像被调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从具有下列尺寸互联网上下载的图像:128像素点¯x128PX
下载后的图像作为背景的​​的ImageButton ,但显示该按钮时,图像是比它它被下载以前小。对于下载我使用的AsyncTask ...这里是我的code:

I am downloading an image from the internet which has the following dimensions: 128px x 128pxAfter the download the image is used as the background of an ImageButton but when the button is displayed, the image is smaller than it was before it was downloaded. For the download i use an AsyncTask ... here is my code:

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

在codesnippet哪里下载startet:

The codesnippet where the download is startet:

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()));

和终于在这里是XML文件包含的ImageButton

and finally here is the xml file which contains the ImageButton:

<?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而这一次是在正确的大小显示。

Can anyone explain to me why the image is resized? The image (noavailableimage) that is shown while the AsyncTask is downloading is 128x128px and this one is shown in the correct size.

截图:



模拟器:


推荐答案

我以前也有这个问题,而不是使用BitmapFactories德code方法 - 尝试使用可绘制类例如

I've had this problem before, instead of using the BitmapFactories decode method - try the using the Drawable class e.g.

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

希望这有助于!

这篇关于下载的图像被调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:59