[Android-Develop] 스크롤 뷰(Scroll View)

2014. 12. 31. 20:35 - Song's IT

기본적으로 Layout을 지정하는 XML파일에 <ScrollView>태그를 사용하면 화면을 벗어나는 

영역을 볼 수 있도록 수직 스크롤이 생성된다.


수평 스크롤을 생성하기 위해서는 <HorizontalScrollView>태그를 사용하여

<ScrollView>태그를 감싸주어야 한다.


이미지를 원본크기(화면크기 이상)로 불러와 스크롤을 생성하는 소스예시는 아래와 같다.

layout.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity">
 
    <button android:id="@+id/backBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="돌아가기">
 
    </button><button android:id="@+id/newBtn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ChangePicture">
 
    <horizontalscrollview android:id="@+id/horScrollView" android:layout_width="wrap_content" android:layout_height="wrap_content">
 
        <scrollview android:id="@+id/scrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content">
 
            <imageview android:id="@+id/imageView01" android:layout_width="wrap_content" android:layout_height="wrap_content">
        </imageview></scrollview>
    </horizontalscrollview>
 
 
</button></linearlayout>



NewActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class NewActivity extends Activity {
     
    ScrollView scrollView;
    ImageView imageView;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_activity);
         
        // layout.xml에 정의한 스크롤 뷰, 이미지뷰, 버튼객체 생성
        scrollView = (ScrollView)findViewById(R.id.scrollView01);
        imageView = (ImageView)findViewById(R.id.imageView01);
        Button button = (Button)findViewById(R.id.newBtn01);
         
        // 이미지 리소스를 얻어오기 위한 Resources객체 생성
        Resources res = getResources();
        BitmapDrawable bitmap = (BitmapDrawable)res.getDrawable(R.drawable.test);
         
        // getIntrinsicWidth()메서드를 통해 이미지의 실제 가로, 세로길이 얻어오기
        int width = bitmap.getIntrinsicWidth();
        int height = bitmap.getIntrinsicHeight();
         
        // 이미지 뷰 출력 후 getLayoutParams()메서드로 객체 로드 후 실제 길이지정
        // Default는  hdpi로 지정되어 있음.
        imageView.setImageDrawable(bitmap);
        imageView.getLayoutParams().width = width;
        imageView.getLayoutParams().height = height;
    }
}


다른 카테고리의 글 목록

Android/Develop 카테고리의 포스트를 톺아봅니다