Java Code Examples for android.view.ViewConfiguration#getPressedStateDuration()

The following examples show how to use android.view.ViewConfiguration#getPressedStateDuration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FTouchHelper.java    From switchbutton with MIT License 6 votes vote down vote up
/**
 * 是否是点击事件
 *
 * @param event
 * @param context
 * @return
 */
public boolean isClick(MotionEvent event, Context context)
{
    if (event.getAction() == MotionEvent.ACTION_UP)
    {
        final long clickTimeout = ViewConfiguration.getPressedStateDuration() + ViewConfiguration.getTapTimeout();
        final int touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        final long duration = event.getEventTime() - event.getDownTime();
        final int dx = (int) getDeltaXFromDown();
        final int dy = (int) getDeltaYFromDown();

        if (duration < clickTimeout && dx < touchSlop && dy < touchSlop)
            return true;
    }
    return false;
}
 
Example 2
Source File: SwitchButton.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private void initView(Context context) {
	mPaint = new Paint();
	mPaint.setColor(Color.WHITE);
	Resources resources = context.getResources();

	// get viewConfiguration
	mClickTimeout = ViewConfiguration.getPressedStateDuration()
			+ ViewConfiguration.getTapTimeout();
	mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

	// get Bitmap
	mBottom = BitmapFactory.decodeResource(resources, R.drawable.bottom);
	mBtnPressed = BitmapFactory.decodeResource(resources,
			R.drawable.btn_pressed);
	mBtnNormal = BitmapFactory.decodeResource(resources,
			R.drawable.btn_unpressed);
	mFrame = BitmapFactory.decodeResource(resources, R.drawable.frame);
	mMask = BitmapFactory.decodeResource(resources, R.drawable.mask);
	mCurBtnPic = mBtnNormal;

	mBtnWidth = mBtnPressed.getWidth();
	mMaskWidth = mMask.getWidth();
	mMaskHeight = mMask.getHeight();

	mBtnOffPos = mBtnWidth / 2;
	mBtnOnPos = mMaskWidth - mBtnWidth / 2;

	mBtnPos = mChecked ? mBtnOnPos : mBtnOffPos;
	mRealPos = getRealPos(mBtnPos);

	final float density = getResources().getDisplayMetrics().density;
	mVelocity = (int) (VELOCITY * density + 0.5f);
	mExtendOffsetY = (int) (EXTENDED_OFFSET_Y * density + 0.5f);

	mSaveLayerRectF = new RectF(0, mExtendOffsetY, mMask.getWidth(),
			mMask.getHeight() + mExtendOffsetY);
	mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
}
 
Example 3
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
  float[] precision = precisionDescriber.describePrecision();
  float[] start = this.start.calculateCoordinates(view);
  float[] end = this.end.calculateCoordinates(view);
  float[][] steps = interpolate(start, end, STEPS);
  int delayBetweenMovements = DURATION / steps.length;
  // Down
  MotionEvent downEvent = MotionEvents.sendDown(uiController, start, precision).down;
  try {
    for (int i = 0; i < steps.length; i++) {
      // Wait
      long desiredTime = downEvent.getDownTime() + (long) (delayBetweenMovements * i);
      long timeUntilDesired = desiredTime - SystemClock.uptimeMillis();
      if (timeUntilDesired > 10L) {
        uiController.loopMainThreadForAtLeast(timeUntilDesired);
      }
      // Move
      if (!MotionEvents.sendMovement(uiController, downEvent, steps[i])) {
        MotionEvents.sendCancel(uiController, downEvent);
        throw new RuntimeException("Cannot drag: failed to send a move event.");
      }
    }
    int duration = ViewConfiguration.getPressedStateDuration();
    if (duration > 0) {
      uiController.loopMainThreadForAtLeast((long) duration);
    }
  } finally {
    downEvent.recycle();
  }
}
 
Example 4
Source File: SwitchButton.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
private void initView(Context context) {
    mPaint = new Paint();
    mPaint.setColor(Color.WHITE);
    Resources resources = context.getResources();

    // get viewConfiguration
    mClickTimeout = ViewConfiguration.getPressedStateDuration()
            + ViewConfiguration.getTapTimeout();
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

    // get Bitmap
    mBottom = BitmapFactory.decodeResource(resources, R.drawable.switch_btn_bottom);
    mBtnPressed = BitmapFactory.decodeResource(resources, R.drawable.switch_btn_pressed);
    mBtnNormal = BitmapFactory.decodeResource(resources, R.drawable.swithc_btn_unpressed);
    mFrame = BitmapFactory.decodeResource(resources, R.drawable.switch_btn_frame);
    mMask = BitmapFactory.decodeResource(resources, R.drawable.switch_btn_mask);
    mCurBtnPic = mBtnNormal;

    mBtnWidth = mBtnPressed.getWidth();
    mMaskWidth = mMask.getWidth();
    mMaskHeight = mMask.getHeight();

    mBtnOffPos = mBtnWidth / 2;
    mBtnOnPos = mMaskWidth - mBtnWidth / 2;

    mBtnPos = mChecked ? mBtnOnPos : mBtnOffPos;
    mRealPos = getRealPos(mBtnPos);

    final float density = getResources().getDisplayMetrics().density;
    mVelocity = (int) (VELOCITY * density + 0.5f);
    mExtendOffsetY = (int) (EXTENDED_OFFSET_Y * density + 0.5f);

    mSaveLayerRectF = new RectF(0, mExtendOffsetY, mMask.getWidth(), mMask.getHeight()
            + mExtendOffsetY);
    mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
}
 
Example 5
Source File: SwitchButton.java    From MaterialQQLite with Apache License 2.0 5 votes vote down vote up
private void initView() {
	mConf = Configuration.getDefault(getContext().getResources().getDisplayMetrics().density);
	mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
	mClickTimeout = ViewConfiguration.getPressedStateDuration() + ViewConfiguration.getTapTimeout();
	mAnimationController = AnimationController.getDefault().init(mOnAnimateListener);
	mBounds = new Rect();
	if (SHOW_RECT) {
		mRectPaint = new Paint();
		mRectPaint.setStyle(Style.STROKE);
	}
}
 
Example 6
Source File: SwitchButton.java    From SettingView with Apache License 2.0 5 votes vote down vote up
private void init(Context context, AttributeSet attrs) {
	mPaint = new Paint();
	mPaint.setColor(Color.WHITE);
	Resources resources = context.getResources();

	int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.switch_button_width), getResources()
			.getDisplayMetrics());
	int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.switch_button_height), getResources()
			.getDisplayMetrics());

	// get viewConfiguration
	mClickTimeout = ViewConfiguration.getPressedStateDuration() + ViewConfiguration.getTapTimeout();
	mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

	// get Bitmap
	bmBgGreen = BitmapFactory.decodeResource(resources, R.drawable.switch_btn_bg_green);
	bmBgWhite = BitmapFactory.decodeResource(resources, R.drawable.switch_btn_bg_white);
	bmBtnNormal = BitmapFactory.decodeResource(resources, R.drawable.switch_btn_normal);
	bmBtnPressed = BitmapFactory.decodeResource(resources, R.drawable.switch_btn_pressed);

	// size Bitmap
	bmBgGreen = Bitmap.createScaledBitmap(bmBgGreen, width, height, true);
	bmBgWhite = Bitmap.createScaledBitmap(bmBgWhite, width, height, true);
	bmBtnNormal = Bitmap.createScaledBitmap(bmBtnNormal, height, height, true);
	bmBtnPressed = Bitmap.createScaledBitmap(bmBtnPressed, height, height, true);

	bmCurBtnPic = bmBtnNormal;// 初始按钮图片
	bmCurBgPic = mChecked ? bmBgGreen : bmBgWhite;// 初始背景图片
	bgWidth = bmBgGreen.getWidth();// 背景宽度
	bgHeight = bmBgGreen.getHeight();// 背景高度
	btnWidth = bmBtnNormal.getWidth();// 按钮宽度
	offBtnPos = 0;// 关闭时在最左边
	onBtnPos = bgWidth - btnWidth;// 开始时在右边
	curBtnPos = mChecked ? onBtnPos : offBtnPos;// 按钮当前为初始位置

	// get density
	float density = resources.getDisplayMetrics().density;
	mVelocity = (int) (VELOCITY * density + 0.5f);// 动画距离
	mSaveLayerRectF = new RectF(0, 0, bgWidth, bgHeight);
}
 
Example 7
Source File: SwitchButton.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private void initView() {
	mConf = Configuration.getDefault(getContext().getResources().getDisplayMetrics().density);
	mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
	mClickTimeout = ViewConfiguration.getPressedStateDuration() + ViewConfiguration.getTapTimeout();
	mAnimationController = AnimationController.getDefault().init(mOnAnimateListener);
	mBounds = new Rect();
	if (SHOW_RECT) {
		mRectPaint = new Paint();
		mRectPaint.setStyle(Style.STROKE);
	}
}
 
Example 8
Source File: SwitchButton.java    From BlunoAccessoryShieldDemo with GNU General Public License v3.0 5 votes vote down vote up
private void initView(Context context) {
    mPaint = new Paint();
    mPaint.setColor(Color.WHITE);
    Resources resources = context.getResources();

    // get viewConfiguration
    mClickTimeout = ViewConfiguration.getPressedStateDuration()
            + ViewConfiguration.getTapTimeout();
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

    // get Bitmap
    mBottom = BitmapFactory.decodeResource(resources, R.drawable.bottom);
    mBtnPressed = BitmapFactory.decodeResource(resources, R.drawable.btn_pressed);
    mBtnNormal = BitmapFactory.decodeResource(resources, R.drawable.btn_unpressed);
    mFrame = BitmapFactory.decodeResource(resources, R.drawable.frame);
    mMask = BitmapFactory.decodeResource(resources, R.drawable.mask);
    mCurBtnPic = mBtnNormal;

    mBtnWidth = mBtnPressed.getWidth();
    mMaskWidth = mMask.getWidth();
    mMaskHeight = mMask.getHeight();

    mBtnOffPos = mBtnWidth / 2;
    mBtnOnPos = mMaskWidth - mBtnWidth / 2;

    mBtnPos = mChecked ? mBtnOnPos : mBtnOffPos;
    mRealPos = getRealPos(mBtnPos);

    final float density = getResources().getDisplayMetrics().density;
    mVelocity = (int) (VELOCITY * density + 0.5f);
    mExtendOffsetY = (int) (EXTENDED_OFFSET_Y * density + 0.5f);

    mSaveLayerRectF = new RectF(0, mExtendOffsetY, mMask.getWidth(), mMask.getHeight()
            + mExtendOffsetY);
    mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
}
 
Example 9
Source File: SettingSwitchButton.java    From WifiChat with GNU General Public License v2.0 5 votes vote down vote up
private void initView(Context context) {
    mPaint = new Paint();
    mPaint.setColor(Color.WHITE);
    Resources resources = context.getResources();

    // get viewConfiguration
    mClickTimeout = ViewConfiguration.getPressedStateDuration()
            + ViewConfiguration.getTapTimeout();
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

    // get Bitmap
    mBottom = BitmapFactory.decodeResource(resources, R.drawable.bottom);
    mBtnPressed = BitmapFactory.decodeResource(resources, R.drawable.btn_pressed);
    mBtnNormal = BitmapFactory.decodeResource(resources, R.drawable.btn_unpressed);
    mFrame = BitmapFactory.decodeResource(resources, R.drawable.frame);
    mMask = BitmapFactory.decodeResource(resources, R.drawable.mask);
    mCurBtnPic = mBtnNormal;

    mBtnWidth = mBtnPressed.getWidth();
    mMaskWidth = mMask.getWidth();
    mMaskHeight = mMask.getHeight();

    mBtnOffPos = mBtnWidth / 2;
    mBtnOnPos = mMaskWidth - mBtnWidth / 2;

    mBtnPos = mChecked ? mBtnOnPos : mBtnOffPos;
    mRealPos = getRealPos(mBtnPos);

    final float density = getResources().getDisplayMetrics().density;
    mVelocity = (int) (VELOCITY * density + 0.5f);
    mExtendOffsetY = (int) (EXTENDED_OFFSET_Y * density + 0.5f);

    mSaveLayerRectF = new RectF(0, mExtendOffsetY, mMask.getWidth(), mMask.getHeight()
            + mExtendOffsetY);
    mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
}
 
Example 10
Source File: NoConstraintsSwipeAction.java    From material-activity-chooser with Apache License 2.0 4 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
  float[] startCoordinates = startCoordinatesProvider.calculateCoordinates(view);
  float[] endCoordinates = endCoordinatesProvider.calculateCoordinates(view);
  float[] precision = precisionDescriber.describePrecision();

  Swiper.Status status = Swiper.Status.FAILURE;

  for (int tries = 0; tries < MAX_TRIES && status != Swiper.Status.SUCCESS; tries++) {
    try {
      status = swiper.sendSwipe(uiController, startCoordinates, endCoordinates, precision);
    } catch (RuntimeException re) {
      throw new PerformException.Builder()
          .withActionDescription(this.getDescription())
          .withViewDescription(HumanReadables.describe(view))
          .withCause(re)
          .build();
    }

    int duration = ViewConfiguration.getPressedStateDuration();
    // ensures that all work enqueued to process the swipe has been run.
    if (duration > 0) {
      uiController.loopMainThreadForAtLeast(duration);
    }
  }

  if (status == Swiper.Status.FAILURE) {
    throw new PerformException.Builder()
        .withActionDescription(getDescription())
        .withViewDescription(HumanReadables.describe(view))
        .withCause(new RuntimeException(String.format(
            "Couldn't swipe from: %s,%s to: %s,%s precision: %s, %s . Swiper: %s "
            + "start coordinate provider: %s precision describer: %s. Tried %s times",
            startCoordinates[0],
            startCoordinates[1],
            endCoordinates[0],
            endCoordinates[1],
            precision[0],
            precision[1],
            swiper,
            startCoordinatesProvider,
            precisionDescriber,
            MAX_TRIES)))
        .build();
  }
}
 
Example 11
Source File: GeneralSwipeAction.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
  float[] startCoordinates = startCoordinatesProvider.calculateCoordinates(view);
  float[] endCoordinates = endCoordinatesProvider.calculateCoordinates(view);
  float[] precision = precisionDescriber.describePrecision();

  Swiper.Status status = Swiper.Status.FAILURE;

  for (int tries = 0; tries < MAX_TRIES && status != Swiper.Status.SUCCESS; tries++) {
    try {
      status = swiper.sendSwipe(uiController, startCoordinates, endCoordinates, precision);
    } catch (RuntimeException re) {
      throw new PerformException.Builder()
          .withActionDescription(this.getDescription())
          .withViewDescription(HumanReadables.describe(view))
          .withCause(re)
          .build();
    }

    int duration = ViewConfiguration.getPressedStateDuration();
    // ensures that all work enqueued to process the swipe has been run.
    if (duration > 0) {
      uiController.loopMainThreadForAtLeast(duration);
    }
  }

  if (status == Swiper.Status.FAILURE) {
    throw new PerformException.Builder()
        .withActionDescription(getDescription())
        .withViewDescription(HumanReadables.describe(view))
        .withCause(
            new RuntimeException(
                String.format(
                    Locale.ROOT,
                    "Couldn't swipe from: %s,%s to: %s,%s precision: %s, %s . Swiper: %s "
                        + "start coordinate provider: %s precision describer: %s. Tried %s times",
                    startCoordinates[0],
                    startCoordinates[1],
                    endCoordinates[0],
                    endCoordinates[1],
                    precision[0],
                    precision[1],
                    swiper,
                    startCoordinatesProvider,
                    precisionDescriber,
                    MAX_TRIES)))
        .build();
  }
}