Popupwindow在指定的x,y坐标显示

来源:互联网 发布:软件设计师怎么备考 编辑:程序博客网 时间:2024/04/30 05:04

项目需求,要将SeekBar的值显示有SeekBar上方的一个PopupWindow中,PopupWindow的位置要随着SeekBar的滑块变换。(之前发的代码经过几次测试,发现不适应于大部分场景,现已经将SeekBar上显示一个PopupWindow封闭成一个CustomView ,大家只要在xml中费用就可以,但是tips.xml一点要有哦)

直接上代码:(注释很清楚)

ManaMySeekBar.java:(自定义的SeekBar)


<pre name="code" class="java">/** * 自定义seekbar *  * */public class ManaMySeekBar extends SeekBar { // 用来显示弹窗textprivate PopupWindow mPopupWindow;private LayoutInflater mInflater; //显示进度textviewprivate View mView; //定义坐标数组  记录当前x,y坐标private int[] mPosition;private TextView mTvProgress;public ManaMySeekBar(Context context, AttributeSet attrs) {super(context, attrs);//加载PopupWindow上显示的布局mInflater = LayoutInflater.from(context);mView = mInflater.inflate(R.layout.tips, null);//布局中的TextView 显示seekBar的值mTvProgress = (TextView) mView.findViewById(R.id.tv_popu_menu);mPopupWindow = new PopupWindow(mView);//设置PopupWindow的背景  mPopupWindow.setBackgroundDrawable(getResources().getDrawable( R.drawable.bg_charg));mPosition = new int[2];}//设置PopupWindow上显示的内容public void setSeekBarText(String str) {mTvProgress.setText(str);}//获得内容public String getstr() {return (String) mTvProgress.getText();}//触摸事件处理@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:this.getLocationOnScreen(mPosition);mPopupWindow.showAsDropDown(this, (int) event.getX(), mPosition[1]- (int) this.getY() - this.getHeight() - 30);//break;case MotionEvent.ACTION_UP:mPopupWindow.dismiss();break;}return super.onTouchEvent(event);}//重写OnDraw方法,更新PopupWindow的位置与显示的值@Overrideprotected synchronized void onDraw(Canvas canvas) {int thumb_x = this.getProgress() * (this.getWidth() / this.getMax());//根据进度值设置PopupWindow显示的位置int middle = (int) this.getY() + this.getHeight();super.onDraw(canvas);if (mPopupWindow != null) {try {this.getLocationOnScreen(mPosition);mPopupWindow.update(thumb_x - 10, middle, 120, 100);//更新PopupWindow的位置与显示的值setSeekBarText(String.valueOf(this.getProgress()));} catch (Exception e) {}}}}





在要费用控件的布局文件引入自定义的SeekBar:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <mystery.honey.edittextinsertimagespan.ManaMySeekBar        android:id="@+id/sb_point"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/ll_body"        android:layout_marginTop="220dp" /></RelativeLayout>


还有就是 popupwindow里的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/tv_popu_menu"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#ffffff"        android:text="TextView" /></LinearLayout>


最后看看效果图吧:



0 0
原创粉丝点击