Android-SeekBar进度条的使用

Android-SeekBar进度条的使用


在我们使用音乐播放器或者是视频播放器时,下面都会有一个进度条,拖动进度条即可改变
音乐的进度和视频播放的进度,那么在安卓里面也有相应的工具类,它就是SeekBar


制作可拖动的进度条
首先是布局文件
activity_main.xml
使用两个TextView用来显示是否拖动以及拖动到了哪一个位置

<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"
              tools:context=".MainActivity"
    >

    <SeekBar
        android:id="@+id/id_seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        />

    <TextView
        android:id="@+id/id_tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/id_tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

主活动获取组件并且设置监听事件
MainActivity.java

package com.xieth.as.seekbardemo;

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

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    private SeekBar seekBar = null;

    private TextView tv1, tv2;

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

        initViews();

    }

    private void initViews() {
        seekBar = (SeekBar) findViewById(R.id.id_seekBar);
        seekBar.setOnSeekBarChangeListener(this);
        tv1 = (TextView) findViewById(R.id.id_tv1);
        tv2 = (TextView) findViewById(R.id.id_tv2);
    }

    // 数值改变
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        tv1.setText("正在拖动");
        tv2.setText("当前数值:" + progress);
    }

    // 开始拖动
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        tv1.setText("开始拖动");
    }

    // 停止拖动
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        tv1.setText("停止拖动");
    }
}

运行:
这里写图片描述
鼠标有点错位,录制gif软件不好,还是在那个进度条上面的。


自定义SeekBar样式
首先来看一下Android系统自带的样式

<SeekBar
        style="@android:style/Widget.SeekBar"
        android:id="@+id/id_seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        />

这一句代码

 style="@android:style/Widget.SeekBar"

运行:
这里写图片描述


首先准备两张图片
这里写图片描述这里写图片描述
灰色代表没有拖动的状态,黄色代表正在进行拖动状态


my_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:drawable="@mipmap/select" android:state_pressed="true"
          android:state_window_focused="true"></item>


    <item android:drawable="@mipmap/normal"></item>
</selector>

然后在布局文件引用即可

<SeekBar
        android:thumb="@drawable/my_style"
        android:id="@+id/id_seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        />

运行:
这里写图片描述


  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值