Jump to content
Gonzalez

Working With Images In Android

Recommended Posts

Basic Android Image Information

Android supports 3 common image formats PNG, JPG, GIF, along with 9 patch PNG images. Images are stored in the directory res/layout/drawable. As of version 1.6 of the Android SDK multiple drawable directories exist for different screen resolutions. There are low, medium, and high DPI specific directories, drawable-ldpi, drawable-mdpi, drawable-hdpi respectively. This allows you to create images at different DPI to enhance the appearance of your application. All image filenames should be lowercase and only contain letters, numbers, and underscores.

Create a new project in Eclipse called TestImages.

Displaying An Image

The ImageView layout component is the base element used for displaying images in Android. Download this image and copy it into res/layout/drawable-mdpi in your project. We're simply going to use a screenshot of the emulator.

<ImageView 
android:id="@+id/test_image"
android:src="@drawable/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

This ImageView loads the image test that you downloaded. Add this to the res/layout/main.xml file below the TextView.

package higherpass.TestImages;

import android.app.Activity;

public class TestImages extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
}
}

All that was added to the default code is how to get the ImageView component from the layout and store it in a variable. We'll do more with this in the next examples.

Changing The Image

Changing the image done by creating an ImageView object for the image component to change and calling the setImageResource() method. Instead of using resources a custom bitmap can also be used by invoking setImageBitmap(). We'll get to bitmaps next. Download this second image and store it as test2.png in res/layout/drawable-mdpi.

package higherpass.TestImages;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class TestImages extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test2);
}
}

Here simply store the ImageView into the image variable and use the setImageResource() method to point the ImageView at the second image.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...