Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Android system add Custom Mouse style how to switch by pressing a key

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to add custom mouse styles to the Android system to switch by pressing buttons. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. APP modifies mouse style through View

It is easy to modify the mouse style on app view. Obtain the mouse coordinates through hover event and modify it to a custom image using the following method:

GetWindow () .getDecorView () .setPointerIcon (PointerIcon.load (getResources (), R.drawable.pointer_spot_touch_icon))

ImageView = (ImageView) findViewById (R.id.image_view); imageView.setOnHoverListener (new View.OnHoverListener () {@ SuppressLint ({"SetTextI18n", "ResourceType"}) @ Override public boolean onHover (View v, MotionEvent event) {int what = event.getAction (); textX.setText ("X:" + event.getX ()); textY.setText ("Y:" + event.getY ()) Switch (what) {case MotionEvent.ACTION_HOVER_ENTER: / / Mouse access to view Log.i (TAG, "bottom ACTION_HOVER_ENTER..."); mOrgPI = getWindow (). GetDecorView (). GetPointerIcon (); getWindow (). GetDecorView (). SetPointerIcon (PointerIcon.load (getResources (), R.drawable.pointer_spot_touch_icon)); break; case MotionEvent.ACTION_HOVER_MOVE: / / mouse over view Log.i (TAG, "bottom ACTION_HOVER_MOVE..."); break Case MotionEvent.ACTION_HOVER_EXIT: / / Mouse away from view Log.i (TAG, "bottom ACTION_HOVER_EXIT..."); getWindow () .getDecorView () .setPointerIcon (mOrgPI); break;} return false;}});}

Where pointer_spot_touch_icon.xml needs to be declared as pointer-icon:

However, when the view of app modifies the mouse style is turned off, the mouse style will revert to the default black arrow, so it is necessary to modify the system source code in the framework layer without relying on APP to dynamically change the mouse style.

Add a custom mouse style to the framework layer and switch it by pressing the key

(1) add custom style resources

The system icon resources are in the frameworks/base/core/res/res/drawable-mdpi/ directory, where pointer_arrow.png and pointer_arrow_large.png are the default black arrows of the system.

Pointer_arrow_red_dot.png and pointer_arrow_red_dot_large.png are self-added red dot style images:

Then add the corresponding xml in the frameworks/base/core/res/res/drawable/ directory:

Pointer_arrow_red_dot_icon.xml

Pointer_arrow_red_dot_large_icon.xml

Modify frameworks/base/core/res/res/values/styles.xml to add resource configuration, pay attention to name matching!

Modify the frameworks/base/core/res/res/values/attrs.xml reference resource:

(2) Java layer acquires resources

Modify the frameworks/base/core/java/android/view/PointerIcon.java and add the following definition:

Return the previously configured resources in the getSystemIconTypeIndex (int type) function:

(3) add corresponding id and load resources in C++ layer

Modify frameworks/base/core/jni/android_view_PointerIcon.h

* Pointer icon styles. * Must match the definition in android.view.PointerIcon. * / enum {POINTER_ICON_STYLE_CUSTOM =-1, POINTER_ICON_STYLE_NULL = 0, POINTER_ICON_STYLE_ARROW = 1000, POINTER_ICON_STYLE_CONTEXT_MENU = 1001, POINTER_ICON_STYLE_HAND = 1002, POINTER_ICON_STYLE_HELP = 1003, POINTER_ICON_STYLE_WAIT = 1004, POINTER_ICON_STYLE_CELL = 1006, POINTER_ICON_STYLE_CROSSHAIR = 1007, POINTER_ICON_STYLE_TEXT = 1008, POINTER_ICON_STYLE_VERTICAL_TEXT = 1009, POINTER_ICON_STYLE_ALIAS = 1010 POINTER_ICON_STYLE_COPY = 1011, POINTER_ICON_STYLE_NO_DROP = 1012, POINTER_ICON_STYLE_ALL_SCROLL = 1013, POINTER_ICON_STYLE_HORIZONTAL_DOUBLE_ARROW = 1014, POINTER_ICON_STYLE_VERTICAL_DOUBLE_ARROW = 1015, POINTER_ICON_STYLE_TOP_RIGHT_DOUBLE_ARROW = 1016, POINTER_ICON_STYLE_TOP_LEFT_DOUBLE_ARROW = 1017, POINTER_ICON_STYLE_ZOOM_IN = 1018, POINTER_ICON_STYLE_ZOOM_OUT = 1019, POINTER_ICON_STYLE_GRAB = 1020 POINTER_ICON_STYLE_GRABBING = 1021, POINTER_ICON_STYLE_SPOT_HOVER = 2000, POINTER_ICON_STYLE_SPOT_TOUCH = 2001, POINTER_ICON_STYLE_SPOT_ANCHOR = 2002, POINTER_ICON_STYLE_REDDOT = 10001, / / add enumerated definitions of custom styles Corresponds to the variables in the PointerIcon.java above}

Modify the frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp to load to the image resources corresponding to the custom enumeration variables:

Void NativeInputManager::loadAdditionalMouseResources (std::map* outResources, std::map* outAnimationResources) {JNIEnv* env = jniEnv (); for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report