시크바(seekBar)도 프로그레스바를 상속받은 상태표시 위젯이다. 추가적으로 사용자가 직접 상태값을 조정할 수 있다. 음악이나 동영상 플레이어의 재생 구간 조절이 대표적인 사용 예이다. 상태값이 조정되었을 때의 이벤트를 처리하기 위해 OnSeekBarChangeListener 리스너를 설정할 수 있다.

OnSeekBarChangeListener

OnSeekBarChangeListener 리스너 객체 내부에서는 아래 세 가지의 메소드를 재정의하여 사용할 수 있다.

 

void onStartTrackingTouch(SeekBar seekBar) //시크바의 핸들러 움직이기 시작할 때
void onStopTrackingTouch(SeekBar seekBar) // 시크바의 핸들러 움직임 멈췄을 때
void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) //시크바의 값이 바뀌었을 때

시크바를 사용해서 화면 밝기를 조정하는 기능을 적용해보겠다.

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="시크바를 사용해 화면밝기 조정하기"
        android:textSize="20dp"
        android:layout_margin="20dp"/>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="#22000000"
        android:max="100"
        android:min="10"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_margin="20dp" />

</LinearLayout>

시크바의 최대값은 100, 최소값은 10으로 설정하였다. 시크바의 값을 설정하면 아래에 있는 텍스트뷰에 그 값을 표시할 것이다.

 

package io.swnomad.sampleseekbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SeekBar seekBar = findViewById(R.id.seekBar);
        textView = findViewById(R.id.textView);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                //윈도우 밝기 값 10% ~ 100% 사이로 제한
                if(progress < 10){
                    progress = 10;
                }else if(progress>100){
                    progress = 100;
                }else{
                    //NOP
                }

                WindowManager.LayoutParams params = getWindow().getAttributes(); //윈도우 관련정보 참조
                params.screenBrightness = (float) progress / 100;
                getWindow().setAttributes(params); //설정한 밝기 값 적용
                textView.setText("현재 화면 밝기(%) : " + progress); //텍스트뷰에 현재 밝기 %단위로 설정
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

OnSeekBarChangeListener 리스너 객체 내부의 onProgressChanged() 메소드는 시크바의 상태값이 변경되었을 때 호출된다. 이 때 전달인자로 들어오는 progress는 시크바의 상태값인데 이 값이 10보다 작거나 100보다 크면 각각 10과 100으로 재설정하였다.

그리고 getWindow().getAttributes() 메소드를 통해서 현재 윈도우 설정 파라미터 정보를 참조했다. 이 파라미터에서 screenBrightness로 화면 밝기값을 참조할 수 있는데, 0~1 사이의 값을 가지므로 progress를 100으로 나눈 값으로 설정한다.

앱을 실행하여 시크바를 통해 화면 밝기를 설정할 수 있다.

1
2

+ Recent posts