2-3-8 SeekBar

2018-06-13  本文已影响0人  努力学习的安同学

标注:本文为个人整理,仅做自己学习参考使用,请勿转载和转发
2018-06-13: 初稿。参考博主coder-pig

0. 引言

1. SeekBar基本用法

android:max="100"    // 滑动条的最大值
android:progress="60"    // 滑动条的当前值
android:secondaryProgress="70"  // 二级滑动条的进度
android:thumb="@mipmap/sb_icon"  // 滑块的drawable
onProgressChange:   进度发生改变时会触发
onStartTrackingTouch:     按住SeekBar时会触发
onStopTrackingTouch:     放开SeekBar时会触发

简单的代码示例
效果图:

public class MainActivity extends AppCompatActivity {

    private SeekBar sb_normal;
    private TextView txt_cur;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        bindViews();
    }

    private void bindViews() {
        sb_normal = (SeekBar) findViewById(R.id.sb_normal);
        txt_cur = (TextView) findViewById(R.id.txt_cur);
        sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txt_cur.setText("当前进度值:" + progress + "  / 100 ");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(mContext, "触碰SeekBar", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(mContext, "放开SeekBar", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

2. 简单SeekBar定制

代码实例:

运行效果图:

代码实现:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@mipmap/seekbar_thumb_pressed"/>
    <item android:state_pressed="false" android:drawable="@mipmap/seekbar_thumb_normal"/>
</selector>

贴下素材:


<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#FFFFD042" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#FFFFFFFF" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#FF96E85D" />
            </shape>
        </clip>
    </item>
</layer-list>

3.然后布局引入SeekBar后,设置下progressDrawable与thumb即可!

<SeekBar
        android:id="@+id/sb_normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="5.0dp"
        android:minHeight="5.0dp"
        android:progressDrawable="@drawable/sb_bar"
        android:thumb="@drawable/sb_thumb"/>

后面的图也做出来了,但是没有理解二级进度条的具体使用规范,没看见二级进度条在哪里啊,sb_bar中背景和过程是用上了

上一篇 下一篇

猜你喜欢

热点阅读