2016-07-12 59 views
0

當我無法獲得ontouch(Canvas對象)在畫布上繪製,沒有畫布,我不能畫一個圓,當touched.How觸摸時可我畫任何形狀或圖像如何感動

public class Board extends View implements View.OnTouchListener { 

public Board(Context context) { 
    super(context); 
    Paint paint1 = new Paint(); 
    paint1.setTextSize(50); 
    paint1.setColor(Color.WHITE); 

    View view=this; 
    view.setOnTouchListener(this);  
} 

@Override 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.drawRGB(200, 100, 0); 

} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    final int action = MotionEventCompat.getActionMasked(event); 
    int pointer = MotionEventCompat.getActionIndex(event); 


    if (action == MotionEvent.ACTION_DOWN) { 

     canvas.drawCircle(70, 1100, 50, paint1); 

    } 

    return false; 
} 
+0

'Canvas'不是一個本地或全局對象,所以你不能從'onTouch()'事件訪問它。可以在此方法上創建Canvas實例或使用Global。 – Riad

+0

[Draw Circle on touch]的可能重複(http://stackoverflow.com/questions/11796357/draw-circle-on-touch) – 0X0nosugar

回答

0

要在您觸摸的任何地方繪製畫布,您需要一條路徑來跟蹤您的觸摸點和路徑。使用可以在畫布上繪製的路徑對象。看到這個答案Draw on touch