项目需求的一个效果请看图
这种效果实现的方式基本思路 seekbar+popwindow,当更新进度的时候同时更新popwindow的位置即可,废话不多说直接上代码
主Activity
package com.seekbar.demo; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; public class SeekBarDemoActivity extends Activity { private ManaMySeekBar mSeekBar; private int mProgress; private LinearLayout mLlMain; @SuppressLint("HandlerLeak") private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { mProgress += 1; if (mProgress <= 100) { mSeekBar.setProgress(mProgress); mSeekBar.setSeekBarText(mProgress + "%"); } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); setEventListener(); initData(); } /** * 初始化控件 */ private void initView() { mLlMain = (LinearLayout) findViewById(R.id.ll_main); } /** * 设置监听 */ private void setEventListener() { } /** * 填充数据 */ private void initData() { //1.创建seekbar //2.并给seekbar添加项目需求的样式属性等等,最后别忘了设置属性方法setLayout(params) //3.隐藏seekbar的Thumb并设置max的最大值 //4.塞进父布局中 mSeekBar = new ManaMySeekBar(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, 30); params.leftMargin = 70; params.rightMargin = 70; mSeekBar.setLayoutParams(params); mSeekBar.setThumb(getResources().getDrawable(android.R.color.transparent)); mSeekBar.setMax(100); mLlMain.addView(mSeekBar); mProgress = 0; //这里在子线程中睡200毫秒是为了防止seekbar加载前,布局还没有加载完成,导致找不到Activity工程崩溃 new Thread() { public void run() { SystemClock.sleep(200); startThread(); }; }.start(); } /** * 模拟下载数据 */ private void startThread() { new Thread() { @Override public void run() { super.run(); while (mProgress <= 100) { SystemClock.sleep(200); handler.sendEmptyMessage(0); } } }.start(); } }
自定义的SeekBar
package com.seekbar.demo; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow; import android.widget.SeekBar; import android.widget.TextView; public class CusSeekBar extends SeekBar { private PopupWindow mPopupWindow; private LayoutInflater mInflater; private View mView; private int[] mPosition; private Context mContext; private final int mThumbWidth = 25; private TextView mTvProgress; private int mWidth; private int mHeight; int mProgress; int mViewWidth; int mDX; int mOneStep; int mStartX; public CusSeekBar(Context context) { super(context); init(context); } public CusSeekBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public CusSeekBar(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { mContext = context; 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); mPosition = new int[2]; WindowManager wm = (WindowManager) mContext .getSystemService(Context.WINDOW_SERVICE); mWidth = wm.getDefaultDisplay().getWidth(); mHeight = wm.getDefaultDisplay().getHeight(); mViewWidth = getWidth(); mDX = mWidth - mViewWidth; mOneStep = mViewWidth / getMax(); mStartX = mWidth - mDX / 2; } public void setSeekBarText(String str) { mTvProgress.setText(str); mProgress = getProgress(); mPopupWindow.showAsDropDown(this, mStartX + mOneStep * mProgress, mHeight - 30); } 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(); } 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) { int thumb_x = this.getProgress() * (this.getWidth() - mThumbWidth) / this.getMax(); int middle = mHeight / 2 - 70;//这里控制pop的Y轴位置 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) { } } } }
源码下载:
http://pan.baidu.com/s/1i3L00SX