Skip to content

Commit b03b75f

Browse files
author
TetsuyaNegishi
committed
Touch Change Circle Color
1 parent ecea3e1 commit b03b75f

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/com/example/drawapp/SampleView.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,37 @@
1111
public class SampleView extends View {
1212

1313
private Paint paint = new Paint();
14+
private int color = Color.RED;
15+
private int bx = 100;
16+
private int by = 100;
17+
private int R = 50;
1418
public SampleView(Context context) {
1519
super(context);
1620
setBackgroundColor(Color.WHITE);
1721
}
1822

1923
@Override
2024
public void onDraw(Canvas canvas){
21-
canvas.drawCircle(100, 100, 20, paint);
25+
paint.setColor(color);
26+
canvas.drawCircle(100, 100, R, paint);
2227
}
2328

2429
@Override
2530
public boolean onTouchEvent(MotionEvent event){
26-
Toast.makeText(getContext(), "Touch", Toast.LENGTH_SHORT).show();
31+
int action = event.getAction();
32+
if((action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN){
33+
int ex = (int)event.getX();
34+
int ey = (int)event.getY();
35+
if((bx - ex)*(bx - ex)+(by - ey)*(by - ey) <= R*R){
36+
Toast.makeText(getContext(),"Touch‚³‚ê‚Ü‚µ‚½", Toast.LENGTH_SHORT).show();
37+
if(color == Color.BLUE){
38+
color = Color.RED;
39+
} else {
40+
color = Color.BLUE;
41+
}
42+
invalidate(); // Ä•`‰æ
43+
}
44+
}
2745
return true;
2846
}
2947
}

0 commit comments

Comments
 (0)