取自这篇文章:
How to programmatically enable "show touches" option in Android?
• 启用显示触摸:
Settings.System.putInt(context.getContentResolver(),
"show_touches", 1);
• 禁用显示触摸:
Settings.System.putInt(context.getContentResolver(),
"show_touches", 0);
记得将 android.permission.WRITE_SETTINGS 添加到您的 Manifest。
编辑:
这里是一个DrawView.java,它实现了 View.OnTouchListener 并定义了多种绘制模式。但是它缺乏指标。
在 DrawView 的 setListener 内的主要活动中,您覆盖 onStartDrawing 方法:在这里您可以决定是否显示指示器以及如何根据 DrawView 的当前模式(笔、橡皮擦等)和其他界面方法设置样式指示器是否可见并在绘图结束时将其隐藏。检查代码:
mDrawView.setOnDrawViewListener(new DrawView.OnDrawViewListener() {
@Override public void onStartDrawing() {
canUndoRedo();
// decide whether or not to show an
// indicator and style it depending on
// mDrawView's attributes and current drawing mode
setIndicator(mDrawView.getDrawingMode(),mDrawView.getDrawWidth());
}
@Override
public void onMove(MotionEvent motionEvent) {
if(isIndicatorVisible){
indicatorView.animate()
.x(motionEvent.getX())
.y(motionEvent.getY())
.setDuration(0)
.start();
}
}
@Override public void onEndDrawing() {
canUndoRedo();
//hide indicator here
if (isIndicatorVisible){
indicatorView.setVisibility(View.GONE);
isIndicatorVisible = false;
}
}
这是我添加到 DrawView.java 的编辑:
定义了新的接口方法void onMove(MotionEvent motionEvent)
public interface OnDrawViewListener {
void onStartDrawing();
void onEndDrawing();
void onClearDrawing();
void onRequestText();
void onMove(MotionEvent motionEvent); // added this method
}
在 onToch 内部调用新方法:
case MotionEvent.ACTION_MOVE:
onDrawViewListener.onMove(motionEvent); // added this line
mDrawMoveHistory.get(mDrawMoveHistory.size() - 1).setEndX(motionEvent.getX()).setEndY(motionEvent.getY());
if (mDrawingTool == DrawingTool.PEN || mDrawingMode == DrawingMode.ERASER) {
mDrawMoveHistory.get(mDrawMoveHistory.size() - 1).getDrawingPathList()
.get(mDrawMoveHistory.get(mDrawMoveHistory.size() - 1).getDrawingPathList().size() - 1)
.lineTo(motionEvent.getX(), motionEvent.getY());
}
invalidate();
break;
这里是 setIndicator 方法:
private void setIndicator(DrawingMode drawingMode, int drawWidth) {
if (drawingMode == DrawingMode.ERASER){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(drawWidth, drawWidth);
indicatorView.setLayoutParams(params);
GradientDrawable border = new GradientDrawable();
border.setStroke(1, 0xFF000000); //black border with full opacity
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
indicatorView.setBackgroundDrawable(border);
} else {
indicatorView.setBackground(border);
}
indicatorView.setVisibility(View.VISIBLE);
isIndicatorVisible = true;
}
}