2014-03-02 2 views
2

나는 당신의 밝은 마음과 강한 안드로이드 기술을 믿습니다. 나는 조금 붙어있다.몸짓 onSingleTap 이상한 행동을 확인

다음과 같은 경우가 있습니다. 제스처와 캔버스 작업 방법을 배우기 위해 앱을 만들었습니다.

아이디어는 화면에서 한 번 탭하고 탭 한 부분이 버블 (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

그것은 코 세라에서 물론 코드의 일부입니다! 해결책을 찾기 위해 다른 옵션을 사용하는 것이 좋습니다. (예를 들어 비디오 및 예제 코드를 검토하여)

+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

나는에 대한 기본 값이 발견이 특정 시나리오에서, 그러나 for 루프의 외부에서 새로운 거품을 만들 수 이그나시오 루비오의 제안에 동의 false이면 첫 번째 클릭으로 두 개의 거품이 생깁니다. (1) if (mFrame.getChildCount() == 0) 및 (2) if (! found) 조건을 만족하기 위해

이 문제를 해결하려면 다음을 사용하십시오. boolean 형 대신 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 위치와 교차해야합니다.

관련 문제