如何拖动和画线?

时间:2018-05-14 08:31:29

标签: android motionevent

我是Android编程的新手,我创建了一个应用程序来绘制矩形和圆形。现在我想画椭圆和线条。我知道drawLine采用起点x,起点y,终点x和终点y。

这是我在mycustomView.java中尝试过但我只能将行从左上角拖到右下角和右下角到左上角?如何让它适用于所有方向?

 protected void onDraw(Canvas canvas) {
    this.canvas = canvas;

    canvas.drawColor(Color.WHITE);

    for (int i = 0; i < numberShapes; i++) {
        paint.setColor(Color.parseColor("#" + Integer.toHexString(Integer.parseInt(shapes[i].getColor())).substring(2))); //avoids alpha
        paint.setStrokeWidth(shapes[i].getBorder());
         //some code here....

                    else if(shapes[i].getShapeType().equals("Ellipse")){
            RectF rect= new RectF(shapes[i].getX(),shapes[i].getY(),shapes[i].getX() + shapes[i].getWidth(),shapes[i].getY() + shapes[i].getHeight());
            canvas.drawOval(rect,paint);

        }
        else if(shapes[i].getShapeType().equals("StraightLine")){
           canvas.drawLine(shapes[i].getX(),shapes[i].getY(),shapes[i].getX() + shapes[i].getWidth(),shapes[i].getY() + shapes[i].getWidth(),paint);
        }
    }

}
}

这是我的viewShapes.java与我的动作事件

public boolean onTouch(View v, MotionEvent ev) {
            //Circle is the default shape if can't find the key
            String selectedShapeDrawing = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE).getString("selectedShapeDrawing", "Circle");
            int x = (int) ev.getX(); int y = (int) ev.getY();
            int dX, dY;

            int action = MotionEventCompat.getActionMasked(ev);
            switch (action){
                case MotionEvent.ACTION_DOWN:
                    mLastTouchX = x; mLastTouchY = y;
                    break;
                case MotionEvent.ACTION_MOVE:


                        break;
                case MotionEvent.ACTION_UP:
                    if ((selectedShapeDrawing.equals("Rectangle")) || selectedShapeDrawing.equals("Circle") || selectedShapeDrawing.equals("Ellipse")) {
                        //some code here...                        }
                   if(selectedShapeDrawing.equals("StraightLine")){
                        if((x>mLastTouchX) && (y>mLastTouchY)) {

                            storeShape(selectedShapeDrawing, mLastTouchX, mLastTouchY, x, y);
                        }

                        else if((x<mLastTouchX) && (y<mLastTouchY)) {

                            storeShape(selectedShapeDrawing, x, y, mLastTouchX, mLastTouchY);
                        }
                        else if((x>mLastTouchX) && (y<mLastTouchY)) {

                            storeShape(selectedShapeDrawing,mLastTouchX, y,x,mLastTouchY);
                        }

                        else if((x<mLastTouchX) && (y>mLastTouchY)) {

                            storeShape(selectedShapeDrawing, x,mLastTouchY,mLastTouchX, y);
                        }

任何帮助将不胜感激!

编辑:这是我的店铺形状

 private void storeShape(String shape, int x, int y, int deltaX, int deltaY) 
{
    int selectedColor = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE).getInt("selectColor", 0);

    ContentValues contentValues = new ContentValues();
    contentValues.put(SchemeShapes.Shape.SHAPE_TYPE, shape);
    contentValues.put(SchemeShapes.Shape.SHAPE_X, x);
    contentValues.put(SchemeShapes.Shape.SHAPE_Y, y);

    contentValues.put(SchemeShapes.Shape.SHAPE_RADIUS, Math.max(deltaX, deltaY));



    contentValues.put(SchemeShapes.Shape.SHAPE_WIDTH, deltaX);
    contentValues.put(SchemeShapes.Shape.SHAPE_HEIGHT, deltaY);
    contentValues.put(SchemeShapes.Shape.SHAPE_BORDER_THICKNESS, 10);
    contentValues.put(SchemeShapes.Shape.SHAPE_COLOR, selectedColor);

    resolver.insert(SchemeShapes.Shape.CONTENT_URI, contentValues);
}

2 个答案:

答案 0 :(得分:1)

好吧,我在画布上过度复杂了这一点。我想做的就是这样做:

else if(shapes[i].getShapeType().equals("StraightLine")){
    canvas.drawLine(shapes[i].getX(), shapes[i].getY(), shapes[i].getWidth(), shapes[i].getHeight(), paint);
}

在我的Action_Up中,我应该这样做:

if ((selectedShapeDrawing.equals("StraightLine"))) {
    storeShape(selectedShapeDrawing,mLastTouchX,mLastTouchY,x,y); 
}

答案 1 :(得分:0)

您可以尝试以下方法:

public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getRawX();
                startY = event.getRawY();
                break;

            case MotionEvent.ACTION_UP:
                Paint paint = new Paint(); 
                paint.setStrokeWidth(10f);
                paint.setColor(getResources().getColor(R.color.text_red));
                canvas.drawLine(startX, startY, event.getRawX(), event.getRawY(), paint);
                break;
        }
    }