下面列出了怎么用android.view.SoundEffectConstants的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Handles left, right, and clicking
* @see android.view.View#onKeyDown
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
if (moveDirection(-1)) {
playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
return true;
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if (moveDirection(1)) {
playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
return true;
}
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
mReceivedInvokeKeyDown = true;
// fallthrough to default handling
}
return super.onKeyDown(keyCode, event);
}
/**
* Helper method to be used for playing sound effects.
*/
@Thunk static void playSoundEffect(int keyCode, View v) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_PAGE_DOWN:
case KeyEvent.KEYCODE_MOVE_END:
v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
break;
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_PAGE_UP:
case KeyEvent.KEYCODE_MOVE_HOME:
v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
break;
default:
break;
}
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
View view = mList.findChildViewUnder(e.getX(), e.getY());
if (view != null) {
view.playSoundEffect(SoundEffectConstants.CLICK);
int pos = mList.getChildPosition(view);
Intent i = new Intent(getActivity(), ViewEntryActivity.class);
Bundle b = new Bundle();
b.putParcelable(ViewEntryActivity.ENTRY_KEY, mItems.get(pos - mAdapter.getSectionOffset(pos)));
i.putExtras(b);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.buildDrawingCache(true);
Bitmap drawingCache = view.getDrawingCache(true);
Bundle bundle = ActivityOptions.makeThumbnailScaleUpAnimation(view, drawingCache, 0, 0).toBundle();
getActivity().startActivity(i, bundle);
} else {
startActivity(i);
}
}
return super.onSingleTapUp(e);
}
@Override
public boolean performClick() {
toggle();
if (onCheckedChangeListener != null)
onCheckedChangeListener.onCheckedChanged(this, isChecked());
final boolean handled = super.performClick();
if (!handled) {
// View only makes a sound effect if the onClickListener was
// called, so we'll need to make one here instead.
playSoundEffect(SoundEffectConstants.CLICK);
}
return handled;
}
@Override
public boolean onLongClick(View v) {
if (mOnCircularViewObjectClickListener != null && isLongClickable()) {
mLongClickRegistered = true;
if (mTouchEventMarkerPos == -1) {
playSoundEffect(SoundEffectConstants.CLICK);
mOnCircularViewObjectClickListener.onClick(CircularView.this, true);
mTouchEventMarkerPos = null;
} else if (mTouchEventMarker != null) {
playSoundEffect(SoundEffectConstants.CLICK);
mOnCircularViewObjectClickListener.onMarkerClick(CircularView.this, mTouchEventMarker, mTouchEventMarkerPos, true);
mTouchEventMarker = null;
mTouchEventMarkerPos = null;
}
return mLongClickRegistered;
}
return false;
}
/**
* Handles left, right, and clicking
*
* @see View#onKeyDown
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
if (movePrevious()) {
playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
}
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if (moveNext()) {
playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
}
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
mReceivedInvokeKeyDown = true;
// fallthrough to default handling
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean performClick() {
final boolean handled = super.performClick();
if (mTab != null) {
if (!handled) {
playSoundEffect(SoundEffectConstants.CLICK);
}
mTab.select();
return true;
} else {
return handled;
}
}
@Override
public boolean performClick() {
// Send the appropriate accessibility events and call listeners
boolean handled = super.performClick();
if (!handled) {
playSoundEffect(SoundEffectConstants.CLICK);
}
return showDialog() || handled;
}
@Override
boolean performItemClick(RecyclerView parent, View view, int position, long id) {
if (mItemClickListener != null) {
view.playSoundEffect(SoundEffectConstants.CLICK);
mItemClickListener.onItemClick(parent, view, position, id);
return true;
}
return false;
}
/**
* Call the OnItemClickListener, if it is defined.
*
* @param view
* The view within the AdapterView that was clicked.
* @param position
* The position of the view in the adapter.
* @param id
* The row id of the item that was clicked.
* @return True if there was an assigned OnItemClickListener that was
* called, false otherwise is returned.
*/
public boolean performItemClick(View view, int position, long id) {
if (mOnItemClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
if (view != null) {
view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
}
mOnItemClickListener.onItemClick(this.mAdapter, view, position, id);
return true;
}
return false;
}
/**
* Scrolls to the next or previous item if possible.
*
* @param direction either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN}
*
* @return whether selection was moved
*/
boolean arrowScroll(int direction) {
try {
mInLayout = true;
final boolean handled = arrowScrollImpl(direction);
if (handled) {
playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
}
return handled;
} finally {
mInLayout = false;
}
}
@Override
boolean performItemClick(RecyclerView parent, View view, int position, long id) {
if (mItemClickListener != null) {
view.playSoundEffect(SoundEffectConstants.CLICK);
mItemClickListener.onItemClick(parent, view, position, id);
return true;
}
return false;
}
@Override
public boolean performClick() {
if (super.performClick()) {
return true;
}
playSoundEffect(SoundEffectConstants.CLICK);
showOverflowMenu();
return true;
}
private boolean checkAudioMotionEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
boolean result = false;
int side = AndroidUtilities.dp(36);
boolean area = false;
if (miniButtonState >= 0) {
int offset = AndroidUtilities.dp(27);
area = x >= buttonX + offset && x <= buttonX + offset + side && y >= buttonY + offset && y <= buttonY + offset + side;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (area) {
miniButtonPressed = true;
invalidate();
result = true;
updateRadialProgressBackground();
}
} else if (miniButtonPressed) {
if (event.getAction() == MotionEvent.ACTION_UP) {
miniButtonPressed = false;
playSoundEffect(SoundEffectConstants.CLICK);
didPressedMiniButton(true);
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
miniButtonPressed = false;
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!area) {
miniButtonPressed = false;
invalidate();
}
}
updateRadialProgressBackground();
}
return result;
}
@Override
public boolean performClick() {
if (super.performClick()) {
return true;
}
playSoundEffect(SoundEffectConstants.CLICK);
showOverflowMenu();
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = false;
checkFrame.getHitRect(rect);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (rect.contains((int) event.getX(), (int) event.getY())) {
pressed = true;
invalidate();
result = true;
}
} else if (pressed) {
if (event.getAction() == MotionEvent.ACTION_UP) {
getParent().requestDisallowInterceptTouchEvent(true);
pressed = false;
playSoundEffect(SoundEffectConstants.CLICK);
delegate.onCheckClick(this);
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
pressed = false;
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
pressed = false;
invalidate();
}
}
}
if (!result) {
result = super.onTouchEvent(event);
}
return result;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = false;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
pressed = true;
invalidate();
result = true;
} else if (pressed) {
if (event.getAction() == MotionEvent.ACTION_UP) {
getParent().requestDisallowInterceptTouchEvent(true);
pressed = false;
playSoundEffect(SoundEffectConstants.CLICK);
delegate.didSelectBot(MessagesController.getInstance(currentAccount).getUser(DataQuery.getInstance(currentAccount).inlineBots.get((Integer) getTag()).peer.user_id));
setUseRevealAnimation(false);
dismiss();
setUseRevealAnimation(true);
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
pressed = false;
invalidate();
}
}
if (!result) {
result = super.onTouchEvent(event);
} else {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startCheckLongPress();
}
}
if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) {
cancelCheckLongPress();
}
return result;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
int position = mDecor.findHeaderPositionUnder((int) e.getX(), (int) e.getY());
if (position != -1) {
View headerView = mDecor.getHeaderView(mRecyclerView, position);
long headerId = getAdapter().getHeaderId(position);
mOnHeaderClickListener.onHeaderClick(headerView, position, headerId);
mRecyclerView.playSoundEffect(SoundEffectConstants.CLICK);
headerView.onTouchEvent(e);
return true;
}
return false;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
int position = mDecor.findHeaderPositionUnder((int) e.getX(), (int) e.getY());
if (position != -1) {
View headerView = mDecor.getHeaderView(mRecyclerView, position);
long headerId = getAdapter().getHeaderId(position);
mOnHeaderClickListener.onHeaderClick(headerView, position, headerId);
mRecyclerView.playSoundEffect(SoundEffectConstants.CLICK);
headerView.onTouchEvent(e);
return true;
}
return false;
}
@Override
public boolean performClick() {
toggle();
final boolean handled = super.performClick();
if (!handled) {
// View only makes a sound effect if the onClickListener was
// called, so we'll need to make one here instead.
playSoundEffect(SoundEffectConstants.CLICK);
}
return handled;
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent motionEvent) {
View child = rv.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
if (child != null && gestureDetector.onTouchEvent(motionEvent)) {
child.playSoundEffect(SoundEffectConstants.CLICK);
int position = rv.getChildAdapterPosition(child);
callback.onFeedbackItemClick(position);
}
return false;
}
@Override
public boolean performClick() {
toggle();
final boolean handled = super.performClick();
if (!handled) {
// View only makes a sound effect if the onClickListener was
// called, so we'll need to make one here instead.
playSoundEffect(SoundEffectConstants.CLICK);
}
return handled;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = false;
checkFrame.getHitRect(rect);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (rect.contains((int) event.getX(), (int) event.getY())) {
pressed = true;
invalidate();
result = true;
}
} else if (pressed) {
if (event.getAction() == MotionEvent.ACTION_UP) {
getParent().requestDisallowInterceptTouchEvent(true);
pressed = false;
playSoundEffect(SoundEffectConstants.CLICK);
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
delegate.onCheckClick(this);
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
pressed = false;
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
pressed = false;
invalidate();
}
}
}
if (!result) {
result = super.onTouchEvent(event);
}
return result;
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if (gainFocus) {
mWholeRow.setBackgroundResource(R.drawable.btn_focus);
playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
if (rowSelectedListener != null) rowSelectedListener.onRowSelected(this);
} else {
mWholeRow.setBackground(normalBackground);
}
}
private boolean checkAudioMotionEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
boolean result = false;
int side = AndroidUtilities.dp(36);
boolean area = false;
if (miniButtonState >= 0) {
int offset = AndroidUtilities.dp(27);
area = x >= buttonX + offset && x <= buttonX + offset + side && y >= buttonY + offset && y <= buttonY + offset + side;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (area) {
miniButtonPressed = true;
invalidate();
result = true;
updateRadialProgressBackground();
}
} else if (miniButtonPressed) {
if (event.getAction() == MotionEvent.ACTION_UP) {
miniButtonPressed = false;
playSoundEffect(SoundEffectConstants.CLICK);
didPressedMiniButton(true);
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
miniButtonPressed = false;
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!area) {
miniButtonPressed = false;
invalidate();
}
}
updateRadialProgressBackground();
}
return result;
}
/**
* Call the OnItemClickListener, if it is defined.
*
* @param view The view within the AdapterView that was clicked.
* @param position The position of the view in the adapter.
* @param id The row id of the item that was clicked.
* @return True if there was an assigned OnItemClickListener that was
* called, false otherwise is returned.
*/
public boolean performItemClick(View view, int position, long id) {
if (mOnItemClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
mOnItemClickListener.onItemClick(this, view, position, id);
return true;
}
return false;
}
/**
* Call the OnItemClickListener, if it is defined.
*
* @param view The view within the AdapterView that was clicked.
* @param position The position of the view in the adapter.
* @param id The row id of the item that was clicked.
* @return True if there was an assigned OnItemClickListener that was
* called, false otherwise is returned.
*/
public boolean performItemClick(View view, int position, long id) {
if (mOnItemClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
mOnItemClickListener.onItemClick(this, view, position, id);
return true;
}
return false;
}
public boolean arrowScroll(int direction) {
View currentFocused = findFocus();
if (currentFocused == this) currentFocused = null;
boolean handled = false;
View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
direction);
if (nextFocused != null && nextFocused != currentFocused) {
if (direction == View.FOCUS_LEFT) {
// If there is nothing to the left, or this is causing us to
// jump to the right, then what we really want to do is page left.
if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {
handled = pageLeft();
} else {
handled = nextFocused.requestFocus();
}
} else if (direction == View.FOCUS_RIGHT) {
// If there is nothing to the right, or this is causing us to
// jump to the left, then what we really want to do is page right.
if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
handled = pageRight();
} else {
handled = nextFocused.requestFocus();
}
}
} else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
// Trying to move left and nothing there; try to page.
handled = pageLeft();
} else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
// Trying to move right and nothing there; try to page.
handled = pageRight();
}
if (handled) {
playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
}
return handled;
}
@Override
public boolean performClick() {
toggle();
final boolean handled = super.performClick();
if (!handled) {
// View only makes a sound effect if the onClickListener was
// called, so we'll need to make one here instead.
playSoundEffect(SoundEffectConstants.CLICK);
}
return handled;
}
@Override
public boolean performClick() {
if (super.performClick()) {
return true;
}
playSoundEffect(SoundEffectConstants.CLICK);
showOverflowMenu();
return true;
}