简体   繁体   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

I have an app in which HTML pages come through the API which contains images and text.我有一个应用程序,其中 HTML 页面通过包含图像和文本的 API。 I am able to display the text and parse the images as well in a text view.我能够在文本视图中显示文本并解析图像。 But the Image height and width are not perfect.但是图像的高度和宽度并不完美。 I am using drawable.setBounds() method to set the height and width of the image, but if I set it to static suppose drawable.setBounds(0, 0,1020,600) , the images come different in different screen sizes.我正在使用drawable.setBounds()方法来设置图像的高度和宽度,但是如果我将其设置为静态假设drawable.setBounds(0, 0,1020,600) ,则图像在不同的屏幕尺寸中会有所不同。 I want to scale the images according to different screen sizes.我想根据不同的屏幕尺寸缩放图像。 How can I do that?我怎样才能做到这一点? Below is my code of ImageParser.下面是我的 ImageParser 代码。

URLImageParser.java 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();
                }
            }
        }

Try this in your fetch drawable method :在你的 fetch drawable 方法中试试这个:

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

You can use this library to make things easier for you to manipulate drawable in different screen sizes.您可以使用这个库让您更轻松地操作不同屏幕尺寸的可绘制对象。

Here is the Library这里是图书馆

You can put the height and width in SDP it will manage itself for all screens您可以将高度和宽度放在 SDP 中,它将为所有屏幕自行管理

For example like this例如像这样

<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