I am creating an Android custom View that allows the user to set a Drawable using both resource id and an instance of a Drawable itself.
The problem is that after a configuration change (eg: change of orientation), the Activity is destroyed and recreated and, with it, also my custom View.
For everything that is parcelable I am using the onSaveInstanceState and onRestoreInstanceState methods, so no problem to save the DrawableRes id, but a generic Drawable instance is not parcelable nor serializable.
The Drawable I want to save is really small, it is just 24x24 dp.
I thought about converting the Drawable to Bitmap, that is parcelable, but I would loose the support of of most of the subclasses, like AnimatedVectorDrawable and LayerDrawable.
Related
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.
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.
What's the difference between drawables and bitmaps? + What's the meaning of a BitmapDrawable?
I just don't understand why all pictures could be used as Bitmap, and only as Bitmap (or Drawable).
Also, what are the pros and cons?
Quoting the documentation for Drawable:
A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user.
In Android's class library, a Bitmap is the data about a raster image, such as one loaded from a PNG or JPEG file.
What's the meaning of a BitmapDrawable?
A Bitmap does not know how to draw itself; the Android class library developers elected to put that logic in a BitmapDrawable.
While trying to implement small in-memory cache of Drawables, I learned that to avoid memory leaks after closing activity I need to unbind those Drawables: set their callback to null.
Because maintaining Drawables cached in each activity would require extra code, I tried to unbind them immediately after setImageDrawable(drawable) and I don't see any consequences so far.
This is code from MyImageView class (extends ImageView):
setImageDrawable(drawable);
d.setCallback(null);
In debugger I can clearly see that before first line callback is null, after first line it is set to this imageView, and after that I set it to null again. It is normally shown after that..
Documentation for setCallback (Drawable.Callback cb) states:
Bind a Drawable.Callback object to this Drawable. Required for clients that want to support animated drawables.
Since I don't need animated drawable, I don't see why I shouldn't do this but it bothers me that in several blogs about memory leakage in Android concerning drawables this is done only after activity is done. Question is, why is callback always automatically set when binding to ImageView?
Are there some border conditions where those drawables with callback set to null will cause a problem? Not displaying or NPE?
You should not cache Drawables -- the Drawable object is very stateful, and intended to be used by one and only one owner.
If you want to implement a cache, you should be caching the drawable's constant state.
The constant state is retrieve with this:
http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getConstantState()
(Note this method can return null; not all Drawables have constant state.)
You can later instantiate new Drawables from a constant state with this:
http://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState.html#newDrawable(android.content.res.Resources)
Also keep in mind that Resources already maintains a cache of Drawables for you, using this facility, so there is no need for you to implement your own cache for any Drawables you are retrieving from Resources.
And if you are making your own Drawables outside of resources, I would strongly recommend making a cache of the underlying data (such as a bitmap downloaded from the network) then trying to mess with the constant state. (And again, definitely don't cache Drawable objects themselves.)
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.