我正在開發一個應用程序,我想在觸摸事件註冊時顯示視圖。問題是視圖不會顯示,我的代碼變得沒有反應。查看不顯示onTouch
這是我的觀點:
public class TouchFX extends View {
RectF oval;
float Tx, Ty;
Paint paint;
int longSide, numRun;
float top, left, right, bottom;
boolean removable;
public TouchFX(Context context) {
super(context);
// TODO Auto-generated constructor stub
Tx = TouchService.x;
Ty = TouchService.y;
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setStrokeJoin(Paint.Join.ROUND);
numRun = 0;
removable = false;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (canvas.getWidth() > canvas.getHeight())
longSide = canvas.getWidth();
else
longSide = canvas.getHeight();
if (numRun == 0) {
left = Tx - (longSide/15);
top = Ty + (longSide/15);
right = Tx + (longSide/15);
bottom = Ty - (longSide/15);
} else if (numRun <= 8) {
left += 2;
top += 2;
right += 2;
bottom += 2;
}
oval = new RectF(left, top, right, bottom);
if (numRun < 9)
canvas.drawArc(oval, 0, 360, false, paint);
else
removable = true;
numRun++;
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
這裏是我的onTouch處理程序:
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (v == tv && event.getAction() == MotionEvent.ACTION_DOWN
&& isRunning == true) {
x = event.getX();
y = event.getY();
tfx = new TouchFX(this);
winmngr.addView(tfx, lpFX);
//Do some stuff, make some Toasts
while(!tfx.removable) {
//waiting for animation to end
}
winmngr.removeView(tfx);
}
return false;
}
現在,很明顯它被陷在while循環中,但爲什麼不是繪製弧形和更新布爾值?
你能解釋爲什麼你要在onDraw()中凍結400毫秒的UI線程嗎? – Paul 2012-07-27 18:07:44