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

标签 java android

我有一个应用程序,其中 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();
                }
            }
        }

最佳答案

在您的 fetch 可绘制方法中尝试一下:

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

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

相关文章:

java - 错误 java.lang.reflect.InitationTargetException

java - 在循环中等待 Action 监听器执行

java - map Pane 地理工具

android - Android 游戏开发者使用哪个 OpenGL/3d 框架?

Android设置图片为墙纸

java - IllegalArgumentException : Navigation action/destination packagename/action_xxx cannot be found from the current destination

java - 从方法返回二维数组的正确语法

android - 在 Google Glass 上模拟触摸板事件

android - 无法在 SD 卡上创建文件夹 - mkdir 总是返回 false

android - Android SDK从23升级到26构建错误