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" />
Related
I've added this attribute to my XML.
android:background="#drawable/background"
"#drawable/background" refers to the file background.psd which is located in the drawable folder.
The background does not appear when i run it (in the emulator).
What am i missing?
You can't use psd files as drawables. Try saving it as png.
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.
I am designing an android application where am storing my images in my local xml file like
imageview.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Source>
<ID>001</ID>
<Name>News</Name>
<image>
<title>News1</title>
<location>file:http:server_ip/Images/news1.png</location>
</image>
<image>
<title>News1</title>
<location>http:server_ip/Images/bnews2.png</location>
</image>
</Source>
Now i would like to display the image from this xml file into my imageview like-
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="here i would like to call my xml files image"/>
Edit:
I displayed this xml image with the help of loadimage. Now my only question is how to display the image from the xml file and display the image.
I am very wonder why your storing the image files such as news1.png and news2.png in your 'F' drive and how you except android to pack or bundle these inside the apk ,
you have two ways to do this
Solution 1:
please store it in your drawable or in assets folder, how android can pack/bundle this file with your apk.
Solution 2:
If you still want to keep your files in 'f' drive,just imitate like downloading the files from server and placing to drawable or assets
depending upon your need, i mean keep your 'f' drive as a localhost,
without downloading or importing to drawable you cannot use that image
inside your app.
I have one big image named panel_bck.jpg to use in different resolutions to decrease apk size. The image format is JPG and it's size is 1.9MB. I put it to default drawable folder only. But when I set the background in code, the BitmapFactory can't find the image at
drawable/panel_bck.jpg
and the result is all black background. I tested this case with other images(PNG files, JPG files) and there is no problem with them. Only question is that a limitation exist for the drawable size or what is the problem? How can I solve this?
If your image very big resoulation you will set drawable-xxhdpi directory only.
I had a problem like yours trying to maka a big image fit into a too small ImageView and I fixed my problem by using this line in the layout xml file:
android:adjustViewBounds="true"
I don't know if it can ba applied to an activity background though.
You should use 9patch image. You can create 9patch image by using the tool called draw9patch located in sdk/tools. First create a large resolution image and give to draw9patch it will create a 9patch image with extension .9.png add that image in drawable it will solve you problem.
See following links for detail
http://developer.android.com/tools/help/draw9patch.html
http://codesignature.com/how-to-design-9-patch-buttons-for-android-using-adobe-photoshop-for-all-pixel-densities-and-states/
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).