2014-03-02 52 views
2

我相信你的聰明才智和強大的android技能。我有點卡住了。手勢onSingleTapConfirmed奇怪的行爲

我有以下情況。我已經創建了學習如何使用手勢和畫布的應用程序。

想法很簡單,當我單擊屏幕上的一個按鈕時,我已經敲擊的地方應該會出現泡泡(R.drawable.bubble)。如果已經有一些泡泡應用程序應該刪除它(清除空間)。

但是,我遇到了一些困難。我挖掘的地方和泡沫實際出現的地方有一些明顯不同的地方。

請給我一些建議,我應該看看。我錯過了什麼?

在此先感謝。下面我提供我的代碼。

public class BubbleActivity extends Activity { 

// Main view 
RelativeLayout mFrame; 

// Bubble image 
private Bitmap mBitmap; 

// gesture detector 
GestureDetector mGestureDetector; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bubble); 

    // setup user interface 
    mFrame = (RelativeLayout) findViewById(R.id.frame); 

    // load basic bubble Bitmap 
    mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b128); 
} 




@Override 
protected void onResume() { 
    super.onResume(); 

    // init gesture detector 
    setupGestureDetector(); 

} 




private void setupGestureDetector() { 

    mGestureDetector = new GestureDetector(this, 
      new GestureDetector.SimpleOnGestureListener() { 

     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) { 


      if(mFrame.getChildCount() == 0) { 

       BubbleView bubble = new BubbleView(getApplicationContext(), 
          e.getX(), 
          e.getY()); 
       mFrame.addView(bubble); 

      } else { 

       for(int i=0; i < mFrame.getChildCount(); i++) { 

        BubbleView bubble = (BubbleView) mFrame.getChildAt(i); 

        if(bubble.intersect(e.getX(), e.getY())) { 

         mFrame.removeViewAt(i); 

        } else { 

         BubbleView newBubble = new BubbleView(getApplicationContext(), 
            e.getX(), 
            e.getY()); 

         mFrame.addView(newBubble); 
        } 

       } 

      } 




      return true; 
     } 



    }); 
} 


@Override 
public boolean onTouchEvent(MotionEvent event) { 

    this.mGestureDetector.onTouchEvent(event); 

    return false; 
} 


private class BubbleView extends View { 
    private static final int BITMAP_SIZE = 64; 
    private float mXPos; 
    private float mYPos; 

    private Bitmap mScaledBitmap; 
    private int mScaledBitmapWidth; 

    public BubbleView(Context context, float x, float y) { 
     super(context); 

     mXPos = x; 
     mYPos = y; 

     Random r = new Random(); 

     createScaledBitmap(r); 
    } 

    private void createScaledBitmap(Random r) { 

     mScaledBitmapWidth = (r.nextInt(3) + 1) * BITMAP_SIZE; 

     mScaledBitmap = Bitmap.createScaledBitmap(mBitmap, 
                mScaledBitmapWidth, 
                mScaledBitmapWidth, 
                false); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     Paint mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 

     canvas.drawBitmap(mScaledBitmap, 
          this.mXPos, 
          this.mYPos, 
          mPaint); 
    } 



    public boolean intersect(float x, float y) { 

     if(Math.abs(this.mXPos - x) < mScaledBitmapWidth 
       || Math.abs(this.mYPos - y) < mScaledBitmapWidth) { 
      return true; 
     } else { 
      return false; 
     } 

    } 

} 





@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.bubble, menu); 
    return true; 
} 

}

回答

1

一兩件事,我所看到的是,你應該外面的for循環創建新的BubbleView對象推薦你。 我會使用布爾值,如果你沒有找到循環內的任何人,那麼你可以創建一個。

public boolean onSingleTapConfirmed(MotionEvent e) { 
     boolean found = false; 

     if(mFrame.getChildCount() == 0) { 

      BubbleView bubble = new BubbleView(getApplicationContext(), 
         e.getX(), 
         e.getY()); 
      mFrame.addView(bubble); 

     } else { 

      for(int i=0; i < mFrame.getChildCount(); i++) { 

       BubbleView bubble = (BubbleView) mFrame.getChildAt(i); 

       if(bubble.intersect(e.getX(), e.getY())) { 

        mFrame.removeViewAt(i); 
        found = true; 
        break; 
       } 
      } 
     } 

     if (!found) { 
      BubbleView newBubble = new BubbleView(getApplicationContext(), 
           e.getX(), 
           e.getY()); 

      mFrame.addView(newBubble); 
     } 
1

這是一個當然的coursera的部分代碼! 我會使用其他選項來查找(通過審查的影片,例如和紅粉示例代碼)的解

+1

是的,這是來自coursera,但我沒有要求給我答案的任務。我只想對特定主題有所瞭解 – SuperManEver

1

在BubbleView構造函數中使用以下行來設置縮放位圖的位置。

  // Adjust position to center the bubble under user's finger 
     mXPos = x - mScaledBitmapWidth/2; 
     mYPos = y - mScaledBitmapWidth/2; 

這會將氣泡視圖位圖居中在用戶的手指下。 Coursera的骨架項目已經包含這些行。

此外,你還需要一個校正代碼 -

 @Override 

    public boolean onTouchEvent(MotionEvent event) { 

      return this.mGestureDetector.onTouchEvent(event); 
      //return false; 
    } 
0

我同意伊格納西奧盧比奧的建議,以創建新的泡沫之外循環然而,在這個特殊的場景下找到爲默認值(1)滿足條件if(mFrame.getChildCount()== 0)和(2)if(!found)

要解決此問題,您可以使用int代替布爾值

public boolean onSingleTapConfirmed(MotionEvent event) { 

      int found = 0; 

      if(mFrame.getChildCount() == 0) { 

       Log.v(TAG, "Make First Bubble"); 
       BubbleView bubble = new BubbleView(getApplicationContext(), 
         event.getX(), 
         event.getY()); 

       mFrame.addView(bubble); 

      } else { 

       for(int i=0; i < mFrame.getChildCount(); i++) { 

        BubbleView bubble = (BubbleView) mFrame.getChildAt(i); 

        if(bubble.intersects(event.getX(), event.getY())) { 

         mFrame.removeViewAt(i); 
         found = 1; 
         break; 

        }else{ 
         found = 2; 
        } 
       } 
      } 

      if (found == 2) { 

       BubbleView newBubble = new BubbleView(getApplicationContext(), 
         event.getX(), 
         event.getY()); 

       mFrame.addView(newBubble); 
      } 

      return true; 
     } 

此外,仔細檢查你View.intersect()。觸摸必須與氣泡的x和y位置相交,而不是x或y位置。