自定义SeekBar实现标尺指示范围的功能(含自定义shape等资源)

来源:互联网 发布:休闲食品网络市场调查 编辑:程序博客网 时间:2024/06/06 01:35

要实现的功能请看图:

1.要实现这个功能就必须要禁止掉SeekBar的拖拽,所以在这里就选择集成自SeekBar,重写其onTouchEvent()方法,将返回值改为false表示处理拖拽事件,将事件往上传递。


import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.SeekBar;/** * Created by Avater on 2017/3/14. * 自定义SeekBar,禁止用户拖动达到指示血压范围的效果 */public class MySeekBar extends SeekBar {    public MySeekBar(Context context) {        super(context);    }    public MySeekBar(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MySeekBar(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        return false;    }


2.在布局文件中使用:

<cn.appscomm.pedometer.avater.MySeekBar    android:id="@+id/blood_limit"    android:layout_width="match_parent"    android:layout_height="20dp"    android:progress="32"
    android:max="99"
  android:progressDrawable="@drawable/seek_list" android:thumb="@drawable/flag_green" />


3.编写seek_list资源文件,(android:thub->表示自定义的图标(自己选择合适的即可)),在drawable文件下->新建Drawable Resouces file ->layer-list(文件类型):seek_list

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:id="@android:id/background"        android:paddingBottom="3px"        android:paddingTop="3px">        <shape>            <corners android:radius="10dip" />            <gradient                android:angle="270"                android:centerColor="@android:color/transparent"                android:centerY="0.45"                android:endColor="@android:color/transparent"                android:startColor="@android:color/transparent" />        </shape>    </item>    <item        android:id="@android:id/progress"        android:paddingBottom="3px"        android:paddingTop="3px">        <clip>            <shape>                <corners android:radius="10dip" />                <gradient                    android:angle="270"                    android:centerColor="@android:color/transparent"                    android:centerY="0.45"                    android:endColor="@android:color/transparent"                    android:startColor="@android:color/transparent" />            </shape>        </clip>    </item></layer-list>


4.在方法中调用:

/** * 以舒张压为标准 * * @param progress 低压(progress<60),正常(60<progress<90),高压(大于90) * @deprecated 将MySeek的最大值设置为99,平均三等分,根据比例算出对应的进度值 */private void setSeekBarProgessAndPng(int progress) {    int progr = 0;    if (progress < 60) {        progr = progress * (33 / 60);        blood_limit.setThumb(getResources().getDrawable(R.drawable.flag_green));    }    if (progress >= 60 && progress < 90) {        progr = 33 + (progress - 60) * (33 / 30);        blood_limit.setThumb(getResources().getDrawable(R.drawable.flag_yellow));    }    if (progress >= 90) {        progr = 66 + (33 / 10) * (progress - 90);        blood_limit.setThumb(getResources().getDrawable(R.drawable.flag_red));    }    blood_limit.setProgress(progr);}

完毕,谢谢支持!









0 0