如何设置高度和drawable.setBounds方法的宽度适当地在不同的设备中的TextView完美显示图像

问题描述 投票:1回答:2

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

URL image parser.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();
                }
            }
        }
java android
2个回答
1
投票

你可以使用这个库使事情更容易为你操纵不同的屏幕尺寸绘制。

这里是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" />

1
投票

在获取绘制方法试试这个:

drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
                        + drawable.getIntrinsicHeight());
© www.soinside.com 2019 - 2024. All rights reserved.