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

Java 如何设置drawable.setBounds方法的高度和宽度,以便在不同的设备中在textview中完美地显示图像,java,android,Java,Android,我有一个应用程序,其中HTML页面通过包含图像和文本的API。我能够在文本视图中显示文本和解析图像。但图像的高度和宽度并不完美。我正在使用drawable.setBounds()方法来设置图像的高度和宽度,但是如果我将其设置为static,则假设drawable.setBounds(0,01020600),图像在不同的屏幕大小中会有所不同。我想根据不同的屏幕大小缩放图像。我该怎么做?下面是我的ImageParser代码 urlmageparser.java public class URLIm

我有一个应用程序,其中HTML页面通过包含图像和文本的API。我能够在文本视图中显示文本和解析图像。但图像的高度和宽度并不完美。我正在使用
drawable.setBounds()
方法来设置图像的高度和宽度,但是如果我将其设置为static,则假设
drawable.setBounds(0,01020600)
,图像在不同的屏幕大小中会有所不同。我想根据不同的屏幕大小缩放图像。我该怎么做?下面是我的ImageParser代码

urlmageparser.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();
                }
            }
        }
公共类URLImageParser实现Html.ImageGetter{
上下文c;
文本视图容器;
/***
*构造URLImageParser,它将执行AsyncTask并刷新容器
*@param t
*@param c
*/
公共URLImageParser(文本视图t,上下文c){
这个.c=c;
这个容器=t;
}
公共可绘制getDrawable(字符串源){
URLDAWABLE URLDAWABLE=新的URLDAWABLE();
//获取实际来源
ImageGetterAsyncTask异步任务=
新的ImageGetterAsyncTask(URLDawable);
asyncTask.execute(源代码);
//返回对URLDrawable的引用,其中我将根据实际图像进行更改
//src标签
返回可提取;
}
公共类ImageGetterAsyncTask扩展异步任务{
URLDrawable URLDrawable;
公共ImageGetterAsyncTask(URLDawable d){
this.urlDrawable=d;
}
@凌驾
受保护的可抽出式doInBackground(字符串…参数){
字符串源=参数[0];
试一试{
URL=新URL(源);
URI=新URI(url.getProtocol()、url.getUserInfo()、url.getHost()、url.getPort()、url.getPath()、url.getQuery()、url.getRef());
source=uri.toASCIIString();
}捕获(格式错误){
e、 printStackTrace();
}捕获(URISyntaxException e){
e、 printStackTrace();
}
//Toast.makeText(上下文、源、Toast.LENGTH_SHORT).show();
Log.d(“Resp”,来源);
返回可提取的(源);
}
@凌驾
受保护的void onPostExecute(可提取结果){
//根据HTTP调用的结果设置正确的绑定
urlDrawable.setBounds(0,0,0+result.getIntrinsicWidth(),0
+result.getIntrinsicHeight());
//将当前绘图的参考更改为结果
//从HTTP调用
urlDrawable.drawable=结果;
//通过使容器无效来重新绘制图像
URLImageParser.this.container.invalidate();
urlmageparser.this.container.setHeight((urlmageparser.this.container.getHeight)()
+result.getIntrinsicHeight());
}
/***
*从URL获取可绘制的内容
*@param-urlString
*@返回
*/
公共可提取可提取(字符串urlString){
试一试{
InputStream is=fetch(urlString);
Drawable Drawable=Drawable.createFromStream(是“src”);
可拉深.立根(0,0800600);
回拉;
}捕获(例外e){
返回null;
}
}
私有InputStream获取(字符串urlString)引发错误的InputStream异常,IOException{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet请求=新的HttpGet(urlString);
HttpResponse response=httpClient.execute(请求);
返回response.getEntity().getContent();
}
}
}

在可提取的方法中尝试以下操作:

drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
                        + drawable.getIntrinsicHeight());

您可以使用此库使操作不同屏幕大小的drawable变得更容易

这是最新的

您可以在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" />


为什么需要将图像缩放到不同的大小?在图像视图中显示?我必须在文本视图中显示,因为在api中它是这样的,请检查示例api,元素“content”@SuhriAbhro为什么要在文本视图中显示drawable?因为我必须这样做。这是应用程序的要求,HTML页面将显示没有网站或实际页面,但来自api,页面设计在编辑器中进行。您是否使用textView作为wrapContent?我已经尝试过了。问题是,如果图像高度和宽度很小,假设520*300,则在文本视图中显示的图像太小。我想要的是得到高度和宽度的百分比,就像在HTML或CSS中,它可以适当地缩放图像。或者,我有没有办法检查设备大小并设置一个条件,如果设备大小为4英寸,那么一部分代码将在其他地方运行?