Custom Widget 1 UNIT 26 로봇 SW 콘텐츠 교육원 조용수
캔버스 public void drawColor(int color) 2 public class ControllerView extends View { public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { canvas.drawColor(Color.BLUE); } public class MainActivity extends Activity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new ControllerView(this)); }
캔버스 public void drawColor(int color) 3 public class ControllerView extends View { public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { canvas.drawColor(0xffff0000); }
캔버스 public void drawRGB (int r, int g, int b) 4 public class ControllerView extends View { public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { canvas.drawRGB(255, 255, 0); }
캔버스 public void drawARGB (int a, int r, int g, int b) 5 public class ControllerView extends View { public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { canvas.drawARGB(100, 255, 255, 0); }
캔버스 public void drawPaint (Paint paint) Paint –public void setARGB (int a, int r, int g, int b) –public void setAlpha (int a) –public void setAntiAlias (boolean aa) –public void setColor (int color) –public void setTextSize (float textSize) 6 public class ControllerView extends View { private final Paint paint = new Paint(); public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { paint.setARGB(100, 255, 255, 0); canvas.drawPaint(paint); }
캔버스 public void drawLine (float startX, float startY, float stopX, float stopY, Paint paint) 7 public class ControllerView extends View { private final Paint paint = new Paint(); public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { paint.setColor(Color.RED); canvas.drawLine(50, 50, 300, 300, paint); }
캔버스 public void drawCircle (float cx, float cy, float radius, Paint paint) 8 public class ControllerView extends View { private final Paint paint = new Paint(); public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { paint.setColor(Color.RED); canvas.drawCircle(200, 200, 100, paint); }
캔버스 public void drawRect (float left, float top, float right, float bottom, Paint paint) 9 public class ControllerView extends View { private final Paint paint = new Paint(); public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { paint.setColor(Color.RED); canvas.drawRect(50, 50, 300, 300, paint); }
캔버스 public void drawRoundRect (RectF rect, float rx, float ry, Paint paint) 10 public class ControllerView extends View { private final Paint paint = new Paint(); public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { paint.setColor(Color.RED); canvas.drawRoundRect(new RectF(50, 50, 300, 300), 10, 10, paint); }
캔버스 public void drawText (String text, float x, float y, Paint paint) 11 public class ControllerView extends View { private final Paint paint = new Paint(); public ControllerView(Context context) { super(context); protected void onDraw(Canvas canvas) { paint.setColor(Color.RED); paint.setTextSize(50); canvas.drawText(" 테스트 ", 100, 100, paint); }
캔버스 public void drawPath(Path path, Paint paint) 12 public class ControllerView extends View { private final Paint paint = new Paint(); private final Path path = new Path(); private float oldX, oldY; private static final float TOUCH_TOLERANCE = 4; public ControllerView(Context context) { super(context); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(12); protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); canvas.drawPath(path, paint); }
캔버스 public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: { path.reset(); path.moveTo(x, y); oldX = x; oldY = y; invalidate(); } break; case MotionEvent.ACTION_UP: { path.lineTo(x, y); invalidate(); } break; case MotionEvent.ACTION_MOVE: { float dx = Math.abs(x - oldX); float dy = Math.abs(y - oldY); if(dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { path.lineTo(x, y); oldX = x; oldY = y; } invalidate(); } break; } return true; }