0

在 onTouchlistener 的帮助下,我的图像正在移动,但我希望它停止在特定位置,以便我可以对其执行一些操作,即点击确切位置时我希望弹出一些来执行另一个活动

这是我所做的编码。是否有任何 ontouchlistner 方法可以执行我想要的操作

    ...
    ...        
        case MotionEvent.ACTION_DOWN:
            float xdiff = Math.abs( x - event.getX());
            float ydiff =  Math.abs( y - event.getY());
            if( xdiff < 23 || ydiff < 23 ){
                isStarted = true;
            }
            break;

        case MotionEvent.ACTION_MOVE:
            if(isStarted)
            {
                x = event.getX() - img.getWidth()/2;
                y = event.getY() - img.getHeight()/2;
                Log.v("X:" + x, "Y: " + y);
                this.invalidate();
            }
            break;

        case MotionEvent.ACTION_UP:
            isStarted = false;

    }
    return true;
}
4

2 回答 2

0

'公共类 MainActivity 扩展 Activity 实现 OnTouchListener {

MySurface msurface;
float x,y,sX,sY,fX,fY;

Bitmap bt,plus;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    msurface=new MySurface(this);
    msurface.setOnTouchListener(this);
    x=0;
    y=0;
    sX=0;
    sY=0;
    fX=0;
    fY=0;

    bt=BitmapFactory.decodeResource(getResources(), R.drawable.hndle);
    plus=BitmapFactory.decodeResource(getResources(), R.drawable.gbut);
    setContentView(msurface);
}
protected void onPause() 
{
    // TODO Auto-generated method stub
    super.onPause();
    msurface.pause();
}

protected void onResume() 
{
    // TODO Auto-generated method stub
    super.onResume();
    msurface.resume();

}
public boolean onTouch(View v, MotionEvent event) 
{
    x=event.getX();
    y=event.getY();


    switch(event.getAction())
    {

    case MotionEvent.ACTION_DOWN:
        sX=event.getX();
        sY=event.getY();
        break;

    case MotionEvent.ACTION_UP:
        fX=event.getX();
        fY=event.getY();
        break;

    }

    return true;
}

公共类 MySurface 扩展 SurfaceView 实现 Runnable {

SurfaceHolder 我们的Holder;

Thread ourTh=null;

boolean isRunning=false;

public MySurface(Context context) 
{
    super(context);
    ourHolder=getHolder();
    ourTh=new Thread(this);
    ourTh.start();

}

public void pause() //bringin android games
{
    isRunning=false;
    while(true)
    {
        try 
        {
            ourTh.join();
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    }
    ourTh=null;
}
public void resume()
{
    isRunning=true;
}


@Override
public void run()
{
    while(isRunning)
    {
        if(!ourHolder.getSurface().isValid())
            continue;

        Canvas canvas=ourHolder.lockCanvas();
        canvas.drawRGB(02, 02, 150); //backgroung color hai
        if(x!=0 && y!=0)
        {
            canvas.drawBitmap(bt, x-(bt.getWidth()/2), y-(bt.getHeight()/2), null);

        }
        if(sX==256 && y==27)
        {

            canvas.drawBitmap(bt, x-(bt.getWidth()/2), y-(bt.getHeight()/2), null);
            Intent a=new Intent(MainActivity.this,dusari.class);
        }
        /*if(fX!=0 && fY!=0)
        {
            canvas.drawBitmap(bt, x-(bt.getWidth()/2), y-(bt.getHeight()/2), null);

        }*/
        ourHolder.unlockCanvasAndPost(canvas);

    }

}
于 2013-11-01T07:13:45.880 回答
0

根据您的评论,您可以使用以下通用解决方案:

    ...
    ...        
        case MotionEvent.ACTION_DOWN:
            float xdiff = Math.abs( x - event.getX());
            float ydiff =  Math.abs( y - event.getY());
            if( xdiff < 23 || ydiff < 23 ){
                isStarted = true;
            }
            break;

        case MotionEvent.ACTION_MOVE:
            if(isStarted)
            {
                x = event.getX() - img.getWidth()/2;
                y = event.getY() - img.getHeight()/2;
                Log.v("X:" + x, "Y: " + y);
                this.invalidate();
            }
            break;

        case MotionEvent.ACTION_UP:
            if(isStarted) 
            {
                Rect imgRect = new Rect();
                img.getGlobalVisibleRect(imgRect);

                // compare if imgRect is close enough to correct place
                // if: imgRect.left, imgRect.top are near door knob position
                //      then: open dialog/ start activity
            }
            isStarted = false;

    }
    return true;
}

基本上,ACTION_UP比较后的位置,img看看它是否接近所需的位置(X,Y),然后执行动作..

如果您可以提供我如何初始化/绘制门和视图上的旋钮,我可以提供更具体的解决方案。

于 2013-11-01T06:08:05.287 回答