下面列出了android.view.ViewConfiguration#getPressedStateDuration ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 是否是点击事件
*
* @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;
}
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);
}
@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();
}
}
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);
}
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);
}
}
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);
}
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);
}
}
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);
}
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);
}
@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();
}
}
@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();
}
}