中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

Android系統(tǒng)添加自定義鼠標(biāo)樣式通過(guò)按鍵切換實(shí)例詳解

一、APP通過(guò)View修改鼠標(biāo)樣式

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供郊區(qū)企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為郊區(qū)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。

app view上修改鼠標(biāo)樣式比較簡(jiǎn)單,通過(guò) hover event 獲取鼠標(biāo)坐標(biāo)并使用如下方法修改為自定義圖片:

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: //鼠標(biāo)進(jìn)入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: //鼠標(biāo)在view上
            Log.i(TAG, "bottom ACTION_HOVER_MOVE...");
            break;
          case MotionEvent.ACTION_HOVER_EXIT: //鼠標(biāo)離開(kāi)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" />
但是app修改鼠標(biāo)樣式的view關(guān)閉后,鼠標(biāo)樣式會(huì)恢復(fù)成默認(rèn)的黑箭頭,因此不依賴APP去動(dòng)態(tài)切換鼠標(biāo)樣式需要在framework層修改系統(tǒng)源碼實(shí)現(xiàn)。

二、framework層添加自定義鼠標(biāo)樣式并通過(guò)按鍵切換

(1)添加自定義樣式資源

系統(tǒng)圖標(biāo)資源在 frameworks/base/core/res/res/drawable-mdpi/ 目錄,其中 pointer_arrow.png、pointer_arrow_large.png 是系統(tǒng)默認(rèn)的黑色箭頭,

pointer_arrow_red_dot.png、pointer_arrow_red_dot_large.png 是自己添加的紅點(diǎn)樣式圖片:

Android系統(tǒng)添加自定義鼠標(biāo)樣式通過(guò)按鍵切換實(shí)例詳解

然后在 frameworks/base/core/res/res/drawable/ 目錄添加對(duì)應(yīng)的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 添加資源配置,注意名字的匹配!

Android系統(tǒng)添加自定義鼠標(biāo)樣式通過(guò)按鍵切換實(shí)例詳解

 

Android系統(tǒng)添加自定義鼠標(biāo)樣式通過(guò)按鍵切換實(shí)例詳解

修改 frameworks/base/core/res/res/values/attrs.xml  引用資源:

Android系統(tǒng)添加自定義鼠標(biāo)樣式通過(guò)按鍵切換實(shí)例詳解

(2)Java 層獲取資源

修改 frameworks/base/core/java/android/view/PointerIcon.java ,添加如下定義:

Android系統(tǒng)添加自定義鼠標(biāo)樣式通過(guò)按鍵切換實(shí)例詳解

在 getSystemIconTypeIndex(int type) 函數(shù)中返回之前配置的資源:

Android系統(tǒng)添加自定義鼠標(biāo)樣式通過(guò)按鍵切換實(shí)例詳解

(3)c++層添加對(duì)應(yīng)的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 中的變量對(duì)應(yīng)
};

修改 frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp ,加載到自定義枚舉變量對(duì)應(yīng)的圖片資源:

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)按鍵切換鼠標(biāo)樣式

此知識(shí)點(diǎn)大家可以參閱創(chuàng)新互聯(lián)其它相關(guān)文章:Android按鈕美化樣式的實(shí)現(xiàn)代碼

標(biāo)題名稱:Android系統(tǒng)添加自定義鼠標(biāo)樣式通過(guò)按鍵切換實(shí)例詳解
分享URL:http://www.rwnh.cn/article22/igicjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管動(dòng)態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)軟件開(kāi)發(fā)、網(wǎng)站導(dǎo)航用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
哈密市| 南宫市| 准格尔旗| 永济市| 马鞍山市| 治多县| 大埔区| 汝阳县| 孝感市| 吐鲁番市| 大埔县| 清流县| 阿合奇县| 哈尔滨市| 聂拉木县| 五莲县| 汶川县| 伊川县| 怀宁县| 巫山县| 文昌市| 新干县| 墨竹工卡县| 徐州市| 嵊州市| 新乐市| 大渡口区| 靖宇县| 嵩明县| 怀来县| 额尔古纳市| 彰化县| 彭州市| 娄底市| 潮州市| 综艺| 黔西| 临潭县| 呼玛县| 永靖县| 凤阳县|