简体   繁体   English

在运行时加载图像,具体取决于ImageView的大小

[英]Load an image at runtime depending on the size of ImageView

I need to load a scaled down version of an image programmatically at runtime depending on the size of ImageView . 我需要在运行时根据ImageView的大小以编程方式加载按比例缩小的图像版本。

For this I intend to use BitmapFactory as described in http://developer.android.com/training/displaying-bitmaps/load-bitmap.html . 为此,我打算使用如http://developer.android.com/training/displaying-bitmaps/load-bitmap.html中所述的BitmapFactory

Also I need to load it again if the size has changed, so that if the view would get bigger, image would still remain high quality, and of course if it would get smaller, I would free up some memory. 另外,如果大小已更改,我还需要再次加载它,这样,如果视图变大,图像仍将保持高质量,当然,如果它变小,我将释放一些内存。

I could override one of onSizeChanged , onMeasure , onLayout , but I don't know which way would be the most appropriate way, maybe even none of these :). 我可以重写onSizeChangedonMeasureonLayout ,但我不知道哪种方法是最合适的,甚至可能都不是:)。

So what I am asking is for advice when and where should I load my image? 因此,我要问的是在when and where should I load my image?

Note: Just a bit of information what I am going to do next, just in case if it affects where and when the image should be loaded. 注意:仅提供一些信息,我接下来将要做什么,以防万一它影响到应在何时何地加载图像。 After this I will work on loading image in chunks if it is still to big to load in one shot. 此后,如果一次加载仍然很大,我将分块加载图像。

我想你对位图有想法,所以可以使用位图在后台加载图像和线程以供执行,然后在位图中传递图像值。

It looks like incorporating image loading into layout mechanism was not a good idea, because most of the time this results in unnecessary work during layout. 将图像加载合并到布局机制中似乎不是一个好主意,因为在大多数情况下,这会导致在布局期间进行不必要的工作。

Since I am developing a framework, my subclass of ImageView can be re-sized in various cases, for example: 由于我正在开发框架,因此可以在各种情况下调整ImageView的子类大小,例如:

If orientation change has occurred and ImageView dimensions are relative to device dimensions - it will get re-sized (if I am handling orientation change myself, since by default android recreates whole view hierarchy), but using inSampleSize option when loading with BitmapFactory - usually bitmap size will still be a bit larger, inSampleSize works with powers of two only. 如果方向发生了变化并且ImageView尺寸是相对于设备尺寸的-它会被调整大小(如果我自己处理方向更改,因为默认情况下android会重新创建整个视图层次结构),但是在使用BitmapFactory加载时使用inSampleSize选项-通常是位图size仍然会更大一些,inSampleSize仅使用2的幂。 So this does not necessarily mean that I will have to reload the bitmap. 因此,这并不一定意味着我必须重新加载位图。

If I am setting ImageView dimensions explicitly - I know if I have to reload bitmap or not and I can do both things in one shot. 如果我明确设置ImageView尺寸-我知道是否必须重新加载位图,并且我可以一次完成这两项操作。

There are more ifs and each case can have it's own optimizations that will save both - processing power and memory. 如果有更多if,则每种情况都可以进行自己的优化以节省成本-处理能力和内存。

Since no correct answer was provided, I hope my solution will by useful to someone else as well. 由于没有提供正确的答案,所以我希望我的解决方案也对其他人有用。

If You have a better idea how this kind of issue should be handled - please share it here with everyone, so that I and everyone else could improve their knowledge a bit :). 如果您对如何处理此类问题有更好的想法-请在此处与大家分享,以便我和其他所有人可以稍微提高他们的知识:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM