此代码在运行时在屏幕上不显示任何内容。我需要在任何地方画一个简单的圆圈。
public class Touchevent extends AppCompatActivity {
float mLastTouchX = 0;
float mLastTouchY = 0;
Canvas zex=new Canvas();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touchevent);
RelativeLayout mylayout = (RelativeLayout)findViewById(R.id.test);
mylayout.setOnTouchListener(
new RelativeLayout.OnTouchListener() {
public boolean onTouch(View v, MotionEvent m) {
onTouchEvent(m);
onDraw(zex);
return true;
}
}
);
}
public void onDraw(Canvas canvasx) {
canvasx.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvasx.drawCircle(mLastTouchX,mLastTouchY,100,paint);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
mLastTouchX = ev.getX();
mLastTouchY = ev.getY();
return true;
}
}
代码显示没有错误。我是否需要添加自定义视图?那么如何实现点击监听器?