Android自定义可拖动进度条__SeekBar

Android里自带的进度条满足不了我们的需求,在这篇文章中,我们拖动的滑块为自定义的一张图片,背景也是自己定义的,废话不多说,先上效果图:
在这里插入图片描述
1.先在project的drawable下定义一个layer-list的xml文件作为背景

<?xml version="1.0" encoding="utf-8"?>
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="60dp" />

        <gradient
            android:angle="0"
            android:centerColor="#F39801"
            android:centerY="0.20"
            android:endColor="#F39801"
            android:startColor="#F39801" />
    </shape>
</item>

2.在布局中定义seekbar
<RelativeLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”

    >

        <SeekBar

            android:id="@+id/seekbar"
            android:layout_width="370dp"
            android:layout_height="40dp"

            android:max="100"
            android:progress="50"
            android:maxHeight="10dp"
            android:minHeight="10dp"
            android:thumbOffset="0dp"

            android:layout_marginTop="50dp"
            android:layout_centerHorizontal="true"
            android:thumb="@mipmap/yuan"
            android:progressDrawable="@drawable/seekbar_img"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="关闭"
            android:layout_marginTop="85dp"
            android:layout_marginLeft="15dp"
            />
        <TextView
            android:id="@+id/tv_window_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="50%"
            android:layout_marginTop="85dp"
            android:layout_centerHorizontal="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开"
            android:layout_alignParentRight="true"
            android:layout_marginTop="85dp"
            android:layout_marginRight="15dp"
            />

3.对进度条设置滑动监听
package com.example.atry.test;

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

public class WindowActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
//布局的seekbar
private SeekBar seekBar;
//这个为布局中改变的数字
private TextView tv_window_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_window);

    tv_window_date = findViewById(R.id.tv_window_date);
    seekBar = findViewById(R.id.seekbar);
    seekBar.setOnSeekBarChangeListener(this);

}

@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

    tv_window_date.setText(i+"%");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

}
ok,完成

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值