【发布时间】:2014-02-11 13:07:26
【问题描述】:
我想设置一个drawText对象可见和不可见,只需点击一下。它一开始是不可见的,但是当用户点击屏幕上的任意位置时,该对象会出现,反之亦然,再次点击后,该对象将再次不可见。
这是我的代码
public void onDraw(Canvas canvas) {
if (GetterSetter.isVisible) {
renderText(canvas);
}
}
private void renderText(Canvas canvas) {
Paint textPaint = new Paint();
textPaint.setTextSize(18);
textPaint.setAntiAlias(true);
textPaint.setARGB(0xff, 0x00, 0x00, 0x00);
canvas.drawText(GetterSetter.currLoc, 16, 50, textPaint);
}
这是我的 onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent e) {
x = e.getX();
y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
if (GetterSetter.counter < 1) {
GetterSetter.counter++;
GetterSetter.isVisible = true;
} else {
GetterSetter.counter = 0;
GetterSetter.isVisible = false;
}
break;
}
return true;
}
这是我的常量:GetterSetter.java
public static String currLoc = "Hello World";
public static boolean isVisible = false;
public static int counter = 0;
我的问题是,它不起作用。除了我目前所做的之外,我不知道应该如何让它工作。
【问题讨论】:
标签: java android canvas touch-event