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 | public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); CustomView cv = new CustomView(this); cv.setBackgroundColor(Color.GRAY); setContentView(cv); } } class CustomView extends View { private Paint paint; private int positionX; private int positionY; private String l = ""; public CustomView(Context context) { super(context); paint = new Paint(); paint.setColor(Color.WHITE); paint.setTextSize(18); paint.setAntiAlias(true); } protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawText(l, 10, 100, paint); invalidate(); } public boolean onTouchEvent(MotionEvent event) { positionX = (int)event.getRawX(); positionY = (int)event.getRawY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: l = "DOWN - " + String.valueOf(positionX) + " " + String.valueOf(positionY); break; case MotionEvent.ACTION_MOVE: l = "MOVE - " + String.valueOf(positionX) + " " + String.valueOf(positionY); break; case MotionEvent.ACTION_UP: l = "UP - " + String.valueOf(positionX) + " " + String.valueOf(positionY); break; } return true; } } |
Tuesday, September 11, 2012
touch event in custom view (android)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment