Android系统添加自定义鼠标样式通过按键切换实例详解
一、app通过view修改鼠标样式
app view上修改鼠标样式比较简单,通过 hover event 获取鼠标坐标并使用如下方法修改为自定义图片:
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: //鼠标进入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: //鼠标在view上 log.i(tag, "bottom action_hover_move..."); break; case motionevent.action_hover_exit: //鼠标离开view log.i(tag, "bottom action_hover_exit..."); getwindow().getdecorview().setpointericon(morgpi); break; } return false; } }); }
其中pointer_spot_touch_icon.xml 需要声明为 pointer-icon :
<?xml version="1.0" encoding="utf-8"?> <pointer-icon xmlns:android="http://schemas.android.com/apk/res/android" android:bitmap="@drawable/pointer_red_dot_arrow" android:hotspotx="6dp" android:hotspoty="6dp" />
二、framework层添加自定义鼠标样式并通过按键切换
(1)添加自定义样式资源
系统图标资源在 frameworks/base/core/res/res/drawable-mdpi/ 目录,其中 pointer_arrow.png、pointer_arrow_large.png 是系统默认的黑色箭头,
pointer_arrow_red_dot.png、pointer_arrow_red_dot_large.png 是自己添加的红点样式图片:
然后在 frameworks/base/core/res/res/drawable/ 目录添加对应的xml:
pointer_arrow_red_dot_icon.xml
<?xml version="1.0" encoding="utf-8"?> <pointer-icon xmlns:android="http://schemas.android.com/apk/res/android" android:bitmap="@drawable/pointer_arrow_red_dot" android:hotspotx="5dp" android:hotspoty="5dp" />
pointer_arrow_red_dot_large_icon.xml
<?xml version="1.0" encoding="utf-8"?> <pointer-icon xmlns:android="http://schemas.android.com/apk/res/android" android:bitmap="@drawable/pointer_arrow_red_dot_large" android:hotspotx="10dp" android:hotspoty="10dp" />
修改 frameworks/base/core/res/res/values/styles.xml 添加资源配置,注意名字的匹配!
修改 frameworks/base/core/res/res/values/attrs.xml 引用资源:
(2)java 层获取资源
修改 frameworks/base/core/java/android/view/pointericon.java ,添加如下定义:
在 getsystemicontypeindex(int type) 函数中返回之前配置的资源:
(3)c++层添加对应的id并加载资源
修改 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, //增加自定义样式的枚举定义,与上面 pointericon.java 中的变量对应 };
修改 frameworks/base/services/core/jni/com_android_server_input_inputmanagerservice.cpp ,加载到自定义枚举变量对应的图片资源:
void nativeinputmanager::loadadditionalmouseresources(std::map<int32_t, spriteicon>* outresources, std::map<int32_t, pointeranimation>* outanimationresources) { jnienv* env = jnienv(); for (int iconid = pointer_icon_style_context_menu; iconid <= pointer_icon_style_reddot; ++iconid) { pointericon pointericon; loadsystemiconasspritewithpointericon( env, mcontextobj, iconid, &pointericon, &((*outresources)[iconid])); if (!pointericon.bitmapframes.empty()) { pointeranimation& animationdata = (*outanimationresources)[iconid]; size_t numframes = pointericon.bitmapframes.size() + 1; animationdata.durationperframe = milliseconds_to_nanoseconds(pointericon.durationperframe); animationdata.animationframes.reserve(numframes); animationdata.animationframes.push_back(spriteicon( pointericon.bitmap, pointericon.hotspotx, pointericon.hotspoty)); for (size_t i = 0; i < numframes - 1; ++i) { animationdata.animationframes.push_back(spriteicon( pointericon.bitmapframes[i], pointericon.hotspotx, pointericon.hotspoty)); } } } loadsystemiconassprite(env, mcontextobj, pointer_icon_style_null, &((*outresources)[pointer_icon_style_null])); }
(4)按键切换鼠标样式
此知识点大家可以参阅其它相关文章:android按钮美化样式的实现代码