2011年4月3日日曜日

Android function(OnTouchListener)

7.2


Android-OnTouchListener

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
package eaft.android.scribe;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Draw extends Activity {
        
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Set full screen view
        getWindow().setFlags(WindowManager.
 LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        drawView = new DrawView(this);
        setContentView(drawView);
        drawView.requestFocus();
    }
}    

************************************************************************************


package eaft.android.scribe;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawView extends View implements OnTouchListener
{
    List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();


    public DrawView(Context context) 
    {
        super(context);
        this.setClickable(true);
        setBackgroundColor(Color.WHITE);    
  
        this.setOnTouchListener(this);        
        
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
    }
    
    @Override
    public void onDraw(Canvas canvas) 
    {
        for (Point point : points) 
        {
                
              canvas.drawCircle(point.x, point.y, 2, paint);
        

        }
    }

    public boolean onTouch(View view, MotionEvent event) 
    {                   
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        
        invalidate();
        return true;
    }
}

class Point 
{
    float x, y;
}






Output from Android Emulator: