Preparing LruCache in Appwidget

114 Views Asked by At

i have an appwidget which starts an activity as user clicks it.

In my Activity i have a gridview containing relatively small Drawables(Images, because drawable could be mor than just Images) but user can size them.

I noticed that it takes too Long to size them at runtime when Scrolling trough the gridview.

I want to prepare an lruCache only one time in my appWidget's onUpdate which is called at the very beginning when the user places the appwidget on the Screen.

The problem

When i define an lruCache in my appWidget with

private final int lruCacheSize = (int) (Runtime.getRuntime().maxMemory()/1024);
private LruCache<String, BitmapDrawable> myLruCache;
...
myLruCache = new LruCache<String, BitmapDrawable>(lruCacheSize) {
            @Override
            protected int sizeOf(String key, BitmapDrawable value) {
                // TODO Auto-generated method stub
                return super.sizeOf(key, value);
            }
        };  

Will it only exist as Long as the process exist of the appWidget? Or does the Cache-File from lruCache stays in my cacheDir of my app? Or will it be deleted after the process of the appWidget is finished? If it does exist over the process-lifetime of my appWidget, how can i Access it from my Activity?

I don't want to create everytime the user clicks on the appWidget a LruCache and fill it up with all the relatively small Images the gridview will Need later. I want to do it once, or if user clears the Cache which will be checked every hour(to save battery).

The Question

How can i achieve that? Or is there a much better/ simpler way.

Time is not really (if it happens once at the very beginning) the Problem, i notify the user that the Cache is being prepared when placing the appWidget on the Screen.

Any help is appreciated.

Update regarding CommonsWare answere

Use Traceview to determine specifically why your implementation is slow.

The app Scrolling is slow because i provide three sizes (small, medium, large) and i scale them at while Scrolling trough my gridview.(Because it would take several seconds to scale them once at activity Startup, so i don't want that).

See here what i do in my imageview which will later Show the drawable(which is an appIcon):

    android:scaleType="fitXY"
    android:adjustViewBounds="true"

This causes the lag because i do this for everyimage depending if selected scale size is small medium or large.

There is no "Cache-File" in your code.

So then i i'm misunderstanding something. Doesn't the lruCache create an Cache-File in the Cache-Directory of my application? If not how does in works?

First, you cannot force the user to install the app widget. Work on solving the actual performance problem, rather than trying to build some optimization that will not help all users.

My "app" is only an appwidget. There is not appIcon like at FaceBook. It is only an appWidget when he doenloads my app. Which starts an activity when you click on the button.

Second, your process can readily be terminated milliseconds after onUpdate() completes. Do not fill a cache, only to have it never be used.

I want to use the Cache which i want to fill with the Drawables in the onUpdate of the appWidget, and then i want to use These Drawables from the Cache in my activity. So i don't understand why i never would use the Cache? Maybe i'm misunderstanding something.

Picasso would give you better performance from the user's standpoint.

Does it fit my Needs after the update right now?

---------------------------------------------------------------------------------------------

Update 2

Since the scaling should be done by the GPU and take microseconds, I have difficulty believing that is your problem. What specifically did Traceview show you that made you think that scaling has something to do with this?

I noticed that the Scrolling is very fluid at medium size because that's nearly the origin size of the appIcon from the PackageManager. If scroll trough large, where only 2 or 3 appIcons are displayed per row (at medium there are 5 or 6 displayed but it is much more fluid)m it lags. (With the same logic behind it). So the only Logical answere can be scaling in the XML at the ImageView. As i commented that XML-Scaling out and scaled the appIcons to the size Large and put them directly scalled to to my Adapter for GridView it runs really smooth( Only one or to really small lags at the beginning because convertView is null??)

onUpdate() will be called much more frequently than the user will actually use your app widget.

That's right after every onclick or at the specified time. But i check if the Cache has been cleared or not, if not don't Change anything, if so load Drawables to Cache.

1

There are 1 best solutions below

2
On

I noticed that it takes too Long to size them at runtime when Scrolling trough the gridview.

Use Traceview to determine specifically why your implementation is slow.

Will it only exist as Long as the process exist of the appWidget?

It will only exist for the lifetime of the process of your app.

Or does the Cache-File from lruCache stays in my cacheDir of my app?

There is no "Cache-File" in your code.

I don't want to create everytime the user clicks on the appWidget a LruCache and fill it up with all the relatively small Images the gridview will Need later. I want to do it once, or if user clears the Cache which will be checked every hour(to save battery).

First, you cannot force the user to install the app widget. Work on solving the actual performance problem, rather than trying to build some optimization that will not help all users.

Second, your process can readily be terminated milliseconds after onUpdate() completes. Do not fill a cache, only to have it never be used.

Or is there a much better/ simpler way.

Use Traceview to determine exactly where your problem lies. Then, solve that problem. For example, the problem could be that you are loading these images on the main application thread, and using a library like Picasso would give you better performance from the user's standpoint.


The app Scrolling is slow because i provide three sizes (small, medium, large) and i scale them at while Scrolling trough my gridview

Since the scaling should be done by the GPU and take microseconds, I have difficulty believing that is your problem. What specifically did Traceview show you that made you think that scaling has something to do with this?

Doesn't the lruCache create an Cache-File in the Cache-Directory of my application?

No.

If not how does in works?

It is an in-memory cache.

So i don't understand why i never would use the Cache?

onUpdate() will be called much more frequently than the user will actually use your app widget.