下面列出了怎么用android.view.HapticFeedbackConstants的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onLongPress(MotionEvent e) {
unpressTouchedChild();
final int index = getChildIndex((int) e.getX(), (int) e.getY());
if (index >= 0 && !mBlockTouchAction) {
View child = getChildAt(index);
OnItemLongClickListener onItemLongClickListener = getOnItemLongClickListener();
if (onItemLongClickListener != null) {
int adapterIndex = mLeftViewAdapterIndex + index;
boolean handled = onItemLongClickListener.onItemLongClick(HorizontalListView.this, child, adapterIndex, mAdapter
.getItemId(adapterIndex));
if (handled) {
// BZZZTT!!1!
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
}
}
}
private boolean dispatchLongPress(View view, int position, long id, float x, float y,
boolean useOffsets) {
boolean handled = false;
if (mOnItemLongClickListener != null) {
handled = mOnItemLongClickListener.onItemLongClick(this, mDownTouchView,
mDownTouchPosition, id);
}
if (!handled) {
mContextMenuInfo = new AdapterContextMenuInfo(view, position, id);
if (useOffsets) {
handled = super.showContextMenuForChild(view, x, y);
} else {
handled = super.showContextMenuForChild(this);
}
}
if (handled) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return handled;
}
@Override
public boolean onDown(MotionEvent e) {
int x = screenToViewX((int) e.getX());
int y = screenToViewY((int) e.getY());
_isCaretTouched = isNearChar(x, y, _textField.getCaretPosition());
if (_textField.isFlingScrolling()) {
_textField.stopFlingScrolling();
} else if (_textField.isSelectText()) {
if (isNearChar(x, y, _textField.getSelectionStart())) {
_textField.focusSelectionStart();
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
_isCaretTouched = true;
} else if (isNearChar(x, y, _textField.getSelectionEnd())) {
_textField.focusSelectionEnd();
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
_isCaretTouched = true;
}
}
if (_isCaretTouched) {
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return true;
}
private void checkForLink(String message, SpannableStringBuilder spanbuilder) {
Matcher linkMatcher = Patterns.WEB_URL.matcher(message);
while (linkMatcher.find()) {
String url = linkMatcher.group(0);
if (!url.matches("^https?://.+"))
url = "http://" + url;
final String finalUrl = url;
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
CustomTabsIntent.Builder mTabs = new CustomTabsIntent.Builder();
mTabs.setStartAnimations(context, R.anim.slide_in_bottom_anim, R.anim.fade_out_semi_anim);
mTabs.setExitAnimations(context, R.anim.fade_in_semi_anim, R.anim.slide_out_bottom_anim);
mTabs.build().launchUrl(context, Uri.parse(finalUrl));
mRecyclerView.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
}
};
spanbuilder.setSpan(clickableSpan, linkMatcher.start(), linkMatcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
boolean performLongPress(final View child,
final int longPressPosition, final long longPressId) {
// CHOICE_MODE_MULTIPLE_MODAL takes over long press.
if (mChoiceMode == AbsListView.CHOICE_MODE_MULTIPLE_MODAL) {
if (mChoiceActionMode == null) {
setItemChecked(longPressPosition, true);
child.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return true;
}
boolean handled = false;
if (mOnItemLongClickListener != null) {
handled = mOnItemLongClickListener.onItemLongClick(this, child,
longPressPosition, longPressId);
}
if (handled) {
child.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return handled;
}
public EntityView(Context context, Point pos) {
super(context);
uuid = UUID.randomUUID();
position = pos;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
if (hasPanned || hasTransformed || hasReleased) {
return;
}
recognizedLongPress = true;
if (delegate != null) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
delegate.onEntityLongClicked(EntityView.this);
}
}
});
}
@TargetApi(VERSION_CODES.HONEYCOMB_MR1)
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
view.postDelayed(repeater, VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1
? ViewConfiguration.getKeyRepeatTimeout()
: ViewConfiguration.getLongPressTimeout());
performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
return false;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
view.removeCallbacks(repeater);
return false;
default:
return false;
}
}
public static void startDrag(View view){
DraggableInfo tag = (DraggableInfo) view.getTag();
if (tag == null){
tag = new DraggableInfo("Text", 0, 0, 1);
}
Intent intent = new Intent();
intent.putExtra("data", tag);
ClipData dragData = ClipData.newIntent("value", intent);
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(view);
// 震动反馈,不需要震动权限
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
view.startDragAndDrop(dragData, myShadow, null, 0);
}else{
view.startDrag(dragData, myShadow, null, 0);
}
}
private boolean dispatchLongPress(View view, int position, long id) {
boolean handled = false;
if (mOnItemLongClickListener != null) {
handled = mOnItemLongClickListener.onItemLongClick(this, mDownTouchView, mDownTouchPosition, id);
}
if (!handled) {
mContextMenuInfo = new AdapterContextMenuInfo(view, position, id);
handled = super.showContextMenuForChild(this);
}
if (handled) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return handled;
}
@Override
public boolean onDown(MotionEvent e) {
int x = screenToViewX((int) e.getX());
int y = screenToViewY((int) e.getY());
_isCaretTouched = isNearChar(x, y, _textField.getCaretPosition());
if (_textField.isFlingScrolling()) {
_textField.stopFlingScrolling();
} else if (_textField.isSelectText()) {
if (isNearChar(x, y, _textField.getSelectionStart())) {
_textField.focusSelectionStart();
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
_isCaretTouched = true;
} else if (isNearChar(x, y, _textField.getSelectionEnd())) {
_textField.focusSelectionEnd();
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
_isCaretTouched = true;
}
}
if (_isCaretTouched) {
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return true;
}
public EntityView(Context context, Point pos) {
super(context);
uuid = UUID.randomUUID();
position = pos;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
if (hasPanned || hasTransformed || hasReleased) {
return;
}
recognizedLongPress = true;
if (delegate != null) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
delegate.onEntityLongClicked(EntityView.this);
}
}
});
}
@Override
public boolean onDown(MotionEvent e) {
int x = screenToViewX((int) e.getX());
int y = screenToViewY((int) e.getY());
_isCaretTouched = isNearChar(x, y, _textField.getCaretPosition());
if (_textField.isFlingScrolling()) {
_textField.stopFlingScrolling();
} else if (_textField.isSelectText()) {
if (isNearChar(x, y, _textField.getSelectionStart())) {
_textField.focusSelectionStart();
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
_isCaretTouched = true;
} else if (isNearChar(x, y, _textField.getSelectionEnd())) {
_textField.focusSelectionEnd();
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
_isCaretTouched = true;
}
}
if (_isCaretTouched) {
_textField.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
unpressTouchedChild();
final int index = getChildIndex((int) e.getX(), (int) e.getY());
if (index >= 0 && !mBlockTouchAction) {
View child = getChildAt(index);
OnItemLongClickListener onItemLongClickListener = getOnItemLongClickListener();
if (onItemLongClickListener != null) {
int adapterIndex = mLeftViewAdapterIndex + index;
boolean handled = onItemLongClickListener.onItemLongClick(HorizontalListView.this, child, adapterIndex, mAdapter
.getItemId(adapterIndex));
if (handled) {
// BZZZTT!!1!
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
}
}
}
private void checkForLink(String message, SpannableStringBuilder spanbuilder) {
linkMatcher = linkPattern.matcher(message);
while(linkMatcher.find()) {
final String url = linkMatcher.group(1);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
CustomTabsIntent.Builder mTabs = new CustomTabsIntent.Builder();
mTabs.setStartAnimations(context, R.anim.slide_in_bottom_anim, R.anim.fade_out_semi_anim);
mTabs.setExitAnimations(context, R.anim.fade_in_semi_anim, R.anim.slide_out_bottom_anim);
mTabs.build().launchUrl(context, Uri.parse(url));
mRecyclerView.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
}
};
int start = message.indexOf(url);
spanbuilder.setSpan(clickableSpan, start, start + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
@Override
public void bind(Context context, final ViewHolder viewHolder) {
super.bind(context, viewHolder);
viewHolder.itemView.findViewById(R.id.first).setOnLongClickListener(v -> {
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
Glide.with(v.getContext()).load(CABBAGE_URL).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
ImageView imageView = viewHolder.itemView.findViewById(R.id.firstImage);
if (imageView != null)
imageView.setImageDrawable(resource);
}
});
return false;
});
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_clear:
mSearchView.setText(null);
mSearchView.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
break;
case R.id.menu_toggle_icon:
item.setChecked(!item.isChecked());
mSearchView.showIcon(item.isChecked());
break;
case R.id.menu_tts:
PackageUtils.startTextToSpeech(this, getString(R.string.speech_prompt), REQ_CODE_SPEECH_INPUT);
break;
case R.id.menu_icon_search:
case R.id.menu_icon_drawer:
case R.id.menu_icon_custom:
updateNavigationIcon(item.getItemId());
Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private void initDelete() {
mDelete.setOnClickListener(v -> new AlertDialog.Builder(getActivity())
.setTitle(mTimes.getName())
.setMessage(getString(R.string.delAlarmConfirm, mAlarm.getTitle()))
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.setPositiveButton(R.string.yes, (dialog, which) -> {
mTimes.getUserAlarms().remove(mAlarm);
dismiss();
}).show());
if (BuildConfig.DEBUG)
mDelete.setOnLongClickListener(v -> {
getActivity().getWindow().getDecorView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
AlarmService.setAlarm(getActivity(), new Pair<>(mAlarm, LocalDateTime.now().plusSeconds(5)));
return true;
});
}
private void growView(@NonNull View view) {
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
view.animate()
.scaleY(1.5f)
.scaleX(1.5f)
.translationY(-selectedVerticalTranslation)
.setDuration(200)
.setInterpolator(INTERPOLATOR)
.start();
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
view.postDelayed(repeater, ViewConfiguration.getKeyRepeatTimeout());
performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
return false;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
view.removeCallbacks(repeater);
return false;
default:
return false;
}
}
@Override
public void onLongPress(@NonNull MotionEvent e) {
if (mDownTouchPosition < 0) {
return;
}
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
final long id = getItemIdAtPosition(mDownTouchPosition);
dispatchLongPress(mDownTouchView, mDownTouchPosition, id, e.getX(), e.getY(), true);
}
public void performHapticFeedback(final View viewToPerformHapticFeedbackOn) {
if (!mSettingsValues.mVibrateOn) {
return;
}
if (mSettingsValues.mKeypressVibrationDuration >= 0) {
vibrate(mSettingsValues.mKeypressVibrationDuration);
return;
}
// Go ahead with the system default
if (viewToPerformHapticFeedbackOn != null) {
viewToPerformHapticFeedbackOn.performHapticFeedback(
HapticFeedbackConstants.KEYBOARD_TAP,
HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
}
public void run() {
if (checkingForLongPress && getParent() != null && currentPressCount == pressCount) {
checkingForLongPress = false;
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
onLongPress();
MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
onTouchEvent(event);
event.recycle();
}
}
public void run() {
if (checkingForLongPress && getParent() != null && currentPressCount == pressCount) {
checkingForLongPress = false;
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
if (pressedLink >= 0) {
delegate.onLinkLongPress(links.get(pressedLink));
}
MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
onTouchEvent(event);
event.recycle();
}
}
private void postBackspaceRunnable(final int time) {
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
if (!backspacePressed) {
return;
}
if (listener != null && listener.onBackspace()) {
backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
}
backspaceOnce = true;
postBackspaceRunnable(Math.max(50, time - 100));
}
}, time);
}
public void run() {
if (checkingForLongPress && getParent() != null && currentPressCount == pressCount) {
checkingForLongPress = false;
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
onLongPress();
MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
onTouchEvent(event);
event.recycle();
}
}
public void performHapticFeedback(final View viewToPerformHapticFeedbackOn) {
if (!mSettingsValues.mVibrateOn) {
return;
}
if (mSettingsValues.mKeypressVibrationDuration >= 0) {
vibrate(mSettingsValues.mKeypressVibrationDuration);
return;
}
// Go ahead with the system default
if (viewToPerformHapticFeedbackOn != null) {
viewToPerformHapticFeedbackOn.performHapticFeedback(
HapticFeedbackConstants.KEYBOARD_TAP,
HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
}
@Override
public void onLongPress(MotionEvent e) {
// Log.d("mobeta", "lift listener long pressed");
if (mHitPos != MISS && mDragInitMode == ON_LONG_PRESS) {
mDslv.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
startDrag(mHitPos, mCurrX - mItemX, mCurrY - mItemY);
}
}
@Override
public void onLongPress(MotionEvent e)
{
// Log.d("mobeta", "lift listener long pressed");
if (mHitPos != MISS && mDragInitMode == ON_LONG_PRESS)
{
mDslv.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
startDrag(mHitPos, mCurrX - mItemX, mCurrY - mItemY);
}
}
private boolean performAdapterViewItemLongClick() {
if (getOpenStatus() != Status.Close) return false;
ViewParent t = getParent();
if (t instanceof AdapterView) {
AdapterView view = (AdapterView) t;
int p = view.getPositionForView(SwipeViewLayout.this);
if (p == AdapterView.INVALID_POSITION) return false;
long vId = view.getItemIdAtPosition(p);
boolean handled = false;
try {
Method m = AbsListView.class.getDeclaredMethod("performLongPress", View.class, int.class, long.class);
m.setAccessible(true);
handled = (boolean) m.invoke(view, SwipeViewLayout.this, p, vId);
} catch (Exception e) {
e.printStackTrace();
if (view.getOnItemLongClickListener() != null) {
handled = view.getOnItemLongClickListener().onItemLongClick(view, SwipeViewLayout.this, p, vId);
}
if (handled) {
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
}
return handled;
}
return false;
}
public void run() {
if (checkingForLongPress && getParent() != null && currentPressCount == pressCount) {
checkingForLongPress = false;
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
if (pressedLink >= 0) {
delegate.onLinkLongPress(links.get(pressedLink));
}
MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
onTouchEvent(event);
event.recycle();
}
}