【问题标题】:How to draw on canvas when touched触摸时如何在画布上绘图
【发布时间】:2016-07-12 07:00:58
【问题描述】:

我无法在 ontouch() 中获取画布对象。没有画布,我无法在触摸时画一个圆圈。如何在触摸时绘制任何形状或图像

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;
}

【问题讨论】:

  • Canvas 不是本地或全局对象,因此您无法从onTouch() 事件中访问它。在此方法上创建 Canvas 实例或使用 Global。
  • Draw Circle on touch的可能重复

标签: android android-canvas draw touch-event


【解决方案1】:

要在画布上绘制您触摸的任何位置,您需要一个路径来跟踪您的触摸点和路径。使用路径对象,您可以在画布上绘制。看到这个答案Draw on touch

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2022-11-02
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多