2012-12-13 90 views
9

我打算在自己的視圖上繪製(R.id.view),但此代碼似乎沒有任何效果。它不允許我畫任何東西。Android - 如何在視圖上繪製

public class MainActivity extends Activity implements OnTouchListener 
     { 
Path mPath; 
Canvas canvas; 
Paint mPaint; 
MaskFilter mEmboss; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     View view=(View)findViewById(R.id.view1); 
     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(0xFFFF0000); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(12); 

     canvas = null; 
     //view.draw(canvas); 
     mPath = new Path(); 
     Bitmap mBitmap; 
     //Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
     mBitmap = Bitmap.createBitmap(210, 170, Bitmap.Config.ARGB_8888); 
     canvas = new Canvas(mBitmap); 
     canvas.drawColor(0xFFAAAAAA); 

     canvas.drawBitmap(mBitmap, 0, 0, mPaint); 


     //canvas.drawPaint(mPaint); 
     view.draw(canvas); 
     canvas.drawPath(mPath, mPaint); 


     mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 
             0.4f, 6, 3.5f); 



     view.setOnTouchListener(new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       float x = event.getX(); 
       float y = event.getY(); 
       Log.d("x", String.valueOf(x)); 
       Log.d("y", String.valueOf(y)); 

       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         Log.d("a","down"); 
         touch_start(x, y); 
         //invalidate(); 
         break; 
        case MotionEvent.ACTION_MOVE: 
         touch_move(x, y); 
         // invalidate(); 
         break; 
        case MotionEvent.ACTION_UP: 
         touch_up(); 
         // invalidate(); 
         break; 
       } 
       return true; 
      } 
     }); 

     // mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); 
    } 
    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4; 

    private void touch_start(float x, float y) { 
     mPath.reset(); 
     mPath.moveTo(x, y); 
     mX = x; 
     mY = y; 
    } 
    private void touch_move(float x, float y) { 
     float dx = Math.abs(x - mX); 
     float dy = Math.abs(y - mY); 
     if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
      mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
      mX = x; 
      mY = y; 
     } 
    } 
    private void touch_up() { 
     mPath.lineTo(mX, mY); 
     // commit the path to our offscreen 
     canvas.drawPath(mPath, mPaint); 
     // kill this so we don't double draw 
     mPath.reset(); 
    } 





} 

我該如何在自己的視野上使用freehanf指紋漆?

+0

**請參閱我的答案[點擊這裏](http://stackoverflow.com/a/41135685/4629101)** –

回答

6

如果您創建了一個擴展視圖的類,並將其設置爲內容視圖,那麼這樣會更好。 Action_Move參數在touch事件中被調用了很多次,所以最好注意這一點,否則當你想要一條曲線時你會得到一條直線。

編輯
你會開始在OnCreate通過改變
View view = new MyView();
setContentView(view);

內,您的MyView的類,你將不得不 '路徑' 對象的ArrayList,每次Action_UP被調用,將路徑添加到arrayList,使用view.Invalidate();,然後該類將調用視圖類中的onDraw方法。所以你重寫它就像這樣。

@Override public void onDraw(Canvas c){
for(Path p: listOfPaths){
c.drawPath(p, paint);}
}

注:這將是很好的簡單的畫圖,不要指望畫蒙娜麗莎在平板電腦上或手機上的這種方法,因爲一旦有許多行你可能會注意到一個滯後。如果你想變得更復雜和專業。看看它的view.Invalidate(Rect r)的方法,它不會使整個視圖無效,但只有你剛畫的部分

+0

任何擴展視圖的類都會有一個onDraw方法。只需在Action_Up期間調用invalidate,它就會調用onDraw方法。重新繪製更新的視圖。希望這有助於。 – dannyRods

+0

我們能否獲得更多信息?你如何在他的代碼中做到這一點? –

+1

希望清除它 – dannyRods