kitty_lovely
ScrollView 본문
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="onButton1Clicked"
android:text="Click Me!" />
<HorizontalScrollView
android:id="@+id/horScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</HorizontalScrollView>
</LinearLayout>
MainActivity.java
package com.example.samplescrollview;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
public class MainActivity extends AppCompatActivity {
ScrollView scrollView;
ImageView imageView;
BitmapDrawable bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 레이아웃에 정의된 뷰 객체를 참조
scrollView = findViewById(R.id.scrollView);
imageView = findViewById(R.id.imageView);
scrollView.setHorizontalScrollBarEnabled(true); // 수평 스크롤바 사용 기능을 설정함
// 리소스의 이미지 참조
Resources res = getResources();
bitmap = (BitmapDrawable) res.getDrawable(R.drawable.back);
int bitmapWidth = bitmap.getIntrinsicWidth();
int bitmapHeight = bitmap.getIntrinsicHeight();
// 이미지 리소스와 이미지 크기 설정
imageView.setImageDrawable(bitmap);
imageView.getLayoutParams().width = bitmapWidth;
imageView.getLayoutParams().height = bitmapHeight;
}
public void onButton1Clicked(View v) {
changeImage();
}
// 다른 이미지 리소스 변경
private void changeImage() {
Resources res = getResources();
bitmap = (BitmapDrawable) res.getDrawable(R.drawable.cat);
int bitmapWidth = bitmap.getIntrinsicWidth();
int bitmapHeight = bitmap.getIntrinsicHeight();
imageView.setImageDrawable(bitmap);
imageView.getLayoutParams().width = bitmapWidth;
imageView.getLayoutParams().height = bitmapHeight;
}
}'안드로이드 > 레이아웃(Layout)' 카테고리의 다른 글
| Layout (0) | 2023.03.08 |
|---|---|
| FrameLayout (0) | 2023.03.08 |