0

My drawing app runs well ,but when I want to see my drawing, I need to press my home button and launch it again.

I want to know why my app doesn't show my drawing at the moment.

public class MainActivity extends ActionBarActivity {

    private MyView vw; 

    //정점 하나에 대한 정보를 가지는 클래스 

    public class Vertex {
        Vertex(float ax, float ay , boolean ad) {
            x= ax;
            y= ay;
            Draw= ad;
        }
        float x;
        float y;
        boolean Draw;
    }

    ArrayList<Vertex> arVertex;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        vw = new MyView(this);
        setContentView(vw);

        arVertex=new ArrayList<Vertex>();
    }

    protected class MyView extends View {
        Paint mPaint;

        public MyView(Context context) {
            super(context);

            //Paint 객체 미리 초기화

            mPaint=new Paint();
            mPaint.setColor(Color.BLACK);
            mPaint.setStrokeWidth(3);
            mPaint.setAntiAlias(true);
        }

        public void onDraw(Canvas canvas) {
            canvas.drawColor(Color.LTGRAY);


            /*정점을 순회하면서 선분으로 있는다.*/

            for(int i=0;i<arVertex.size();i++) {
                if(arVertex.get(i).Draw) {
                    canvas.drawLine(arVertex.get(i-1).x,arVertex.get(i-1).y,arVertex.get(i).x,arVertex.get(i).y, mPaint);
                }
            }
        }

        //터치 이동시마다 정점추가 

        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction()==MotionEvent.ACTION_DOWN) {
                arVertex.add(new Vertex(event.getX(),event.getY(),false));
                return true;
            }
            if (event.getAction()==MotionEvent.ACTION_MOVE) {
                arVertex.add(new Vertex(event.getX(),event.getY(),true));
                return true;
            }
            return false;
        }
    }   
}

I want to know why my app doesn't show my drawing at the moment. Sorry

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
  • Please translate your native language statements to English, I can't understand those at all! – frogatto Aug 16 '14 at 06:15
  • 2
    where do you call `invalidate()` to refresh the draw. check this http://stackoverflow.com/questions/16650419/draw-in-canvas-by-finger-android – Raghunandan Aug 16 '14 at 06:15

1 Answers1

0

Add invalidate() in your onTouchEvent method:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction()==MotionEvent.ACTION_DOWN) {
        arVertex.add(new Vertex(event.getX(),event.getY(),false));
        invalidate(); // <-- Here
        return true;
    }
    if (event.getAction()==MotionEvent.ACTION_MOVE) {
        arVertex.add(new Vertex(event.getX(),event.getY(),true));
        invalidate(); // <-- And here            
        return true;
    }
    return false;
}

invalidate docs

frogatto
  • 28,539
  • 11
  • 83
  • 129