[main.xml]
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.android:layout_width="fill_parent"
04.android:layout_height="fill_parent"
05.android:orientation="vertical" android:padding="10dp">
06. 
07.<TextView
08.android:layout_width="wrap_content"
09.android:layout_height="wrap_content"
10.android:text="Image sequence:"
11.android:textAppearance="?android:attr/textAppearanceMedium" />
12. 
13.<SeekBar
14.android:id="@+id/imgSeqSeekBar"
15.android:layout_width="match_parent"
16.android:layout_height="wrap_content" android:max="5"/>
17. 
18.<TextView
19.android:id="@+id/imgSeqNum"
20.android:layout_width="fill_parent"
21.android:layout_height="wrap_content"
22.android:text="0" android:gravity="right"/>
23. 
24.<TextView
25.android:layout_width="wrap_content"
26.android:layout_height="wrap_content"
27.android:text="Selected image:"
28.android:textAppearance="?android:attr/textAppearanceMedium" />
29. 
30.<ImageView
31.android:id="@+id/imgView"
32.android:layout_width="match_parent"
33.android:layout_height="wrap_content"
34.android:src="@drawable/img_0" />
35. 
36.</LinearLayout>

다음, Main 액티비티의 코드를 다음과 같이 작성합니다.

[Main.java]
01.package com.androidhuman.example.MultipleImageHandling;
02. 
03.import java.lang.reflect.Field;
04. 
05.import android.app.Activity;
06.import android.os.Bundle;
07.import android.widget.ImageView;
08.import android.widget.SeekBar;
09.import android.widget.SeekBar.OnSeekBarChangeListener;
10.import android.widget.TextView;
11. 
12.public class Main extends Activity {
13. 
14.private SeekBar sbImgSequence;
15.private TextView tvImgSequence;
16.private ImageView ivSequenceImage;
17. 
18.private final String IMG_NAME_FORMAT = "img_%s";
19. 
20.@Override
21.public void onCreate(Bundle savedInstanceState) {
22.super.onCreate(savedInstanceState);
23.setContentView(R.layout.main);
24. 
25.sbImgSequence = (SeekBar)findViewById(R.id.imgSeqSeekBar);
26.tvImgSequence = (TextView)findViewById(R.id.imgSeqNum);
27.ivSequenceImage = (ImageView)findViewById(R.id.imgView);
28. 
29.sbImgSequence.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
30. 
31.@Override
32.public void onProgressChanged(SeekBar seekBar, int progress,
33.boolean fromUser) {
34.try {
35.// Get resources' Field from R.drawable class
36.Field drawableRes = R.drawable.class.getField(String.format(IMG_NAME_FORMAT, progress));
37.// Get field's value and set as resource address to be displayed on ImageView
38.ivSequenceImage.setImageResource(drawableRes.getInt(R.drawable.class));
39.tvImgSequence.setText(String.valueOf(progress));
40.catch (SecurityException e) {
41.e.printStackTrace();
42.catch (NoSuchFieldException e) {
43.e.printStackTrace();
44.catch (IllegalArgumentException e) {
45.e.printStackTrace();
46.catch (IllegalAccessException e) {
47.e.printStackTrace();
48.}
49.}
50. 
51.@Override
52.public void onStartTrackingTouch(SeekBar seekBar) {
53.}
54. 
55.@Override
56.public void onStopTrackingTouch(SeekBar seekBar) {
57.}
58. 
59.});
60.}
61.}
IMG_NAME_FORMAT에는 이미지 파일의 포맷을 지정합니다. 여기에서는 리소스 이름으로 img_0, img_1 .. 이므로 img_%s로 지정하여 format()메서드를 통해 이미지 번호를 입력받아 최종 이미지 리소스 이름을 만들 수 있도록 하였습니다.

사용자가 SeekBar를 움직였을 때 SeekBar의 위치를 이미지 번호로 받고, 이를 사용하여 이미지 이름을 완성하여 리소스에서 불러오기 위해 format()메서드를 사용하여 완성된 이미지 리소스 이름을 사용하여 해당 필드를 Field 형태로 받고, 필드의 값에는 이미지 리소스에 접근할 주소가 있으므로 setImageResource() 의 인자로 이 값을 넘겨주어 이미지뷰에 해당 리소스를 표시하도록 할 수 있습니다.

1.// Get resources' Field from R.drawable class
2.Field drawableRes = R.drawable.class.getField(String.format(IMG_NAME_FORMAT, progress));
3.// Get field's value and set as resource address to be displayed on ImageView
4.ivSequenceImage.setImageResource(drawableRes.getInt(R.drawable.class));
5.tvImgSequence.setText(String.valueOf(progress));

애플리케이션을 실행한 모습은 다음과 같습니다. SeekBar의 슬라이더를 움직여 이미지 번호를 선택하면 선택한 이미지가 아래의 ImageView에 표시되는 것을 확인할 수 있습니다.



<script src="http://androidhuman.tistory.com/plugin/CallBack_bootstrapper?&src=http://s1.daumcdn.net/cfs.tistory/v/0/blog/plugins/CallBack/callback&id=495&callbackId=androidhumantistorycom4958249&destDocId=callbacknestandroidhumantistorycom4958249&host=http://androidhuman.tistory.com&float=left&random=265"></script>