How to get the name of drawable through resource id? - android

I am trying to make the bitmap and for this I need the drawable name such as:
overlayScaledBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.monkey);
This is working fine But my problem is that I can not do this same thing when a user selects a picture from a list because I have done this statically. As you can see I have wrote R.drawable.monkey. So its means every time it is going to create bitmap with monkey image.
But it is not the case, the user selects image which gets fixed into the image view. Now I want to get the ImageView as drawable. If it is possible then it could be dynamically and would be easy to handle. Any idea How I can get drawable of ImageView to use in bitmap scaling? please help

You could create an enum object containing all refs to the bitmaps and an id.
But another way is using getTag() and setTag() on the resources to identify by.

Related

Get the current drawable in the Imageview of appWidget

I'm trying to get the current drawable of the ImageView of the widget of my app. But there is no getDrawable() in appWidgetProvider. How to get the current drawable of the Imageview?
Ex. if I have current drawable A in the imageview, I want to change it to B. But first I want to check if it is A or B and then change accordingly.
How to get the current drawable of the Imageview?
That is not possible, sorry.
However, since you are the one who is defining what drawable is used by the RemoteViews, you can track this yourself, holding onto an appropriate identifier in a database, SharedPreferences, or other sort of file.

How to reset the resource of image view in android?

Trying to change the image in imageView dynamically based on the status.
Image changed successfully but problem is walk image overwrite on the run image. I want to clear the existing image and then set the new image.
status_image.setImageResource(if(runningState == RunningState.FAST) R.drawable.ic_run_fast else R.drawable.ic_walk)
Just call it with a "0" parameter before doing anything else:
status_image.setImageResource(0)
Nullability of drawable content is your friend, AndroidDocs nailed it: setImageDrawable
So in your case statusImageView.setImageDrawable(null)
Try to set the ImageView to transparent. Then change it back to the resource you want.
status_image.setImageResource(android.R.color.transparent)
Another option is to use some third party lib to handle the images for you as well, like Glide, Fresco, or Picasso.

Is imageview needed within this imageview?

I use an ImageView to display the entire image. I want to launch the next activity when the sun is touched. Is it possible by using this single image or should i use separate imageview for sun? I know how to do it with separate imageview. I want to know whether it is possible by using only the full image?
Use two imageviews
Easier and less bug-prone

Does bitmap of an image remains constant throughout the lifecycle of the android application?

Okay so this might be a very silly question to ask.
Does a bitmap of an image remains constant traversing through different activities in the android.
Well my issue is :
I have an activity with image view having a default image.
I am previewing another image picked from the gallery if the user wants it.
So now then i move on to the next activity displaying the image user picked from the gallery.
Now if the user hasn't picked any image i want to show another default image.
Please note not the 1st default image in 2nd activity.
If a bitmap remains in the drawables folder, then you can access it from every Activity and it will remain "constant" so to say (if that's what you meant). If you're using the bitmap in the code (to set it as the background of the ImageView), then this can be changed - othwerise if in drawables - it's just usable, you can manipulate it, but you will never change its basic status (in the folder).

Do multiple ImageView's holding the same image create extra Drawable objects or Drawable instances?

I currently have a ViewFlipper that holds the same ImageView in each screen. The issue is that I have to create an ImageView[] array with a unique ImageView per screen in the ViewFlipper in order to add them to the ViewFlipper since I run into the child already has a parent issue when using the same ImageView. They all refer to the same resource in R.drawable.
My question is this: does every ImageView in the array create a separate instance of the drawable object or do they each simply contain a reference to the same drawable object? Also, are ImageView instances resource intensive? I'm worried that this would run into overhead issues, since this ImageView array isn't the only one.
As far as drawables loaded from the same resource share a common state
it seems that android architects have thought of this question and the resources are used for 1 image only, so you won't get overhead in such way.
In addition: BitmapDrawables created from the same resource will for instance share a unique bitmap stored in their ConstantState.

Categories

Resources