I am trying to change an image in my app depending on the position of the phone using the accelerometer. This isn't where I'm stuck, its not to hard to do.
I have an array of images from my drawable folders that change depending on the angle of the device. This works fine (switching the image of the imageview), with low res images but what would like to know is, is there a way or preloading images in java like I can in JavaScript when creating rollover images. So the hi res images would change seamlessly like the low res images.
Yes.
Create a subclass MyApp of Application.
In MApp.onCreate() you can preload your images; e.g.
image1 = getResources().getDrawable (R.drawable.image1);
And in your app, you simply do something like
image1 = ((MyApp)getApplication()).getImage1();
where getImage1 is a getter you provide in MyApp.
Hope this works for you!
https://github.com/luckybilly/PreLoader can resolve your problem.
you can pre-load almost everything you need with PreLoader. And you don`t need to worry about it not completed when you should get it: DataListener.onDataArrived(data) will be called after pre-load is completed
Related
I have a big set of images saved in one atlas, but due to the 2048*2048 size limitation of a single page I had to split my packs in 12 pages.
My problem is that when I use atlas.findRegion(imageName).getTexture() to load the texture, it loads the whole 2048*2048 page and not the single texture inside the page (the image pack contains the correct image though, so I know it's loading the correct file, I just don't understand why it's not cropping the texture around the image I pass as parameter to findRegion).
Thanks in advance, hope the question is detailed enough to receive an answer.
The problem was actually calling the getTexture() method, I just needed to load the Region. More info here loading regions of textureatlas in libgdx loads the entire file
I have a view model exposing a list of Android.Graphics.Bitmap (if there is something else better, please let me know, thanks).
So in reality, the images data will be fetched from some service (represented as bytes), the bytes then will be converted to Android.Graphics.Bitmap - I've not tested that case to see if there is any problem. Now in development (on client side) process, I would like to use just 1 sample image resource added to the drawable folder. If exposing some list of resource ids to help show the images, everything is just fine. However what exposed is a list of Android.Graphics.Bitmap, and I've tried using BitmapFactory.DecodeResource method to convert the added image resource id to a Bitmap, like this:
//in the context of some Activity
var images = new List<Bitmap>();
for(var i = 0; i < 30; i++){
var bm = BitmapFactory.DecodeResource(Resources, Resource.Drawable.SampleImage);
images.Add(bm);
}
The code above runs throwing an OutOfMemoryException (at the line var bm...).
I'm just a beginner in Android programming, so it's really hard for me to solve this problem. The exception is clear, looks like the memory is limited but I don't like to reduce the image size, resolution or something to make the code run (because if showing the images via resource id, everything is just fine, the image is shown with just what it should be without any degradation in quality).
I think java developer can also help with this, here is the code in java:
//don't need to add the bitmap to any list, just like this will
//throw the exception
for(int i = 0; i < 30; i++){
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.sampleImage);
//...
}
Thank you for your help!
UPDATE
Per my deeper debugging, the exception is thrown at the 20th loop (i=19). Also the device I'm using is an emulator (Visual Studio Emulator for Android), maybe that's some kind of limit of the emulator. I'm not sure how it will be for actual physical device. BUT I can run many apps (even a 3D game) in the emulator, so looks like the emulator is just fine.
you are flooding the memory provided for you with your bitmaps. you should take a look at Picasso library to load images efficiently and also the recycler view to reduce the burden of loading images.
Is there a way to load the images that will come on the screen soon as opposed to loading the images that are currently in view?
If you're asking how to pre-load images so they are freshly cached, so that when the corresponding ImageView comes on screen the loading is nice and fast, you could use (somewhat untested):
String uri = getUriOfImageAboutToComeOnScreen(...);
ImageLoader.getInstance().loadImage(uri, null /* or use a listener */);
Of course, the magic contained in getUriOfImageAboutToComeOnScreen() is up to you :)
Im creating a simple application that converts the image dimensions to a size that the user wants, im laying it out over a 3 screens
the first screen allows a user to select an image and it is displayed on screen.
the second screen then displays the attributes of the file (path, name, height and width) with the option of adjusting the name, height and width.
the third screen displays the resized image with an option to save the new image.
at the moment im only passing the image url between the classes and decoding the bitmap within each class eg
Bitmap bmap = BitmapFactory.decodeImage(image_URL);
my question is it better to pass the URL between the classes or pass the Bitmap?
many thanks
You have several options:
Share the image through static data - Preferably in your
Application class. Example: Using a class to store static data in Java?
Pass the image through the Intent data which is possible, but
highly discouraged.
Pass the URL as you suggest and re-open and resize it each time.
Use a combination. For example, you should pass only image
information to your second screen which displays said information.
I suggest that the first bullet is best practice and will work well for you.
That should be the correct approach. Passing Bitmap shouldn't be done from one activity to another. Passing the path is always recommended
I think that dependent on where is your image if (you have the image on device ) I think the best is to pass Image path , but if you have the image on web and you have to download it I think passing bitmap is better than pass URL(Just to decries internet connection & faster for the user )
I develop an application in which I want to display a grid with a list of images. For each image I create an instance of a class myImage. MyImage class, extends JCompoment and create an thumbnail and after draw it with overide thepaintCompoment(Graphics g).
All is ok, but in big size images I have a lot of delay to create the thumbnail.
Now I think to when I scan the folders for image(to create the list I said above to create an thumbanail of each image and save it to disc. For each image I will have a database record to save the image path and thumbnail path.So is this a good solution of the problem?
Is there a way to get the thumbnails of the system creates for each image, in file manager. Or a more effective solution than I try.
Thank you!!
Your best bet is to use something like imagemagick to convert the image and create the thumbnail. There's a project called JMagick which provides JNI hooks into Imagemagick, but running a process work too.
Imagemagick is heavily optimized C code for manipulating images. It will also be able to handle images that Java won't and with much less memory usage.
I work for a website where we let users upload art and create thumbnails on the fly, and it absolutely needs to be fast, so that's what we use.
The following is Groovy code, but it can modified to Java code pretty easily:
public boolean createThumbnail(InputStream input, OutputStream output){
def cmd = "convert -colorspace RGB -auto-orient -thumbnail 125x125 -[0] jpg:-"
Process p = cmd.execute()
p.consumeProcessErrorStream(System.out)
p.consumeProcessOutputStream(output)
p.out << input
p.out.close()
p.waitForOrKill(8000)
return p.exitValue()==0
}
This creates a thumbnail using pipes without actually writing any data to disk. The outputStream can be to a file if you wanted to immediately write it as well.
One way to avoid OS dependance is to use getScaledInstance(), as shown in this example. See the cited articles for certain limitations. If it's taking too long, use a SwingWorker to do the load and scale in the background.
I haven't used it for the creation of thumbnails, but you may also want to take a look at the ImageIO API.
ImageIO