2016-12-05 57 views
0

我开发简单的绘图应用程序,我想知道是否有可能触及的画布时添加可见的指针?我的意思是类似于PC上的鼠标指示器。其原因是 - 当使用橡皮擦它是不可见哪儿你触摸注册,并与指针,这将有助于(用手指触摸尤其是当,因为笔是相当精确)。我尝试了其他类似的主题,但没有人回答这个问题。如何将触摸指针添加到应用程序?

+0

答案是肯定的。 – GurV

+0

标题问题的答案如何? – DiTi

+0

你说“我试过了”。告诉我们。 – GurV

回答

0

从这个POS采取: 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添加到您的表现。

编辑:

这里是一个DrawView.java实现View.OnTouchListener并限定多个拉伸模式。但它缺乏指标。

在setListener的drawLayout中,您可以重写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的编辑:

定义新接口方法空隙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; 
     } 
    } 
+0

我已经看到了这个帖子前面,这并不完全是我所需要的,而且是完全肯定,我用我的代码试了一下。它只是作为一种解决方法,因为我得到了我触摸的点的轮廓。我要求的是一个指针,它可以完全按照它的大小绘制/擦除(绘图/擦除已经被覆盖)。我只希望它在画布上,因为在工具箱或其他任何地方都没用。 – DiTi

+0

另一个问题是,这似乎只适用于API <23。后来的版本不允许更改私人设置,因此问题保持打开状态。 – DiTi

+0

我编辑答案..代码是在此基础上github上的项目:https://github.com/ByoxCode/DrawView ..我想我所做的更改,它的工作很好..好运 – msdev16