AppCompatSeekBar 를 사용하면 Max값이랑 Progress만 설정 할 수 있게 되어있습니다.

Min이랑 Step 도 되게 해주면 편하게 개발할텐데요.

없으니깐 만들어서 쓰셔야 합니다.


작업 내용

xml 은 아래와 같이 작성하였습니다.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:id="@+id/tv_"
            android:text="SeekBar : "/>

        <android.support.v7.widget.AppCompatSeekBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_"
            android:layout_marginTop="20dp"
            android:id="@+id/sb_"/>

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>


.Java code는 아래와 같이 작성하였습니다.

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatSeekBar;
import android.view.animation.LinearInterpolator;
import android.widget.SeekBar;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    final int max = 5;
    final float min = (float) 0.1;
    final float step = (float) 0.1;
    final String str_title = "SeekBar : ";

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

        final TextView tv_ = (TextView) findViewById(R.id.tv_);
        final AppCompatSeekBar sb_ = (AppCompatSeekBar) findViewById(R.id.sb_);

        setSeekBarMax(sb_, max);
        sb_.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                setSeekBarChange(progress, tv_);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        setSeekBarAnimation(sb_);
    }

    private void setSeekBarMax(AppCompatSeekBar sb, int max_value) {
        sb.setMax((int)((max_value-min) / step));
    }

    private void setSeekBarChange(int progress, TextView tv) {
        float value = min + (progress * step);
        DecimalFormat format = new DecimalFormat(".#");
        String str = format.format(value);
        tv.setText(str_title +" ( "+Float.valueOf(str)+" )");
    }

    // 최초 중간 위치 설정
    private void setSeekBarAnimation(AppCompatSeekBar sb) {

        int progress_half = (int)(((max / min) / 2)-1);

        ObjectAnimator animation = ObjectAnimator.ofInt(sb, "progress", progress_half);
        animation.setDuration(100); // 0.5 second
        animation.setInterpolator(new /*DecelerateInterpolator()*/LinearInterpolator());
        animation.start();
    }
}


결과화면

최소 value 0.1 ~ 5.0 사이에 step 은 0.1 만큼씩 움직이게 됩니다.





+ Recent posts