查看不显示onTouch

时间:2012-07-27 18:03:42

标签: android view while-loop

我正在开发一个应用程序,我想在注册触摸事件时显示视图。问题是视图无法显示,我的代码无响应。

以下是我的观点:

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循环中,但是为什么它不是绘制弧并更新布尔值?

2 个答案:

答案 0 :(得分:1)

由于onTouch事件在UI线程中运行(因此onDraw),因此它会卡住。所以你在while循环中阻止了你的UI线程,onDraw()没有得到处理器时间。

考虑使用回调机制,因此removeView()例程会触发onDraw例程。 UI线程中的sleep(400)也是非常糟糕的主意。您可以尝试使用处理程序并执行延迟执行:

 public class YouCustomView extends View
 {

     Handler handler = new Handler();

     @Override
     protected onDraw(Canvas canvas){
         //your draw routine here
         .... 

         //wait 400 ms and remove view
         handler.postDelayed(new Runnable()
         {
             @Override
             public void run()
             {
                 //trigger callback to remove view
             }
         }, 400);

答案 1 :(得分:-1)

这里是我的代码,它完全适合我,它将绘制一个字符串希望它将帮助您解决问题

@Override
public boolean onTouchEvent(MotionEvent event) {
    // draw text at touch
    try {
        canvas = getHolder().lockCanvas();
        synchronized (getHolder()) {
            if (event.getAction() == MotionEvent.ACTION_DOWN 
                    || event.getAction() == MotionEvent.ACTION_MOVE) {
                // clear the screen
                canvas.drawColor(Color.BLACK);
                // draw glyphs
                glyphs.drawString(canvas, "Drawing string at "
                        + (int) event.getX() + " " + (int) event.getY(),
                        (int) event.getX(), (int) event.getY());
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                canvas.drawColor(Color.BLACK);
                glyphs.drawString(canvas, "Drawn string at "
                        + (int) event.getX() + " " + (int) event.getY(),
                        (int) event.getX(), (int) event.getY());
            }
        }
    } finally {
        if (canvas != null) {
            getHolder().unlockCanvasAndPost(canvas);
        }
    }
    // event was handled
    return true;
}