Question

I want a bitmap to follow my finger. But when I use MotionEvent.ACTION_MOVE the bitmap lags behind. Is there a way the bitmap follows instantly (or almost instantly) your finger?

    public boolean onTouch(View v, MotionEvent me) 
{   
    // TODO Auto-generated method stub
    switch(me.getAction())
    {
    case MotionEvent.ACTION_DOWN:
        if(me.getX() > player.get_RectXPos() && me.getX() < player.get_RectXPos() + player.get_Width() && 
           me.getY() > player.get_RectYPos() && me.getY() < player.get_RectYPos() + player.get_Height())
        {
            isDown = true;
        }
        break;
    case MotionEvent.ACTION_UP:
        isDown = false;
        break;
    case MotionEvent.ACTION_MOVE:
        if(isDown == true)
        {
            player.set_xPos(me.getX());
            player.set_yPos(me.getY());
        }

        break;
    }
    return true;
}

I think you asked for this part:

    void render(Canvas canvas)
{
    m_Src = new Rect(0, 0, m_RectWidth, m_RectHeight);
    m_Dst = new Rect(m_RectX, m_RectY, m_RectX + m_RectWidth, m_RectY + m_RectHeight);
    canvas.drawBitmap(m_Bitmap, m_Src, m_Dst, null);
}
Was it helpful?

Solution

void render(Canvas canvas)
{
m_Src = new Rect(0, 0, m_RectWidth, m_RectHeight);
m_Dst = new Rect(m_RectX, m_RectY, m_RectX + m_RectWidth, m_RectY + m_RectHeight);
canvas.drawBitmap(m_Bitmap, m_Src, m_Dst, null);
}

Here you allocate 2 Rect objects each time. You should initialize them only once and use Rect.set to change their values.

Also if your Bitmap size should not change, you could use public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint) since this method doesn't do any scaling/translation to the Bitmap and should be more efficient.

OTHER TIPS

My guess is that the "onTouch" handler interrupts everytime the drawing...

Your "onDraw" routine should be located in its proper thread (maybe ASyncTask could do the job too). You'll have then to forward the x,y coordinates of your finger to the gui thread.

Type "Thread Gui Android" in google for a comprehensive example :)

You must do some unnecessary work on your onTouch method. Because it should not be laggy if you only change its position.

Edit: You can save your Rect m_Src and m_Dst and just change their coordinates in your render. That will save a lot of work for the system and the garbage collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top