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.
Related
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
I wanted to know the best way of taking a list of images which come from an HTTP request, each image then needs to be retrieved.
I would then like to use the images to make an animation behaviour so it looks like the list of images is for example an animated gif. Moving between each image using a configurable time period.
This needs to be done using GWT. I saw that you can create a custom animation by using extends Animation and I also saw AbstractImagePrototype but I'm not sure what to use, also exists AnimationScheduler.
I made a simple example with javascript using setInterval but its not clear in GWT how this should be done.
Thanks for any help.
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
I want to know if there is any solution for the following scenario:
I have an application which uploads the files, after scanning and transcoding them, onto a server. Suppose, an image file is being uploaded which has been tampered with some additional contents over it. Now, as the uploaded file is illegitimate, I want to remove the additional tampered contents and upload just the original part of this image file. Is it possible to do so in Java?
Thanks.
It's not possible to detect in the general case, but there are some heuristic methods available to determine whether an image has been edited. Try using the tools at http://imageedited.com/ to get an idea of what's possible.
Removing the edit is a much more difficult problem, which is probably impossible with current methods.
I'm just speculating here, and I don't know how well it would work in practice, but you could do it if you limit to specific sources of tampering. E.g., suppose you want to remove the logo added to an image by memegenerator.net.
You know in advance what the text looks like and where it is. Create a transparent png template that matches the text. Then sum the differences between the image and template pixel colors, multiplying each by the alpha of the template pixel. Since for this particular logo, it's basically white (although it seems to have a thin black shadow) you would get false positives for a picture with a white part there, so you'd also need to verify that the surrounding pixels are (within a tolerance) not white. It's not clever but it could work for certain sites.
For anything more flexible (e.g., logos on images which have subsequently been resized) you're in to the territory of OCR and TinEye-like image matching, which are more advanced than I could advise you on.
To correctly detect all kinds of "tampering" and filter "illegitimate" from "legitimate" in general, you'd need an artificial intelligence that could understand the meaning and context of what it's seeing. The short answer is: you can't. That's what humans are for.
If this is for a website, probably the best thing you can do is a report button that lets users of your site report images that don't fit with your site's rules.
I want to be able to add a text-messaging balloon every time the user revives data from a HttpGet, I want it so that it looks nearly identical to the default Android text messaging UI. I'm fine with all the code, I just need a way to create the UI and create another text balloon every time data comes back from a HttpGet request.
Thanks ever so much, for the answering this questions and I'm sure there's an easy way to do it, yet I've found no way by using the 'ole Google.
I am doing something similar for my app am doing the following to achieve it:
You will need a 9-Patch-Image (a stretchable PNG, see here) that represents the bubble. You want to make the part stretchable that does not include the corners of the bubble. You can create the bubbles using an image editor of your choice (I'd recommend a vector graphics editor like Inkscape). Then use the 9-Patch editor included in the Android Developer Tools to transform the PNG image into a 9-Patch PNG.
Create a custom layout file for one bubble. Create a textview inside it, and add your bubble as a background resource. (android:background)
Use an arraylist with a custom adapter to inflate and fill your items.
So far, this will give you identical bubbles as background for all messages.
If you want to get fancy, you can create different bubbles for participants, and use the setBackgroundResource method in your Adapter to set the correct background.
Further, if you wish to align them left or right, like in the message app, you will need to add spacers to the left and right of your TextView in the layout file. I used FrameLayouts with a fixed width. Make sure to set their visibility to GONE.
As with swapping the different bubble colors, just set the visibility of the left/right spacer.