使用URL使用屏幕宽度图像填充TextView

时间:2014-08-10 19:35:14

标签: android textview android-drawable

我正在从网址加载Feed并获取包含我放在TextView中的图片的文字,但TextView内的图片在超过3.5英寸的显示屏上很小。我想问题出在setBounds方法中。这是我的班级:

public class URLImageParser implements Html.ImageGetter {
Context c;
TextView container;


/***
 * Construct the URLImageParser which will execute AsyncTask and refresh the container
 * @param t
 * @param c
 */



public URLImageParser(TextView t, Context c) {
    this.c = c;
    this.container = t;
}

public Drawable getDrawable(String source) {
    URLDrawable urlDrawable = new URLDrawable();

    // get the actual source
    ImageGetterAsyncTask asyncTask =
            new ImageGetterAsyncTask( urlDrawable);

    asyncTask.execute(source);

    // return reference to URLDrawable where I will change with actual image from
    // the src tag
    return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
    URLDrawable urlDrawable;

    public ImageGetterAsyncTask(URLDrawable d) {
        this.urlDrawable = d;
    }

    @Override
    protected Drawable doInBackground(String... params) {
        String source = params[0];
        return fetchDrawable(source);
    }

    @Override
    protected void onPostExecute(Drawable result) {
        try {
            // set the correct bound according to the result from HTTP call
            urlDrawable.setBounds(0, 0, result.getIntrinsicWidth(), result.getIntrinsicHeight());


            // change the reference of the current drawable to the result
            // from the HTTP call
            urlDrawable.drawable = result;


            // For ICS
            URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
                    + result.getIntrinsicHeight()));

            // redraw the image by invalidating the container
            URLImageParser.this.container.invalidate();
        } catch (NullPointerException ex){
            urlDrawable.setBounds(0,0,0,0);
            urlDrawable.drawable = result;

        }
        // Pre ICS
        URLImageParser.this.container.setEllipsize(null);
    }

    /***
     * Get the Drawable from URL
     * @param urlString
     * @return
     */
    public Drawable fetchDrawable(String urlString) {
        try {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            return drawable;
        } catch (Exception e) {
            return null;
        }
    }

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }
  }
}

如何解决我的问题?我想用图片填充TextView使用设备屏幕宽度或任何其他可以制作大尺寸图片的宽度。

2 个答案:

答案 0 :(得分:1)

如其他地方所述,您不能使用内在的衡量标准。您需要考虑屏幕像素和密度。换句话说,当需要图像不在自动缩放的对象中时,您需要计算图像的宽度。

这是一个很好的参考:

Android: How to stretch an image to the screen width while maintaining aspect ratio?

此外,您应该考虑使用“sp”而不是“dp”,因为您的图像与文本一致。阅读本文:

What is the difference between "px", "dp", "dip" and "sp" on Android?

换句话说,您的图像应该与文本以及屏幕尺寸和屏幕密度一起缩放。

答案 1 :(得分:0)

你的问题与解决方案有关。当你设置

setBounds(0, 0, result.getIntrinsicWidth(), result.getIntrinsicHeight());

在具有低分辨率的设备中,您的照片会很大,而在具有高分辨率的设备中,您的照片会很小。

因此,如果您想保持大小,请设置硬编码大小:

setBounds(0, 0, 100, 100);

或根据设备密度计算宽度和高度