Remove the image from a imageview Android [duplicate] - java

This question already has answers here:
How to clear an ImageView in Android?
(18 answers)
Closed 4 years ago.
I'm trying to make an ImageView that holds a gallery of images. By touching the user request to load the next image. If the next image isn't found in the server or takes time to load I need the old image to be empty.
setVisibility(View.GONE) or setVisibility(View.INVISIBLE) don't work for me because when invisible/gone I stop the onTouch() detecting (and the user is locked to current image).
How can I make the ImageView to load a empty bitmap or clear (remove) current bitmap?

I always use
imageView.setImageDrawable(null);

Try:
imageView.setImageResource(0);
This will set the image view to use no resource.

From what I've noticed, the "working" or not of certain method when clearing image depends on the method used to populate ImageView.
So if you set img.setImageBitmap(bmp) then to clear you should use img.setImageBitmap(null).
When you img.setImageResource(resId) then to clear you should use img.setImageResouce(0).
Etc.

Certainly imageView.setImageResource(0) works. It has never failed for me and I have used it many times.
setImageResource is usually passed the reference R.drawable,(the reference for the picture), which is stored as an int, but displayed in the R.java class as a hex value, 0xf2fs... So assuming this reference exist it will show a picture, if you later pass that same imageview a reference which does not exist the old picture will no longer show. So, if you pass it 0, or 5 or an int which does not match a resource referenced in your R.java class it will remove the picture completely from the src of the imageView. So if you are passing 0 to the old reference of the imageView.

Related

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

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

How do I store several .jpg and .png files in an array? Android Studio

So here is a full explanation.
Currently, I'm building an Android app wherein there will be 3 to 5 images at a time. Then, after pressing an imagebutton (like ¨Forward¨ or ¨Next¨), another set of 3 to 5 images will appear.
The set of images will be as follows: first, a .jpg image at the background; second, an smaller .png image in the middle; third, an even smaller .png image up front; along with an imagebutton. And so on.
I'm using Android Studio. I know that I should connect an array in the .java file with ImageView in the .xml file using an object. But, I still can't picture the code in my head.
I thought of using an array, otherwise my .xml file will be massive. I assume. I will do some trial-and-error and post my results, but any help is welcomed.
Store the images inside res/drawable folder. Suppose the name of the images will be , a.jpg a1.png a2.png. Now in your java code you can create an integer array like
int icons[] = {R.drawable.a, R.drawable.a1, R.drawable.a2};
Now you can use these values according to your requirements.
save the image names which are there in res/drawable folder to an array of strings, and set the ImageView src by the names present in that array.
At first, create a list of all image resources:
List<Integer> myImages = Arrays.asList(R.drawable.image1, R.drawable.image2, R.drawable.image3);
Wherever in your code, just access a random or a specific image as follows:
int currentIndex = 0; // your index
myImageView.setImageResource(bgImageList.get(currentIndex));

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.

Android Change image depending on a global variable

the question is simple.
I have 2 images in PNG format (logo1.png and logo2.png) in the project. Currently the project is loaded (in a imageview) the logo1.png, but I would do, depending on the value of a variable load the logo1.png or load logo2.png in imageview control.
The project currently has 20 Activitys with this picture (each with its own layout in XML), I will not be changing code on the 20 screens, it could do with a simple instruction to verify the value of the variable, but would have to make change in the 20 screens.
wonder if there is no way to do that depending on the value of a variable, change the image in the ImageView.
will be able to access the value of the variable from the same XML?
Thanks in advance.
I'm a bit confused, but I'll give it a shot.
Yes, you can have a global variable where you define which image will load. But from my understanding, you would need to change the code in the activities that load that image to make them load the image dynamically via the code-behind.
I suspect it would be something like this:
Get info from the database indicating what value to load.
SetImageToLoad(someValue)
In each class that loads the image, you'll need to retrieve the value that you previously set in Step #2.
public class HelperClass
{
int resIDOfImageToLoad = 0;
public static void SetImageToLoad(String imageName)
{
if(imageName.equals("abc"))
{
resIDOfImageToLoad = R.id.abc;
}
else if(imageName.equals("xyz"))
{
resIDOfImageToLoad = R.id.xyz;
}
}
public static int GetResourceIDOfImageToLoad()
{
return resIDOfImageToLoad;
}
}
Then in the class that needs to load the image, you would call something like this
ImageView myImage = (ImageView)findViewById(...)
myImage.setImageResource(HelperClass.GetResourceIDOfImageToLoad());
If i understand you correctly. Create a new XML View of the image e.g. logo.xml and use it in all 20 Views and when you want to change that image change only in the logo.xml.
Assuming you have a base Activity class, you can define a method getLogo() in the base class that would return the png (or the filename or whatever suits you). Then just call that method when inflating the layout.
Initially, you'd have to change all the Activities, but after that you just need to change the base class if you decide to change the logic that chooses which image to show. (If this is not what you intended, please clarify your question).
I would recommend using Style or Themes. Read over the section that talks about inheritance, and declare a separate Style for each logo. You can then reuse the Style in each one of your XML files.
If you decide to programmatically determine which image to use, you can declare a static method that can be used application wide to determine which logo to use in each Context, then setImageResource() accordingly.

How to remove the background of a (regular) view (Android)

I have the following code:
View view = new View(this);
view.setBackgroundDrawable(...);
...
And here I want to remove that background.
Just turn it back as it was before.
I tried these and failed:
view.setBackgroundDrawable(null);
view.setBackgroundColor(0xFF000000);
view.setBackgroundColor(0x00000000);
Any more ideas?
view.setBackgroundDrawable(null); should work.
You may try one of these:
v.setBackgroundColor(Color.WHITE);
//or
v.setBackgroundColor(Color.parseColor("#ff0000")); //whatever color
Make sure the view you're applying the background to is the correct instance.
That's because view.setBackgroundColor(int) expects a color resource not an "actual" integer value. Try declaring it in your colors.xml, see this. However, I'm not quite sure what you mean by "removing" the background. If you want it to have the original value, then I suggest you store the original drawable (using getBackground()) somewhere. Otherwise you will most likely lose formatting, since most default backgrounds in Android are Drawable resources (selectors), not simple colors.

Categories

Resources