当使用现代版本的Android时- Honeycomb或更高版本-如果硬件合适,支持显示鼠标指针。例如,在华硕Transformer或东芝AC100笔记本上。
是否有API允许在这些设备上运行的应用程序以编程方式更改其鼠标指针?(或者在应用程序窗口中时完全隐藏指针。)
发布于 2019-04-03 04:06:30
此功能是在Android 7.0中添加的。您可以选择一个预设的系统指针,也可以从位图中创建一个。您还可以隐藏指针。
Android7.0文档:https://developer.android.com/about/versions/nougat/android-7.0#custom_pointer_api
PointerIcon类:https://developer.android.com/reference/android/view/PointerIcon.html
我用它来定制WebView的指针。你需要创建一个类来扩展你想要改变指针的视图。
如果使用位图可绘制文件,则应将其放在适当的密度文件夹中( drawable -mdpi。drawable-xxxhdpi。)如果你不这样做,系统会自动缩放它,它看起来真的很模糊。系统默认指针似乎在18dp左右。
位图示例:
package com.example.packageName;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.webkit.WebView;
public class CustomWebview extends WebView {
private Bitmap bmCursor;
private PointerIcon pntCursor;
public CustomWebview(Context context, AttributeSet attrs) {
super(context, attrs);
if (Build.VERSION.SDK_INT >= 24) {
bmCursor = BitmapFactory.decodeResource(getResources(), R.drawable.cursor);
pntCursor = PointerIcon.create(bmCursor,0,0);
}
}
@TargetApi(24)
@Override
public PointerIcon onResolvePointerIcon(MotionEvent me, int pointerIndex) {
return pntCursor;
}
}系统指针示例:
package com.example.packageName;
import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.webkit.WebView;
public class CustomWebview extends WebView {
Context c;
public CustomWebview(Context context, AttributeSet attrs) {
super(context, attrs);
c = context;
}
@TargetApi(24)
@Override
public PointerIcon onResolvePointerIcon(MotionEvent me, int pointerIndex) {
return PointerIcon.getSystemIcon(c, PointerIcon.TYPE_CROSSHAIR);
}
}使用PointerIcon.TYPE_NULL将隐藏光标。
因为我对WebView使用了自己的类,所以我不得不在布局的xml中将它的标记重命名为com.example.packageName.CustomWebview。就像这样。
<?xml version="1.0" encoding="utf-8"?>
<com.example.packageName.CustomWebview xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0px"
android:layout_margin="0px"
android:scrollbars="none"
android:nestedScrollingEnabled="false"
android:background="@drawable/webview_style"
android:foreground="@drawable/webview_style"
android:id="@+id/webGame" />还有一个view.setPointerIcon(PointerIcon)方法,但它似乎不会永久地更改视图的指针。
https://stackoverflow.com/questions/15636855
复制相似问题