隐藏Android系统的鼠标

公司最近有自研云桌面相关的产品,不过Server端使用的操作系统不是Linux也不是Windows,而是Android系统,有些类似于华为的云手机,Client端登录服务器以后要操作Server端,而且产品要求操作Server端是以触摸的方式像操作本地的手机一样,但默认情况下Android系统都会有个鼠标的图标,这样很影响体验,于是就有了个这个隐藏鼠标的需求,下面记录下修改的过程:

安卓系统中有两个系统APK,负责控制和显示安卓系统的界面元素

一个是framework-res.apk(源码 frameworks/base/core/res/); 另外一个是SystemUI.apk( 源码 frameworks/base/packages/SystemUI/) 前者主要负责保存安卓系统用到的资源文件,比如图片/XML 文件等,而后者负责何时显示和隐藏这些资源/图标等;

于是从鼠标的图标入手,在res文件夹下的drawable-mdpi子目录中发现有 pointer_arrow.png 这张图片  正是Android系统默认的图标;

在安卓源码中搜索用到该图片的相关代码

可以看到图片是被 一个XML文件所引用 文件名为pointer_arrow_icon.xml,那么进一步搜索 pointer_arrow_icon

可以看到主要使用到这个XML文件的JAVA类是 base/core/java/android/view/PointerIcon.java 基本锁定要修改的文件就是这个了

进入到PointerIcon类中详细分析,该类中主要的方法有getNullIcon() 获取空图标 ;getDefaultIcon()获取默认图标;getSystemIcon()获取系统图标等,认真读几遍代码,不难发现获取鼠标的方法最终都会调用到getSystemIcon()这个方法里

/**
     * Gets a system pointer icon for the given type.
     * If typeis not recognized, returns the default pointer icon.
     *
     * @param context The context.
     * @param type The pointer icon type.
     * @return The pointer icon.
     *
     * @throws IllegalArgumentException if context is null.
     */
    public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
        if (context == null) {
            throw new IllegalArgumentException("context must not be null");
        }			
        
        if (type == TYPE_NULL) {
            return gNullIcon;
        }

        PointerIcon icon = gSystemIcons.get(type);
        if (icon != null) {
            return icon;
        }

        int typeIndex = getSystemIconTypeIndex(type);
        if (typeIndex == 0) {
            typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
        }

        int defStyle = sUseLargeIcons ?
                com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
        TypedArray a = context.obtainStyledAttributes(null,
                com.android.internal.R.styleable.Pointer,
                0, defStyle);
        int resourceId = a.getResourceId(typeIndex, -1);
        a.recycle();

        if (resourceId == -1) {
            Log.w(TAG, "Missing theme resources for pointer icon type " + type);
            return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT);
        }

        icon = new PointerIcon(type);
        if ((resourceId & 0xff000000) == 0x01000000) {
            icon.mSystemIconResourceId = resourceId;
        } else {
            icon.loadResource(context, context.getResources(), resourceId);
        }
        gSystemIcons.append(type, icon);
        return icon;
        
    }

为了隐藏鼠标的图标,那么我们这里每次都返回一个空的图标即可,最终修改也很简单,附上修改后的代码

/**
     * Gets a system pointer icon for the given type.
     * If typeis not recognized, returns the default pointer icon.
     *
     * @param context The context.
     * @param type The pointer icon type.
     * @return The pointer icon.
     *
     * @throws IllegalArgumentException if context is null.
     */
    public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
        if (context == null) {
            throw new IllegalArgumentException("context must not be null");
        }

		//主要是这里,始终返回空图标,并且不执行后面的代码
		return gNullIcon;

        /**
        if (type == TYPE_NULL) {
            return gNullIcon;
        }

        PointerIcon icon = gSystemIcons.get(type);
        if (icon != null) {
            return icon;
        }

        int typeIndex = getSystemIconTypeIndex(type);
        if (typeIndex == 0) {
            typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
        }

        int defStyle = sUseLargeIcons ?
                com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
        TypedArray a = context.obtainStyledAttributes(null,
                com.android.internal.R.styleable.Pointer,
                0, defStyle);
        int resourceId = a.getResourceId(typeIndex, -1);
        a.recycle();

        if (resourceId == -1) {
            Log.w(TAG, "Missing theme resources for pointer icon type " + type);
            return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT);
        }

        icon = new PointerIcon(type);
        if ((resourceId & 0xff000000) == 0x01000000) {
            icon.mSystemIconResourceId = resourceId;
        } else {
            icon.loadResource(context, context.getResources(), resourceId);
        }
        gSystemIcons.append(type, icon);
        return icon;**/
        
    }

编译整个Android源码后再在手机上验证,亲测有效!希望能帮到用到类似功能的网友们~

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值