介绍
如何在Android应用中利用SeekBa添加一个进度条功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
<强>步骤1,强>先来看一看PopupWindow的布局文件
& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比; http://schemas.android.com/apk/res/android" & lt; RelativeLayout xmlns: android=?; android: layout_width=皐rap_content" android: layout_height=皐rap_content" android:背景=癅drawable/seek_dialog_bg"比; & lt; !——展现当前播放进度时间的文本框——比; & lt; TextView android: id=癅 + id/dialogSeekTime" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android: layout_marginLeft=?0 dip" android: layout_marginTop=?2 dip" android:文本=癅string/unknow_seek_time" 输入textColor=癮ndroid: @color/black" android: textSize=?2 sp"/比; & lt;/RelativeLayout>
<强>步骤2,>强自定义一个SeekBar
进口com.canplay.video.R; 进口android.content.Context; 进口android.util.AttributeSet; 进口android.view.LayoutInflater; 进口android.view.View; 进口android.widget.PopupWindow; 进口android.widget.SeekBar; 进口android.widget.TextView;/* * *自定义进度拖动条控件 */公开课MySeekBar延伸SeekBar {/* * *定义一个展现时间的PopupWindow */私人PopupWindow mPopupWindow; 私人观点mView;/* * *显示时间的TextView */私人TextView dialogSeekTime;/* * *用来表示该组件在整个屏幕内的绝对坐标,其中mPosition[0]代表X坐标,mPosition[1]代表Y坐标。 */私人int [] mPosition;/* * * SeekBar上的拇指的宽度,即那个托动的小黄点的宽度 */私人最终int mThumbWidth=25; 公共MySeekBar(上下文语境){ 这(上下文,null); } 公共MySeekBar(上下文语境,AttributeSet attrs) { 超级(上下文,attrs); mView=LayoutInflater.from(上下文).inflate (R.layout.seek_popu, null); dialogSeekTime=(TextView) mView.findViewById (R.id.dialogSeekTime); mPopupWindow=new PopupWindow (mView mView.getWidth (), mView.getHeight(),真的); mPosition=new int [2]; }/* * *获取控件的宽度 * * @param v * @return控件的宽度 */私人int getViewWidth(查看v) { int w=View.MeasureSpec。makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); int h=View.MeasureSpec。makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); v。测量(w h); 返回v.getMeasuredWidth (); }/* * *获取控件的高度 * * @param v * @return控件的高度 */私人int getViewHeight(查看v) { int w=View.MeasureSpec。makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); int h=View.MeasureSpec。makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); v。测量(w h); 返回v.getMeasuredHeight (); }/* * *隐藏进度拖动条的PopupWindow */公共空间hideSeekDialog () { 如果(mPopupWindow !=零,,mPopupWindow.isShowing ()) { mPopupWindow.dismiss (); } }/* * *显示进度拖动条的PopupWindow * * @param str *时间值 */公共空间showSeekDialog (String str) { dialogSeekTime.setText (str); int进步=this.getProgress ();//计算每个进度值所占的宽度 int thumb_x=(int)(进展* (1.0 f * (this.getWidth () - 22)/this.getMax ()));//22是两边的空白部分宽度//更新后的PopupWindow的Y坐标 int中间=this.getHeight ()/2 + 120; 如果(mPopupWindow !=null) { 尝试{/* *获取在整个屏幕内的绝对坐标,注意这个值是要从屏幕顶端算起,也就是包括了通知栏的高度。 *其中mPosition[0]代表X坐标,mPosition[1]代表Y坐标。 */this.getLocationOnScreen (mPosition);//相对某个控件的位置(正左下方),在X, Y方向各有偏移 mPopupWindow。showAsDropDown(这(int) mPosition [0], mPosition [1]);/* *更新后的PopupWindow的X坐标 *首先要把当前坐标值减去PopWindow的宽度的一半,再加上拇指的宽度一半。 *这样才能使PopWindow的中心点和拇指的中心点的X坐标相等 */int x=thumb_x + mPosition [0] - getViewWidth (mView)/2 + mThumbWidth/2;//更新弹出窗口的位置 mPopupWindow。更新(x,中间,getViewWidth (mView) getViewHeight (mView)); }捕捉(异常e) { } } } }
<强>,强>将自定义的拖动条加入到布局文件中、下面是部分代码