开始学习Canvas,目前有两个类(主要的一个叫view和view)View类onDraw创建一个目标(即cicle的数量,每一个颜色不同)
我设置了一个 ontouch 监听器来记录用户点击的 x 和 y
然后我的麻烦是在用户触摸的地方画一个新的圆圈/点
更新了课程
主班
public class StartScreen extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
绘图类
public class DrawView extends View implements View.OnTouchListener {
private Paint paint[];
private Context context;
private Canvas canvas;
//definging some variables
public DrawView(Context pContext) {
super(pContext);
this.context = pContext;
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
paint = new Paint[5];
setupColours();
// setting varibale like raduis etc
}
private void setupColours() {
// Creating Arrray of Paint Colours
}
@Override
public void onDraw(Canvas pCanvas) {
canvas = pCanvas;
newRadius = radius;
for (int i = 0; i < rings; i++) {
if (i == 3) {
canvas.drawCircle(centreWidth, centreHeight, newRadius, paint[0]);
} else {
canvas.drawCircle(centreWidth, centreHeight, newRadius, paint[1]);
}
canvas.drawCircle(centreWidth, centreHeight, newRadius - targetBoundary, paint[i / 2]);
newRadius = newRadius - ringOffset;
}
this.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("TAG2", "x: " + event.getX() + " y: " + event.getY());
drawHit(event.getX(), event.getY());
return true;
}
public void drawHit(float hitX, float hitY) {
Log.d("HIT", "Hit being drawn");
Paint paint2 = new Paint();
paint2.setColor(Color.BLACK);
canvas.drawCircle(hitX, hitY, 100, paint2);
}
调用了该方法,但未绘制圆。我究竟做错了什么。谢谢