gpt4 book ai didi

java - 如何设置 drawable.setBounds 方法的高度和宽度以在 textview 中完美地在不同设备中正确显示图像

转载 作者:搜寻专家 更新时间:2023-11-01 09:21:48 28 4
gpt4 key购买 nike

我有一个应用程序,其中的 HTML 页面通过包含图像和文本的 API。我能够在 TextView 中显示文本并解析图像。但是图像的高度和宽度并不完美。我正在使用 drawable.setBounds() 方法来设置图像的高度和宽度,但是如果我将它设置为静态,假设 drawable.setBounds(0, 0,1020,600),不同屏幕尺寸的图像会有所不同。我想根据不同的屏幕尺寸缩放图像。我怎样才能做到这一点?下面是我的 ImageParser 代码。

URLImageParser.java

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

try {
URL url= new URL(source);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
source=uri.toASCIIString();


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
//Toast.makeText(context, source, Toast.LENGTH_SHORT).show();

Log.d("Resp",source);

return fetchDrawable(source);
}


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

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

// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ result.getIntrinsicHeight()));


}

/***
* 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,800,600);
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();
}
}
}

最佳答案

您可以使用这个库来让您更轻松地在不同的屏幕尺寸下操作可绘制对象。

这是 Library

您可以将高度和宽度放在 SDP 中,它会为所有屏幕自行管理

比如像这样

<TextView
android:id="@+id/tvImage"
android:layout_width="@dimen/_9sdp"
android:layout_height="@dimen/_18sdp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />

关于java - 如何设置 drawable.setBounds 方法的高度和宽度以在 textview 中完美地在不同设备中正确显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54529199/

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