Scale images inside textview using ImageGetter

半腔热情 提交于 2019-12-13 04:23:19

问题


I'm trying to scale the images showed in the textview but i just can't.

I'm using this code but no matter what, it shows the image cropped inside the container or doesn't show at all.

int width, height;
DisplayMetrics metrics = new DisplayMetrics();
metrics = Resources.getSystem().getDisplayMetrics();

int originalWidthScaled = (int) (result.getIntrinsicWidth() * metrics.density);
            int originalHeightScaled = (int) (result.getIntrinsicHeight() * metrics.density);
            if (originalWidthScaled > metrics.widthPixels) {
                height = result.getIntrinsicHeight() * metrics.widthPixels
                        / result.getIntrinsicWidth();
                width = metrics.widthPixels;
            } else {
                height = originalHeightScaled;
                width = originalWidthScaled;
            }
                urlDrawable.drawable = result; 

                urlDrawable.setBounds(0, 0, 0+width, 0+height);

                // change the reference of the current drawable to the result 
                // from the HTTP call 

                // redraw the image by invalidating the container 

                container.invalidate();

                // For ICS
                container.setHeight(
                        container.getHeight() + 
                        result.getIntrinsicHeight());

                // Pre ICS
                container.setEllipsize(null);

回答1:


I answer myself i've changed

 if (originalWidthScaled > metrics.widthPixels) {
                height = result.getIntrinsicHeight() * metrics.widthPixels
                        / result.getIntrinsicWidth();
                width = metrics.widthPixels;
            }

for

if (originalWidthScaled > (metrics.widthPixels * 70) / 100) {
                width = (metrics.widthPixels * 70) / 100;

                height = result.getIntrinsicHeight() * width
                        / result.getIntrinsicWidth();
            }

And now it occupies the 70% of the space of the screen which is exactly the max size of the container



来源:https://stackoverflow.com/questions/23256955/scale-images-inside-textview-using-imagegetter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!