Class로 그리기
public class CustomView extends View {
private Paint paint;
public CustomView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(100, 100, 500, 200, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(super.getContext(), "MotionEvent.ACTION_DOWN:" +
event.getX() + "," + event.getY(), Toast.LENGTH_SHORT).show();
}
return super.onTouchEvent(event);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomView myView=new CustomView(this);
setContentView(myView);
}
}
참조
https://www.youtube.com/watch?v=iNOltfj_40Y