繁体   English   中英

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

[英]How can i set the height and width of drawable.setBounds method to display the image properly in different devices perfectly in textview

我有一个应用程序,其中 HTML 页面通过包含图像和文本的 API。 我能够在文本视图中显示文本并解析图像。 但是图像的高度和宽度并不完美。 我正在使用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 方法中试试这个:

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

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

这里是图书馆

您可以将高度和宽度放在 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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM