Android SeekBar,动态显示进度且随SeekBar一起移动

https://www.jianshu.com/p/46242fde8cee

package com.xq.customview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;
/**
 */
public class ThreeActivity extends Activity {
    private int startTimeStr = 50;
    private int endTimeStr =100;
    private TextView textView;
    private SeekBar seekBar ;
    /**
     * 距离
     */
    private int totalSeconds = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seekbar_layout);
          textView = findViewById(R.id.seek_text);
        //seekbar
         seekBar = findViewById(R.id.seekBar);
        totalSeconds = endTimeStr - startTimeStr;
        seekBar.setProgress(50);
        textView.setText(check(50));
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                //进度是从-50~50的,但是seekbar.getmin()有限制,所以这里用0~100 -50;
                int text = progress ;
                //设置文本显示
                textView.setText(check(progress));
                Log.d("=====ssss",text+"");
                //获取文本宽度
                float textWidth = textView.getWidth();

                //获取seekbar最左端的x位置
                float left = seekBar.getLeft();

                //进度条的刻度值
                float max =Math.abs(seekBar.getMax());

                //这不叫thumb的宽度,叫seekbar距左边宽度,实验了一下,seekbar 不是顶格的,两头都存在一定空间,所以xml 需要用paddingStart 和 paddingEnd 来确定具体空了多少值,我这里设置15dp;
                float thumb = dip2px(ThreeActivity.this,15);

                //每移动1个单位,text应该变化的距离 = (seekBar的宽度 - 两头空的空间) / 总的progress长度
                float average = (((float) seekBar.getWidth())-2*thumb)/max;
                //int to float
                float currentProgress = progress;
                //textview 应该所处的位置 = seekbar最左端 + seekbar左端空的空间 + 当前progress应该加的长度 - textview宽度的一半(保持居中作用)
                float pox = left - textWidth/2 +thumb + average * currentProgress;
                textView.setX(pox);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
    public  int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    private String check(int progress) {
        int curValue = totalSeconds * progress/Math.abs(endTimeStr);
        curValue = curValue + startTimeStr;
        Log.e("check", totalSeconds+","+curValue+","+ seekBar.getMax()+"progress"+progress);
        return String.valueOf(curValue);
    }
}

下面是seekbar_layout.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parentLayout"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekLayout"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/seek_text"
            android:textColor="@color/colorAccent"
            android:text="0"
            android:gravity="center"
            android:layout_gravity="center_horizontal"
            android:paddingBottom="5dp"
            />
        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:maxHeight="5dp"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            android:progressDrawable="@drawable/po_seekbar"
            android:layout_height="wrap_content"
            android:thumb="@drawable/seekbar_thumb"/>
    </LinearLayout>
</RelativeLayout>

下面是进度条的样式po_seekbar.xml,如果需要改变样式才需要这个

<?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="#bebebebe" />
        </shape>
    </item>
    <item android:id="@*android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#bebebebe" />
            </shape>
        </clip>
    </item>
    <item android:id="@*android:id/progress">
        <clip>
            <shape>
                <solid android:color="#06a7fa" />
            </shape>
        </clip>
    </item>
</layer-list>

猜你喜欢

转载自blog.csdn.net/code_dream_wq/article/details/82626342