How to save images with Picasso so there is no loading time? - java

I have an app where I display a quote and there is a picture in the background. When I click on a "next"-button, the next quote will be shown and the new image will be loaded. The loading takes around 0.5 seconds and has the image before as placeholder. But when I switch back, there is no loading time.
This means, the image is saved somewhere, so it does not need to be loaded again. Unfortunately, this is just temporarily. When I get to see the next 5 pictures and go back to the first one, the first picture needs to be loaded again. So I tried loading all the pictures in the beginning (it is just 25 pictures), like this:
Picasso.get().load(backgrounds.get(1));
Picasso.get().load(backgrounds.get(2));
Picasso.get().load(backgrounds.get(3));
Picasso.get().load(backgrounds.get(4));
Picasso.get().load(backgrounds.get(5));
Picasso.get().load(backgrounds.get(6));
Picasso.get().load(backgrounds.get(7));
and when I click on my "next"-button, I use this:
Picasso.get().load(backgrounds.get(counterBackground)).fit().noPlaceholder().into(background);
But both things without the expected effect. The images need a loading time around 0.5 every time and "noPlaceholder" doesn't work like expected, the placeholder is still there.
So, does someone know how to cut the loading time? Like, how to load all images in the beginning?
Thanks for every answer!

You can create cache while using Picasso, this will lead to quicker loading times (as long the images are cached
Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(getCacheDir(), 250000000)).build();
Picasso.setSingletonInstance(picasso);
source
You can use indicators to see from where the image is loaded by using
setIndicatorsEnabled(true)
source
You can also use callbacks to have an indication when the image is completed loading
you can see more details here
Edit
You can use
Picasso.get().load(R.drawable.pic).into(imageview)
I think it will work also for mipmap, usually mipmap are used for errors / place holders for your resources (if you are not using drawables)
2.
You can create your own policies about how to cache/load your images
Link

Related

Libgdx - Remove HTML greybackground/recompile button/ load splash

What is the proper way to remove the grey background that covers the entire screen, the recompile button, and the default libGDX load and/or load splash in a HTML build of my game?
Note: This answer applies only to the gdx-setup tool as of late 2022. The gdx-liftoff tool is similar but has a slightly less boneheaded configuration out of the box. Additionally, I would like to get some of libGDX's HTML backend reworked one day, as there is no point in the padding and it's applied unevenly, plus less obvious things like the way it creates a table for layout.
Grey background
The background colour can be customised by changing background: #222222 in html/webapp/styles.css to some other colour. Or apply it directly to the body in index.html and delete styles.css (plus the link to it) as it doesn't contain anything important once the superdev button has been removed.
Grey border
The border around the game can be removed by editing HtmlLauncher like so:
#Override
public GwtApplicationConfiguration getConfig () {
GwtApplicationConfiguration config = new GwtApplicationConfiguration(true);
config.padHorizontal = 0;
config.padVertical = 0;
return config;
}
Separating GwtApplicationConfiguration into a config variable brings it in line with the other launchers (desktop, Android, iOS) and setting the padding to 0 is self-explanatory. While we're here, passing true into the app config's constructor tells it to render at native resolution on high-DPI/"retina" displays instead of upscaling.
Recompile button
Or the superdev button, as I call it. Just remove the <a class="superdev"... line from html/webapp/index.html. If you need access to it during development, it's recommended you add its link to your bookmark bar. Visibility of the bookmark bar can be toggled using Ctrl+Shift+B in Chrome and Firefox.
Load/splash screen
You're probably best referring to https://libgdx.com/wiki/html5-backend-and-gwt-specifics#changing-the-load-screen-progress-bar for this (which may not have existed when the question was asked). In short, getPreloaderCallback() and adjustMeterPanel() can be overridden in HtmlLauncher. I typically just overwrite logo.png after building instead of using the recommended method for changing the logo.
Other changes
Things you might want to change before a final release:
styles.css isn't very important beyond changing the background colour, as noted earlier.
In index.html, a comma should be added to between device-width and initial-scale for it to be valid HTML.
In index.html, applying align="center" to a div is deprecated behaviour. Probably best remove that alignment. If you need it, apply via CSS instead.
In index.html, handleMouseDown() and handleMouseUp() are completely pointless, as far as I can see. I don't use them for my own projects and have had no complaints.
html/build/dist/assets/assets.txt references some files that may not be necessary. The default font (arial or lsans, depending on libGDX version) is only needed if you use it and the vertex/fragment shaders are only needed if you do 3D stuff, I believe. Removing these can remove load times ever so slightly, especially on HTTP/1.1 connections. But I don't have an automated way to remove those lines (except on Linux - head -n -8).
Setting an asset filter as seen at https://libgdx.com/wiki/html5-backend-and-gwt-specifics#speeding-up-preload-process is an easy way to reduce your load times. I return false for music files to reduce load times greatly - it ends up streaming music instead of preloading it (if using Music, not AssetManager).

Reading and storing Java Icons

In my Java application I'm planning to use a lot of ImageIcons which will be shown in JLabels. The size of a single .png file is about 8 kB. During runtime, icons will be shown in JLabels, but the icons shown in the labels will change often, using .setIcon(icon).
Should I load all those icons at once when the program starts and store those in an array (length would be > 150) or should I load the icons every time I change an Icon as shown in the code below?
Note that the number of JLabels is rather small, so, at a given moment, only a selection of icons is shown.
ClassLoader cl = getClass().getClassLoader();
String path = "somePath";
URL url = cl.getResource(path);
Icon icon = new ImageIcon(url);
The application will be a jar file with the resources inside the jar.
This will be a bit cliche, but "Do not optimize your application unless it needs to be optimized." It makes maintenance more difficult in future.
Because, every program evolves in time, and the kind of the optimization required may not be the one you are planning to do at the moment. Your JLabels may increase in number, or your possible icons may increase. Or some other new feature comes up and you may need to display all the icons at the same time etc. etc.
So, just write your code as clean as possible, and when you see that an optimization is really required, do it after measuring where is the bottle neck.
PS: I am telling this, because you write "I am planning to use" so you did not actually finished the application and saw the effect of this many icon loadings.
PS2: OS and Disk caches are getting better everyday, so it may just be enough for the time being.

Resize static image file in Google App Engine

I have uploaded a static image (e.g., at location: /images/no-image.jpg) as part of my code that gets used as the default image when there is no specific image available (basically of the type: "no image for this item")
Just realized that this image may have to be served at more than one size (I have uploaded -- and want to upload -- just one size).
How do I resize this image using Google's Images Service?
If I use the example shown at https://developers.google.com/appengine/docs/java/images/#Java_Transforming_images_in_Java , that means having to read the file, apply the transform, then write a servlet to serve up the file... which seems like a lot of work. Is there an easier way?
Will it be easier to just upload the image to the BlobStore :-)?
You can add the following lines to your CSS file:
.placeholder {
background-image:url('../images/bg.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;
}
Now, when you don't have a specific image to serve, simply add "placeholder" class to your container element.
Remember to make this image cacheable.
I strongly recommend using the getServingUrl function for serving and resizing images for two reasons:
simpler to use. to resize, you just append parameters to the image url
It will significantly reduce the load off your instances, since the images are served without a servlet, but with google magic

Android how to do image screen slide?

I'm building a very basic gallery on android, it shows the last image on the camera folder and the user can slide left to see the previous one.
I finished an implementation using a viewpager, and a pagerAdapter using sampleSize to scale images. My problem is that the implementation is nowhere as efficient as the default gallery app, every time you slide an image you have to wait around 200ms for the next one to load. So basically my idea on how to implement it is not working out.
How can I do this efficiently, and if possible with an implementation that allows zooming in and out later on?
This looks promising but I don't know how to implement it with files instead of drawables
http://www.androidviews.net/2012/11/photoview/
EDIT:
I've managed to improve speed a bit using photoview (a hacked viewpager), however it takes too much to convert the filepaths to bitmaps or drawables.
I have tried these two methods suggested by dilix and iDroid Explorer:
// Method A
Drawable d = Drawable.createFromPath(imagePath);
imageView.setImageDrawable(d);
// Method B
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(myBitmap);
But both produce error Bitmap too large to be uploaded into a texture. I need to get the image to the imageView as fast as possible and somehow bypass this error and maybe I'll get a decent speed. Right now I'm scaling the images and converting them to a drawable but it's not very efficient.
Check this awesome website : http://www.androidviews.net/
and maybe this is what you want: http://www.androidviews.net/2012/11/photoview/
It has page scroll, zooming and panning as you need
I have used it in my project with some modifications and lazy image loading. It is working exactly as you need.
See the official documentation for a great example that will walk you through using bitmaps with a viewpager efficiently. You should use a cache of the images that have already been loaded, and the images should be loaded outside of the UI thread. You also want to scale the image to the minimum size needed for your display (usually powers of two - note that the image won't always scale to what you ask it to!)
ViewPager also loads images in the background to prepare for the next slide, which is set with the setOffScreenPageLimit method (the default is usually good, I think it's 2.)
https://developer.android.com/training/displaying-bitmaps/index.html
I am not sure whether you got correct solution or not. But if you want to continue with your own implementation with your given link then:
http://www.androidviews.net/2012/11/photoview/ //this link is given in your question
And you can see this SO for how to convert file's path in to the drawable. Now, you get drawable of your specified file and implement on the link you have given.
I think that link will works nice.
Feel free to comments.
1)
This looks promising but I don't know how to implement it with files
instead of drawables http://www.androidviews.net/2012/11/photoview/
You have to store not list of drawables in you adapter, but links to files on your sd card and then load it.
2) But i can imagine that file instead of drawables won't be so efficient because to load from files you have to parse it from sd card.
I think you can save time by loading several images in your memory simultaneously (compressed) and when you swype to your othe image you'll already have the image and memory and while swyping you can start loading other image in memory.
When fling over the gallery you may not load images and load it after almost stopping.
It's the main problem: memory consuming vs performance.
UPDATE
I think better than exploring this by yourself will be exploring existing code of google gallery: https://github.com/CyanogenMod/android_packages_apps_Gallery
As i checked - they store cache (almost as i've said about loading multiple bitmaps) and store only resized images - seems to be efficient.

Displaying bitmaps after sometime

I have a large number of bitmap files in a directory and I want to display them using the java code (something like a slideshow) I was able to display them using a Jpanel.
But the problem is when the next image gets displayed the jframe blinks.I want the next image to appear without blinking. What should I do??
Thanks in advance.
How are you loading and showing the new image (can you show your code)? Don't forget that this takes time and this may be what is causing your blinking or flicker.
Standard practice is to load the image and only change it when it has been prepared and buffered.

Categories

Resources