转载请标明出处:
http://blog.csdn.net/lxk_1993/article/details/50931607
本文出自:【lxk_1993的博客】;
progesssValue.post(new Runnable() { @Override public void run() { int w = progesssValue.getMeasuredWidth(); } });
final ViewTreeObserver observer = progesssValue.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onGlobalLayout() { //此处不能写 observer.removeOnGlobalLayoutListener(this); 否则会报错 progesssValue.getViewTreeObserver().removeOnGlobalLayoutListener(this); int w = progesssValue.getMeasuredWidth(); } });
//方法3 onWindowFocusChanged(true) 表示view获得了焦点 // 不过注意:onWindowFocusChanged 方法会在activity获得焦点和失去焦点的时候调用 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { int w = progesssValue.getMeasuredWidth(); } }
full.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: x1 = (int) event.getRawX(); break; case MotionEvent.ACTION_MOVE: x2 = (int) event.getRawX(); dx = x2 - x1; int w = getWindowManager().getDefaultDisplay().getWidth(); if (Math.abs(dx) > w / 100) { x1 = x2; // 去掉已经用掉的距离, 去掉这句 运行看看会出现效果 progesss.setProgress(progesss.getProgress() + dx * 100 / w); setPos(); } break; case MotionEvent.ACTION_UP: break; } return true; } });
/** * 设置进度显示在对应的位置 */ public void setPos() { int w = getWindowManager().getDefaultDisplay().getWidth(); Log.e("w=====", "" + w); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) progesssValue.getLayoutParams(); int pro = progesss.getProgress(); int tW = progesssValue.getWidth(); if (w * pro / 100 < tW * 0.7) { params.leftMargin = 0; } else if (w * pro / 100 + tW * 0.3 > w) { params.leftMargin = w - tW; } else { params.leftMargin = (int) (w * pro / 100 - tW * 0.7); } progesssValue.setLayoutParams(params); progesssValue.setText(new StringBuffer().append(progesss.getProgress()).append("%")); }