2013-04-10 3 views
0

좋아, 나는 linedrawview를 사용하는 프로그램을 만들려고 노력하고있다. 사용자가 터치 이벤트 (동작 DOWN)를 시작하면 현재 x 및 y를 가져 와서 변수에 저장합니다. 그런 다음 사용자가 손가락을 드래그하면 선이 그려지고 고무 밴드 방식으로 움직입니다. 마지막으로 사용자가 이동 (작업 UP)을하면 줄이 만들어집니다. 나는 이것에 많은 문제가있어 도움을 원합니다. 지금까지 LineDrawView.java에 대한 나의 코드 :ontouchevent로 고무 밴드 선 그리기

// Project:   Java2LineDrawEx 
    // File:   LineDrawView.java 
    // Date:   4/9/13 
    // Author:   Joshua Lefelhocz 
    // Description:  custom view to draw lines on 

    package com.lcc.java2lab11lefelhocz; 

    import android.content.Context; 
    import android.graphics.Canvas; 
    import android.graphics.Color; 
    import android.graphics.Paint; 
    import android.view.MotionEvent; 
    import android.view.View; 

    // Notice this class extends View 
    public class LineDrawView extends View 
    { 
// This view's bounds 

private int xMin = 0;   
private int xMax; 
private int yMin = 0; 
private int yMax; 
private float currentX; 
private float currentY; 

// Paint object 
private Paint paintFill; 

// constructor 
public LineDrawView(Context context) 
{ 
    // call the super class constructor 
    super(context); 

    // The Paint class holds the style and color information about how to draw geometries, text and bitmaps. 
    // For efficiency create the paint objects in the constructor, not in draw 
    // paint.setStrokeWidth(10); // works on lines 
    // You can change the color of Paint without effecting objects already drawn 
    // You can NOT change the style of Paint without effecting objects already drawn 
    // The Style, TextSize apply to all objects drawn with the paint. 

    // Create a default Paint object Style=Fill 
    paintFill = new Paint(); 

    // set the background color when the view is created 
    this.setBackgroundColor(Color.LTGRAY); 
} 

// Called to draw the view. Also called by invalidate(). 
@Override 
protected void onDraw(Canvas canvas) 
{ 
    //  canvas.drawColor(Color.LTGRAY); // this works in onDraw to set the background color 

    // Draw a Red Diagonal line from upper left corner to bottom right corner 
    paintFill.setColor(Color.RED); 
    canvas.drawLine(xMin, yMin, xMax, yMax, paintFill); 

    // draw a blue line 10 pixels wide horizontal across the center. 
    paintFill.setColor(Color.BLUE); 
    paintFill.setStrokeWidth(10); 
    canvas.drawLine(xMin, yMax/2, xMax, yMax/2, paintFill); 

    // draw a yellow line 20 pixels wide vertical across the center. 
    paintFill.setColor(Color.YELLOW); 
    paintFill.setStrokeWidth(20); 
    canvas.drawLine(xMax/2, yMin, xMax/2, yMax, paintFill); 
} 

// Called when the view is first created or its size changes. 
@Override 
public void onSizeChanged(int width, int height, int oldWidth, int oldHeight) 
{ 
    // Set the view bounds 
    xMax = width-1; 
    yMax = height-1; 
} 

public boolean onTouchEvent(MotionEvent event) 
{ 
    currentX = event.getX(); 
    currentY = event.getY(); 



    switch(event.getAction()) 
    { 
    case MotionEvent.ACTION_DOWN: 
     float startX = currentX; 
     float startY = currentY; 


    case MotionEvent.ACTION_MOVE: 
     float endX = 

    case MotionEvent.ACTION_UP: 


     return super.onTouchEvent(event); 
    } 
    return super.onTouchEvent(event); 
} 
    } 

답변

0

좋아, 내가 발견 : 확인과 같이 시작 위치의 x와 y를 설정해야합니다 :

currentXExample = event.getX(); currentYExample = event.getY();

exampleStartX = currentXExample; exampleStartY = currentYExample;

exampleEndX = currentXExample;

exampleEndY = currentYExample;

Up 이벤트를 벗어날 수 있지만 MOVE 이벤트를 벗어나지 마십시오. 그렇지 않으면 작동하지 않습니다.

LineDrawView.java

// Project:   Java2Lab 
// File:   LineDrawView.java 
// Date:   4/9/13 
// Author:   Joshua Lefelhocz 
// Description:  custom view to draw lines on 

package com.lcc.java2lab11lefelhocz; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.MotionEvent; 
import android.view.View; 

// Notice this class extends View 
public class LineDrawView extends View 
{ 
     // This view's bounds 

private int xMin = 0;   
private int xMax; 
private int yMin = 0; 
private int yMax; 
private float currentX; 
private float currentY; 
private float startX; 
private float endX; 
private float startY; 
private float endY; 
// Paint object 
private Paint paintFill; 

// constructor 
public LineDrawView(Context context) 
{ 
    // call the super class constructor 
    super(context); 

    // The Paint class holds the style and color information about how to draw geometries, text and bitmaps. 
    // For efficiency create the paint objects in the constructor, not in draw 
    // paint.setStrokeWidth(10); // works on lines 
    // You can change the color of Paint without effecting objects already drawn 
    // You can NOT change the style of Paint without effecting objects already drawn 
    // The Style, TextSize apply to all objects drawn with the paint. 

    // Create a default Paint object Style=Fill 
    paintFill = new Paint(); 

    // set the background color when the view is created 
    this.setBackgroundColor(Color.LTGRAY); 
} 

// Called to draw the view. Also called by invalidate(). 
@Override 
protected void onDraw(Canvas canvas) 
{ 
    //canvas.drawColor(Color.LTGRAY); // this works in onDraw to set the background color 

    // Draw a Red Diagonal line from upper left corner to bottom right corner 

    paintFill.setColor(Color.BLACK); 
    canvas.drawLine(startX, startY, endX, endY, paintFill); 

} 

// Called when the view is first created or its size changes. 
@Override 
public void onSizeChanged(int width, int height, int oldWidth, int oldHeight) 
{ 
    // Set the view bounds 
    xMax = width-1; 
    yMax = height-1; 
} 

public boolean onTouchEvent(MotionEvent event) 
{ 
    currentX = event.getX(); 
    currentY = event.getY(); 



    switch(event.getAction()) 
    { 
    case MotionEvent.ACTION_DOWN: 
     startX = currentX; 
     startY = currentY; 
     return true; 
    case MotionEvent.ACTION_MOVE: 
    endX = currentX; 
    endY = currentY; 
    invalidate(); 
     return true; 
    case MotionEvent.ACTION_UP: 


     return true; 
    } 
    return super.onTouchEvent(event); 
    } 
}