自定义seekbar PopupWindow显示问题,求解决办法,谢谢。

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
}
}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值