시크바는 프로그레스바를 확장하여 만들어진 것으로, 프로그레스바의 속성을 가지고 있으면서 사용자가 값을 조정할 수 있도록 해준다. 볼륨 조절이나 동영상 재생시 주로 사용된다.
아래 예제는 시크바를 조절하여 밝기를 조절하는 예이다.
<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="vertical" >
<Button
android:id="@+id/showBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="화면 밝기 조절"
/>
<LinearLayout
android:id="@+id/panel01"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_horizontal"
android:visibility="gone"
>
<SeekBar
android:id="@+id/seekbar01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="100"
android:max="100"
/>
<TextView
android:id="@+id/text01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:text="밝기 수준 : "
/>
</LinearLayout>
</LinearLayout>
package com.example.sampleseekbar;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.xxOnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private View panel;
private SeekBar seekbar;
private TextView text01;
private int brightness = 50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text01 = (TextView)findViewById(R.id.text01);
seekbar = (SeekBar)findViewById(R.id.seekbar01);
/**
* 시크바의 리스너 설정.
*/
seekbar.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener());
}
/**
* 시크바를 조절할때 밝기를 조절하는 메서드.
* @param value
*/
private void setBrightness(int value)
{
if(value < 10)
{
value = 10;
}
else if(value > 10)
{
value = 100;
}
brightness = value;
/**
* WindowManager를 이용하여 디바이스의 밝기값을 조절.
*/
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = (float)value/100;
getWindow().setAttributes(params);
}
/**
* 시크바 위젯의 리스너
*
* @author hjs6877
*
*/
class MyOnSeekBarChangeListener implements OnSeekBarChangeListener {
/**
* 시크바의 상태값이 변경될때마다 호출됨.
*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
/**
* 디바이스의 밝기값 변경 메서드 호출.
*/
setBrightness(progress);
/**
* 밝기수준을 텍스트뷰에 표시.
*/
text01.setText("밝기 수준 : " + progress);
}
@Override
public void xxonStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
}