2016-12-05 58 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