2

我有这门课: 我的问题:

public class Example extends Activity implements OnClickListener{
    DrawView view1;
    Canvas canvas = new Canvas();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        view1 = new DrawView(this);
        setContentView(view1);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            view1.setFirstCoord(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            view1.setSecondCoord(x, y);
            view1.dispatchDraw(canvas);
            break;
        }
    return true;
    }

    public class DrawView extends LinearLayout {
        private Paint paint = new Paint();
        public int x1, x2;
        public int y1, y2;

        public DrawView(Context c){
            super(c);           
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            addView(inflater.inflate(R.layout.union, null));

            x1 = 0;
            x2 = 0;
            y1 = 0;
            y2 = 0;

            paint.setColor(Color.BLACK);
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(10);        
        }

        @Override
        public void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
            canvas.drawLine(0, 50, 900,2000 , paint);//WORKS
            canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);//NO WORKS
        }

        public void setFirstCoord(int e, int f){
            x1 = e;
            y1 = f;
        }

        public void setSecondCoord(int e, int f){
            x2 = e;
            y2 = f;
        }
    }
}

执行下一行时,我在屏幕上看到“示例”行:

canvas.drawLine(0, 50, 900,2000 , paint);

但是当下面的行“被执行”时没有画出任何直线。为什么???

canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);

我也尝试过:

canvas.drawLine(x1, y1, x2, y2, paint);

但是,显然,结果是一样的。

我也尝试过执行 onDraw() 方法,但任何线都被绘制,因为 onDraw 在“UI”下绘制(在布局 .xml 文件的内容下)

我希望找到可以帮助我的人。我认为解决方案很容易,但我会在没有找到解决方案的情况下疯狂尝试。

谢谢!!!

解决方案:

public class Example extends Activity implements OnClickListener{
    DrawView view1;
    Canvas canvas = new Canvas();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        view1 = new DrawView(this);
        setContentView(view1);
        view1.setOnClickListener(this);
    }

    public class DrawView extends LinearLayout {
        private Paint paint = new Paint();
        public int x1, x2;
        public int y1, y2;

        public DrawView(Context c){
            super(c);           
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            addView(inflater.inflate(R.layout.union, null));

            x1 = 0;
            x2 = 0;
            y1 = 0;
            y2 = 0;

            paint.setColor(Color.BLACK);
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(10);        
        }

        public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            view1.setFirstCoord(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            view1.setSecondCoord(x, y);
            view1.onDraw(canvas);
            view1.postInvalidate();
            break;
        }
        return true;
        }

        public void setFirstCoord(int e, int f){
            x1 = e;
            y1 = f;
        }

        public void setSecondCoord(int e, int f){
            x2 = e;
            y2 = f;
        }

        public void onDraw(Canvas canvas){
        canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);
    }
    }
}

再次感谢您的帮助和关注。我的荣幸!!!

4

4 回答 4

1

如果要为此使用 OnClickListener 接口,则需要将活动设置OnClickListener为视图:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    view1 = new DrawView(this);
    setContentView(view1);

    // Add this:
    view1.setOnClickListener(this);
}

否则,onTouchEvent永远不会被调用。

另外,onTouchEvent应该命名为onClick.

于 2012-10-22T20:19:13.547 回答
1

不要将您的绘图代码放在 dispatch draw 中,而是尝试将其放在 onDraw 中。

然后修改你的 onTouch 方法,如下所示:

@Override
public boolean onTouchEvent(MotionEvent event) {
  int x = (int) event.getX();
  int y = (int) event.getY();
  switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    view1.setFirstCoord(x, y);
    break;
    case MotionEvent.ACTION_MOVE:
    break;
    case MotionEvent.ACTION_UP:
    view1.setSecondCoord(x, y);
    view1.postInvalidate();
    break;
  }
   return true;
}
于 2012-10-22T20:21:46.780 回答
1
  • 将 OnTouchEvent 移动到 DrawView,而不是活动
  • 使 DrawView 扩展 View,而不是 LinearLayout
  • 代替 dispatchDraw,实现方法 onDraw 并在 onTouchEvent 中调用 invalidate()
于 2012-10-22T20:26:01.723 回答
0

我认为您的问题可能是您在活动中检索的 x/y 坐标在屏幕空间中,而您在画布上的绘制坐标在画布空间中。

因此,您活动中的 (0,0) 与画布的 (0,0) 位置不同。

您可能应该在视图中而不是在活动中接收触摸事件,然后触摸事件和画布绘制的坐标系将相同。

于 2012-10-22T20:26:40.020 回答