在Android中查看自由手

时间:2014-01-09 10:39:47

标签: android canvas view

路径不正确。

活动:

<?xml version="1.0" encoding="utf-8"?>
<view xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.example.freehandpaint.DragObserverLayout" >

 </view>

MainActivity:

public class MainActivity extends Activity{
DragObserverLayout dlayout;
Canvas canvas = new Canvas();
Paint paint = new Paint();
private Path path;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //path = new Path();
    dlayout = (DragObserverLayout) findViewById(R.id.dlayout);
    dlayout.setBackgroundColor(Color.parseColor("#F5F5DC"));

    dlayout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub


             if(event.getAction() == MotionEvent.ACTION_DOWN){
                    path = new Path();
                    path.moveTo(event.getX(), event.getY());
                    //path.lineTo(event.getX(), event.getY());
                }else if(event.getAction() == MotionEvent.ACTION_MOVE){

                    path.lineTo(event.getX(), event.getY());
                    dlayout.AddPath(path);

                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    path.lineTo(event.getX(), event.getY());
                    dlayout.AddPath(path);
                }


            return true;
        }
    });
}



}

DragObserverLayout:

public class DragObserverLayout extends RelativeLayout {

float startX, startY, stopX, stopY;
private Paint mPaint = new Paint();
private List<Rect> lines = new ArrayList<Rect>();
private ArrayList<Path> linePath = new ArrayList<Path>();
public DragObserverLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    mPaint.setColor(Color.BLUE);
    mPaint.setStrokeCap(Paint.Cap.BUTT);
    mPaint.setStrokeWidth(5.0f);
}


@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    for (Path path : linePath) {
        //canvas.drawPoint(graphic.x, graphic.y, mPaint);
        canvas.drawPath(path, mPaint);
    }
}

public void AddPath(Path p){
    linePath.add(p);
    invalidate();
}

public void addLine(Rect r) {
    lines.add(r);
    invalidate();
}

}

我画了一条从一直到底部的线,但是没有绘制正确的线

enter image description here

1 个答案:

答案 0 :(得分:0)

在xml中写

<LinearLayout
       android:layout_height="45dp" 
       android:orientation="horizontal"
       android:layout_width="fill_parent"
        android:id="@+id/signaturearea" 
        android:layout_marginTop="320dp"
         android:layout_marginRight="20dp" 
         android:layout_marginLeft="20dp" 
         android:background="#FFFFFF" /> 

现在创建一个内部类

public class signature extends View {
        private static final float STROKE_WIDTH = 5f;
        private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
        private Paint paint = new Paint();
        private Path path = new Path();
        private float lastTouchX;
        private float lastTouchY;
        private final RectF dirtyRect = new RectF();


        public signature(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
        }


        @Override
        protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        path.moveTo(eventX, eventY);
        lastTouchX = eventX;
        lastTouchY = eventY;
        return true;
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
        resetDirtyRect(eventX, eventY);
        int historySize = event.getHistorySize();
        for (int i = 0; i < historySize; i++) {
        float historicalX = event.getHistoricalX(i);
        float historicalY = event.getHistoricalY(i);
        expandDirtyRect(historicalX, historicalY);
        path.lineTo(historicalX, historicalY);
        }
        path.lineTo(eventX, eventY);
        break;
        default:
        debug("Ignored touch event: " + event.toString());
        return false;
        }
        invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
        (int) (dirtyRect.top - HALF_STROKE_WIDTH),
        (int) (dirtyRect.right + HALF_STROKE_WIDTH),
        (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
        lastTouchX = eventX;
        lastTouchY = eventY;
        return true;
        }

        private void debug(String string) {
        }

        private void expandDirtyRect(float historicalX, float historicalY) {
        if (historicalX < dirtyRect.left) {
        dirtyRect.left = historicalX;
        } else if (historicalX > dirtyRect.right) {
        dirtyRect.right = historicalX;
        }
        if (historicalY < dirtyRect.top) {
        dirtyRect.top = historicalY;
        } else if (historicalY > dirtyRect.bottom) {
        dirtyRect.bottom = historicalY;
        }
        }
        private void resetDirtyRect(float eventX, float eventY) {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
        }
    }

在活动的onCreate

//create the inner class object
signature mSignature =new signature(this, null);
LinearLayout mContent;
mContent = (LinearLayout) findViewById(R.id.signaturearea);
mContent.addView(mSignature, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);