본문 바로가기

Android/SeekBar

Android SeekBar example

Android의 ProgressBar와 유사한 SeekBar 사용 예

SeekBar를 이용하여 이미지의 크기를 축소/확대하는 예

layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onBtnShow" android:text="Show SeekBar" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/persian_smile" android:scaleType="matrix"/> </LinearLayout>


TestActivity.java

package com.example.androidapp; import android.app.Activity; import android.graphics.Matrix; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class TestActivity extends Activity implements OnSeekBarChangeListener { SeekBar seekBar; ImageView iv; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); iv = (ImageView) findViewById(R.id.imageView1); seekBar = (SeekBar) findViewById(R.id.seekBar1); seekBar.setOnSeekBarChangeListener(this); seekBar.setProgress(100); } public void onBtnShow(View v) { // 버튼을 누르면 시크바가 왼쪽으로부터 이동하여 나타난다 Animation inAnimation = AnimationUtils.loadAnimation(this, R.anim.seekbar_in); seekBar.setVisibility(View.VISIBLE); seekBar.startAnimation(inAnimation); } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {//시크바의 값으로 이미지 크기조정 Matrix m = new Matrix(); m.postScale(progress/100f, progress/100f); iv.setImageMatrix(m); } public void onStartTrackingTouch(SeekBar seekBar) {} public void onStopTrackingTouch(SeekBar seekBar) { // 시크바의 값을 조정하고 나면 시크바는 사라진다 Animation outAnimation = AnimationUtils.loadAnimation(this, R.anim.seekbar_out); seekBar.startAnimation(outAnimation); seekBar.setVisibility(View.GONE); } }


res/anim/seekbar_in.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p" android:toXDelta="0%p" android:interpolator="@android:anim/overshoot_interpolator" android:duration="2000" /> </set>


res/anim/seekbar_out.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0%p" android:toXDelta="-100%p" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:duration="2000" /> </set>