gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:40:08 24 4
gpt4 key购买 nike

我正在从 URL 加载提要并获取包含我放入 TextView 中的图像的文本,但是 TextView 中的图像在超过 3.5 英寸的显示器上很小.我猜问题出在 setBounds 方法中。这是我的类(class):

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 使用设备屏幕宽度或任何其他可以制作大尺寸图片的宽度。

最佳答案

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

这是一个很好的引用:

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?

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

关于android - 使用 URL 用屏幕宽度的图像填充 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25232275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com