使用ImageGetter在textview中缩放图像

时间:2014-04-23 23:20:05

标签: android textview android-image

我正在尝试缩放textview中显示的图像,但我不能。

我正在使用此代码但无论如何,它会显示在容器内裁剪的图像或根本不显示。

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 个答案:

答案 0 :(得分:1)

我回答自己,我已经改变了

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

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

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

现在它占据了屏幕空间的70%,这正是容器的最大尺寸