I use in my application more than 150 images, and 30 icons ! so I find it a little bit messy to put those images all together in Drawable folder, and as it is impossible to have sub-folders in drawable , I am trying to load my images from the Assets folder.
So to load images from assets programmatically its OK.
MY QUESTION is how to access Assets through XML?!
with drawable we use this :
<ImageView
android:id="#+id/img"
android:layout_width="25dp"
android:layout_height="25dp"
android:src=" **#drawable/ic_rate** "
/>
So what is the equivalent using assets.
Despite the fact that you should use drawables, you can define your own asset_drawable attribute as string than override the ImageViewto look for that atttibute and load the bitmap from assets. Your xml will be look like this
<com.foo.bar.AssetImageView
android:id="#+id/img"
android:layout_width="25dp"
android:layout_height="25dp"
app:asset_drawable="some/path/asset"
/>
You don't. An ImageView expects a drawable. Not all assets are drawables.
Drawables let you use images of different screen densities. Just use those. If you're struggling to stay organized, perhaps you could try using prefixes (e.g., prefix icons with ic). Another option would be to create a library project exclusively for icons, though it may be overkill.
Related
When I have an xml layout like:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_image" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_image" />
Then there are 2 instances of the bitmap associated with drawable in memory or just one?
When loading drawables from Resources, these will be cached with a DrawableCache which is a ThemedResourceCache.
The ResourcesImpl states in a comment:
// First, check whether we have a cached version of this drawable
// that was inflated against the specified theme. Skip the cache if
// we're currently preloading or we're not using the cache.
Good question!!!
I have viewed source code and it looks like it will create new bitmap instances for each ImageView. There is a cumbersome process goes under all of that...
ImageView uses Drawable
Drawable uses ImageDecoder
ImageDecoder uses BitmapFactory
BitmapFactory uses native C++ code for image decoding. (Look at line 157 and below)
Brief look at the code tells me that it will create new instances of bitmap Drawable for each ImageView even if used same image. I didn't find any logic related to such optimization.
UPDATE
Yes there is a optimization took place in case of bitmap loading. Abhisek Mallick absolutely right. Even without looking into the code it is easy to check. Create a RecyclerView with ImageView's. Load into those ImageView's same bitmap. And try to change the number of items. While changing look at the Profiler->Memory. Memory allocation won't be changed.
You can find the answer here :
For instance, every time you create a Button, a new drawable is loaded from the framework resources (android.R.drawable.btn_default). This means all buttons across all the apps use a different drawable instance as their background. However, all these drawables share a common state, called the "constant state."
source: https://android-developers.googleblog.com/2009/05/drawable-mutations.html?m=1
I'm developing an app that display a listview with different images that are edited when the user click on them.
This is the code of my row.xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:id="#+id/textView1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="TextView"
android:textSize="25sp"
/>
I'm using only one set of images, stored in the folder res/drawable/drawable-nodpi (Because I need them to keep its original size in order to edit them as bitmaps).
The problem is that those images are to big too be displayed in a single activity and therefore, when I create the listview it use a lot of memory and the list is scrolls with some lag. How can I display those images in the list without this problem? Should I create another set of smaller images to use them in the list? I've read post when the app save the images to the SD card, or the cache memory, but actually i'm not sure about the solution that fit best with my specific application.
Thanks in advance
You can use the following link below to make your list. Here they free the image cache once it is not seen. Using this you can put many image sin your list :
https://github.com/thest1/LazyList
Hope this helps you.
Try this..
Android-Universal-Image-Loader libriary. This project aims to provide a reusable instrument for asynchronous image loading, caching and displaying. It is originally based on Fedor Vlasov's project and has been vastly refactored and improved since then.
https://github.com/nostra13/Android-Universal-Image-Loader
and for drawable resources use this..
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
I think these ways may help:
Put these images in server, then use Universal image loader to load it. Universal image loader have solved this problem. you can try this project https://github.com/dodola/Android-Universal-Image-Loader
If you have to put these images in res, I think you can implement you own image loader. In your Image loader, you have to cache the images in memory by use softreference. This is another problem.
I suggest you to use Aquery to download images. Just download the jarfile from this link
How to use :
AQuery _aq = new AQuery(youractivity context);
//fetch and set the image from internet, cache with file and memory
aq.id(R.id.your_imageview_object).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
I want to ask if Android accepts JPEG images instead of PNG, because PNG has bigger file size than JPEG.
I want to know how to use JPEG as background image in layout.xml file. Can you help me?
Just put a .jpg file in your res/drawable folder and refer to it as you normally would.
For example, if you put picture.jpg in your drawable folder, from one of your views you could just do the following.
<View
android:id="#+id/view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/picture" />
I'm translating my apps to a few languages but now I'm struggling with a problem! I a few images with text on it, there is a way to provide drawable resources using country code? If I add the drawable to my apps using the country code It will increase the size of the app? I don't wan't have an app that is now 1MB to be 4 or 5 just because of those images.
Thank You!
Equal you set diferent languages with the res directories values-es values-fr values ...
you can set differents drawables for each location with the directories:
drawable-es drawable-fr ...
http://developer.android.com/training/basics/supporting-devices/languages.html
Remember that the extra information of the directories has specials importances and you should put in the correct positions http://developer.android.com/guide/topics/resources/providing-resources.html
For example, drawable-es-hdpi
The problem of the size of your apk i imagine that you don't need to set all your images diferents for all the countries, isn't it? Normally you only need configure a few images
Yes, it is possible to provide resources per country. Check out this answer for details on how to do it: Localization and drawables
It will definitely increase the size of your app. It is, like everything else in software development, a compromise between performance (or quality of images) and resouces (in this case, size of apk). To avoid increasing the size of your app exponentially, you can try to provide resources per locale instead of country, or simply, provide only the hdpi version.
If you don't want to increase the size of your apk with the different drawables, you can code a custom ImageView and set the text over it with a String (that will be translated) or put a textView on top of the ImageView setting them inside a relativelayout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/yImage"
android:layout_width="fill_parent"
android:layout_height="25dp"/>
<TextView
android:id="#+id/yTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/yImage"
android:layout_alignLeft="#+id/yImage"
android:layout_alignRight="#+id/yImage"
android:layout_alignTop="#+id/yImage"
android:background="#color/transparent"
android:lines="2"
android:textColor="#color/Black"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
It would be easy also with a frame layout.
Essentially I want an HD background on a layout, but instead of being a pristine image, it shows up extremely compressed.
Example:
Source Image:
Android Emulator version:
I thought maybe that it was just the emulator, but it looks that bad on the phone itself. I know these are high quality Samsung Galaxy S phones (these are the ones that came with Avatar preinstalled after all).
Does anyone know how to load images without compression? Source files are uncompressed pngs.
here's the code for what it's worth:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1" android:background="#drawable/background" android:id="#+id/blank">
</LinearLayout>
Figured it out: The problem is the image is automatically converted into RGB565 colorspace from AGB888. To prevent this, make sure the image has an alpha channel and is in a "raw" directory instead of the "drawable" directory.
The aapt may compress images when it builds the apk, but that compression is supposed to be lossless. The problem is probably somewhere else. Here are two possibilities.
If your image resource is being loaded from res/drawable on a device (or emulator) that is not mdpi, then it will be scaled as described here. One solution is to put the resource in the res/drawable-nodpi folder. Another is to provide several, density-specific images.
Another problem will arise if your background image does not match the size of the view. The view will automatically scale the image to fit the view size. You can prevent this by defining the background as an XML Bitmap with a gravity set to something that does not scale (such as center).