简体   繁体   English

如何检测窗口小部件是否在屏幕上?

[英]How to detect if a widget is on/off screen?

I'm writing a customized view similar to Grid which will be used to hold a large amount of images. 我正在编写一个类似于Grid的自定义视图,该视图将用于保存大量图像。 I'm using ImageButton to display the images, and I only want to load images onto the buttons once they are on the screen (and just display a static background image when they are not). 我正在使用ImageButton来显示图像,并且我只想在按钮在屏幕上后将它们加载到按钮上(而当按钮不在屏幕上时只显示静态背景图像)。 The reason for it is that I'm fetching images from some back-end service and I only want to fetch them when the buttons are on the screen. 原因是我要从某些后端服务中获取图像,而我只想在屏幕上有按钮时获取它们。

To do that, I'd like to know if the widget is on the screen. 为此,我想知道窗口小部件是否在屏幕上。 Is it possible to detect if a widget like button is on/off screen? 是否可以检测按钮之类的小部件是否在屏幕上? If not, any suggestions on how to achieve what I'm trying to do? 如果没有,关于如何实现我正在尝试做的任何建议?

I don't think you can. 我认为你不能。 The framework will just send you an onUpdate request when the widget is created, and then again according to your update policy. 框架将在创建窗口小部件时向您发送一个onUpdate请求,然后根据您的更新策略再次向您发送。

I don't see why there would be a reason to change the image when the widget isn't being shown. 我不明白为什么当不显示小部件时会有理由更改图像。 You're not saving the OS any resources by doing so; 您这样做不会节省OS的任何资源; it already knows not to draw your widget when it's off-screen. 它已经知道在屏幕外时不绘制小部件。

You can use the following from Android 2.1 an onwards to check if the screen is on. 您可以从Android 2.1及更高版本开始使用以下命令检查屏幕是否打开。

PowerManager mPower = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean screen_is_on = mPower.isScreenOn();

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

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