下面列出了怎么用android.view.PointerIcon的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void addView(View child) {
if (child.getLayoutParams() == null) {
final LinearLayout.LayoutParams lp = new LayoutParams(
0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
lp.setMargins(0, 0, 0, 0);
child.setLayoutParams(lp);
}
// Ensure you can navigate to the tab with the keyboard, and you can touch it
child.setFocusable(true);
child.setClickable(true);
if (child.getPointerIcon() == null) {
child.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND));
}
super.addView(child);
// TODO: detect this via geometry with a tabwidget listener rather
// than potentially interfere with the view's listener
child.setOnClickListener(new TabClickListener(getTabCount() - 1));
}
@UiThreadTest
@Test
@SmallTest
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
@TargetApi(Build.VERSION_CODES.N)
public void testPointerIcon() throws Throwable {
final Activity activity = activityTestRule.getActivity();
final PointerIcon expectedIcon = PointerIcon.getSystemIcon(activity, PointerIcon.TYPE_HAND);
final MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_HOVER_MOVE, 0, 0, 0);
final Menu menu = bottomNavigation.getMenu();
for (int i = 0; i < menu.size(); i++) {
final MenuItem item = menu.getItem(i);
assertTrue(item.isEnabled());
final View itemView = activity.findViewById(item.getItemId());
assertEquals(expectedIcon, itemView.onResolvePointerIcon(event, 0));
item.setEnabled(false);
assertEquals(null, itemView.onResolvePointerIcon(event, 0));
item.setEnabled(true);
assertEquals(expectedIcon, itemView.onResolvePointerIcon(event, 0));
}
}
@Test
@UiThreadTest
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
@TargetApi(Build.VERSION_CODES.N)
public void testPointerIcon() {
final LayoutInflater inflater = LayoutInflater.from(activityTestRule.getActivity());
final TabLayout tabLayout = (TabLayout) inflater.inflate(R.layout.design_tabs_items, null);
final PointerIcon expectedIcon =
PointerIcon.getSystemIcon(activityTestRule.getActivity(), PointerIcon.TYPE_HAND);
final int tabCount = tabLayout.getTabCount();
assertEquals(3, tabCount);
final MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_HOVER_MOVE, 0, 0, 0);
for (int i = 0; i < tabCount; i++) {
assertEquals(expectedIcon, tabLayout.getTabAt(i).view.onResolvePointerIcon(event, 0));
}
}
public void updateAccessibilityLargePointerFromSettings() {
final int accessibilityConfig = Settings.Secure.getIntForUser(
mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_LARGE_POINTER_ICON,
0, UserHandle.USER_CURRENT);
PointerIcon.setUseLargeIcons(accessibilityConfig == 1);
nativeReloadPointerIcons(mPtr);
}
/** @hide */
public void setCustomPointerIcon(PointerIcon icon) {
try {
mIm.setCustomPointerIcon(icon);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (getPointerIcon() == null && isClickable() && isEnabled()) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return super.onResolvePointerIcon(event, pointerIndex);
}
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (!isEnabled()) {
return null;
}
final int degrees = getDegreesFromXY(event.getX(), event.getY(), false);
if (degrees != -1) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return super.onResolvePointerIcon(event, pointerIndex);
}
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (getPointerIcon() == null && isClickable() && isEnabled()) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return super.onResolvePointerIcon(event, pointerIndex);
}
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (!isEnabled()) {
return null;
}
return super.onResolvePointerIcon(event, pointerIndex);
}
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (!isEnabled()) {
return null;
}
// Add 0.5f to event coordinates to match the logic in onTouchEvent.
final int x = (int) (event.getX() + 0.5f);
final int y = (int) (event.getY() + 0.5f);
final int dayUnderPointer = getDayAtLocation(x, y);
if (dayUnderPointer >= 0) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return super.onResolvePointerIcon(event, pointerIndex);
}
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (getPointerIcon() == null && isClickable() && isEnabled()) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return super.onResolvePointerIcon(event, pointerIndex);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (!isEnabled()) {
return null;
}
final int degrees = getDegreesFromXY(event.getX(), event.getY(), false);
if (degrees != -1) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return super.onResolvePointerIcon(event, pointerIndex);
}
@Nullable
@Override
@TargetApi(VERSION_CODES.N)
public PointerIcon onResolvePointerIcon(@NonNull MotionEvent event, int pointerIndex) {
if (getCloseIconTouchBounds().contains(event.getX(), event.getY()) && isEnabled()) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return null;
}
void overridePointerIconLocked(int touchSource) {
mTouchSource = touchSource;
if (isFromSource(InputDevice.SOURCE_MOUSE)) {
InputManager.getInstance().setPointerIconType(PointerIcon.TYPE_GRABBING);
}
}
@Override
public void setCustomPointerIcon(PointerIcon icon) {
Preconditions.checkNotNull(icon);
nativeSetCustomPointerIcon(mPtr, icon);
}
private PointerIcon getPointerIcon() {
return PointerIcon.getDefaultIcon(mContext);
}
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (mState == STATE_DRAGGING || isPointInside(event.getX(), event.getY())) {
return PointerIcon.getSystemIcon(mList.getContext(), PointerIcon.TYPE_ARROW);
}
return null;
}
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
return super.onResolvePointerIcon(event, pointerIndex);
}
/** See {@link View#setPointerIcon(PointerIcon)}. */
public static void setPointerIcon(View view, PointerIcon icon) {
view.setPointerIcon(icon);
}
/** See {@link PointerIcon#create(Bitmap, float, float)}. */
public static PointerIcon createPointerIcon(Bitmap bitmap, float width, float height) {
return PointerIcon.create(bitmap, width, height);
}
/**
* Adds support for different mouse pointer icons depending on document state and mouse position
*/
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
int icon = PointerIcon.TYPE_DEFAULT;
Layer l = mCurrentLayer;
float x = event.getX() - getViewportX();
float y = event.getY() - getViewportY();
if (mMode == MODE_LAYER_DRAG || mMode == MODE_LAYER_PRE_DRAG) {
icon = PointerIcon.TYPE_GRABBING;
} else {
if (l != null) {
if (inPointTouchRadius(x, y, l.getTopLeft())
|| inPointTouchRadius(x, y, l.getBottomRight())) {
icon = PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW;
} else if (inPointTouchRadius(x, y, l.getTopRight())
|| inPointTouchRadius(x, y, l.getBottomLeft())) {
icon = PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW;
} else if (inPointTouchRadius(x, y, l.getMidTop())
|| inPointTouchRadius(x, y, l.getMidBottom())) {
icon = PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW;
} else if (inPointTouchRadius(x, y, l.getMidLeft())
|| inPointTouchRadius(x, y, l.getMidRight())) {
icon = PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW;
} else if (l.inBounds(x, y)) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
// Only change to hand if this is a primary button click
if (event.getActionButton() == MotionEvent.BUTTON_PRIMARY) {
icon = PointerIcon.TYPE_GRABBING;
} else {
icon = PointerIcon.TYPE_DEFAULT;
}
break;
case MotionEvent.ACTION_HOVER_MOVE:
icon = PointerIcon.TYPE_GRAB;
break;
case MotionEvent.ACTION_UP:
default:
if (event.getActionButton() == MotionEvent.BUTTON_PRIMARY) {
icon = PointerIcon.TYPE_GRAB;
} else {
icon = PointerIcon.TYPE_DEFAULT;
}
}
}
}
}
return PointerIcon.getSystemIcon(getContext(), icon);
}
private static native void nativeSetCustomPointerIcon(long ptr, PointerIcon icon);