private PopupWindow mPopupWindow;
private LayoutInflater mInflater;
private View mView;
/**
* 用来表示该组件在整个屏幕内的绝对坐标,其中 mPosition[0] 代表X坐标,mPosition[1] 代表Y坐标。
*/
private int[] mPosition;
/**
* SeekBar上的Thumb的宽度,即那个托动的小黄点的宽度
*/
private final int mThumbWidth = 25;
private TextView mTvProgress;
public ManaMySeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(context);
mView = mInflater.inflate(R.layout.popwindow_layout, null);
mTvProgress = (TextView) mView.findViewById(R.id.tvPop);
mPopupWindow = new PopupWindow(mView, mView.getWidth(),
mView.getHeight(), true);
mPopupWindow.setFocusable(false);
mPosition = new int[2];
}
public void setSeekBarText(String str) {
mTvProgress.setText(str);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.getLocationOnScreen(mPosition);
//想显示在seekbar的上面,但是怎么修改都无法实现。
// mPopupWindow.showAsDropDown(this, (int) event.getX(),
// mPosition[1] - 30);
mPopupWindow.showAsDropDown(this, 0, -300);
break;
case MotionEvent.ACTION_UP:
// mPopupWindow.dismiss();
break;
}
return super.onTouchEvent(event);
}
/**
* 获取控件的宽度
*
* @param v
* @return 控件的宽度
*/
private int getViewWidth(View v) {
int w = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
v.measure(w, h);
return v.getMeasuredWidth();
}
/**
* 获取控件的高度
*
* @param v
* @return 控件的高度
*/
private int getViewHeight(View v) {
int w = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
v.measure(w, h);
return v.getMeasuredHeight();
}
@Override
protected synchronized void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
int thumb_x = this.getProgress() * (this.getWidth() - mThumbWidth)
/ this.getMax();
int middle = this.getHeight() / 2 + 40;
super.onDraw(canvas);
if (mPopupWindow != null) {
try {
this.getLocationOnScreen(mPosition);
mPopupWindow.update(thumb_x + mPosition[0]
- getViewWidth(mView) / 2 + mThumbWidth / 2, middle,
getViewWidth(mView), getViewHeight(mView));
} catch (Exception e) {
// TODO: handle exception
}
}
}
自定义seekbar PopupWindow显示问题,求解决办法,谢谢。
最新推荐文章于 2021-05-27 15:24:18 发布