获得可绘制的imageView的大小,以用作占位符 [英] get drawable exactly the size of the imageView to use as a placeholder

查看:74
本文介绍了获得可绘制的imageView的大小,以用作占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通用图像视图将图像加载到我的应用程序中.

I am using Universal Image View to load images into my application.

这是我实现的方法

public static void setBitmapToAImageviewUIL(final ImageView imageView, String uriPath, String imageName)
{
  if(imageName != null && !imageName.equals(""))
  {
     //We use the local image as a temporary placeholder
     DisplayImageOptions options = new DisplayImageOptions.Builder()
           .showStubImage(Utilities.getDrawableResource(imageName))
           .cacheOnDisc()
           .cacheInMemory()
           .imageScaleType(ImageScaleType.EXACTLY)
           .bitmapConfig(Bitmap.Config.ARGB_8888)
           .build();

     imageLoader.displayImage(uriPath, imageView, options);
  }

}

逻辑是这样的:我在可绘制对象中有一些图像.首先,我将本地drawable用作占位符(showStubImage). Utilities.getDrawableResource()重新运行可绘制对象.完成下载后,将显示从服务器下载的图像.

The logic is this: I have some of the images in the drawables. First I use the local drawable as a placeholder (showStubImage). Utilities.getDrawableResource() retruns the drawable. Then the image downloaded from the server will be displayed when it finishes downloading.

我的问题

用作占位符的临时图像大于从服务器下载的图像.因此,首先图像很大,然后在完成新图像的下载后,图像将按比例缩小.如何使它们都与imageView大小完全相同?

The temporary image used as the placeholder is larger than the image downloaded from the server. So at first the image is big, then when the new image finishes downloading it scales down. How can I make them both exactly the same size of the imageView?

我正在尝试避免手动调整大小的代码以使其具有相同的大小.谢谢

I am trying to avoid manual resize code to make them the same size. Thanks

PS: Utilities.getDrawableResource()使用 getResource().getIdentifier()来获取正确的可绘制对象.

PS: The Utilities.getDrawableResource() uses getResource().getIdentifier() to get the correct drawable.

推荐答案

有几种不同的方法,具体取决于您想要实现什么以及您拥有多少信息.

There're a few different approaches depending on what you want to achieve, and how much information you have up-front.

一种方法是将图像视图设置为XML上的确切大小(例如 width:match_parent height:144dp ),然后在其中放置服务器和可绘制比例

One way is set your imageview to an exact size on your XML (e.g. width:match_parent height:144dp) and let both the server and your drawable scale inside that.

如果在下载之前可以访问图像服务器的大小,则可以将imageView layoutparams设置为这些确切值,然后将drawable调整为该大小(服务器将恰好适合)

If you have access to the image-server size before downloading it, you can set the imageView layoutparams to those exact values and let the drawable resize to that (and the server will just fit perfectly)

我认为您可以使用XML形状可绘制对象和/或9patch使其缩放更好,但是您仍然需要上述两种方法之一.

I'm thinking you could use XML shape drawables and/or 9patch to make it scale nicer, but you would still need one of the two methods above.

这篇关于获得可绘制的imageView的大小,以用作占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆