android - 单击手势确认奇怪行为

标签 android

我相信你聪明的头脑和强大的机器人技能。我有点卡住了。

我有以下情况。我创建了用于学习如何使用手势和 Canvas 的应用程序。

想法很简单,当我单次点击屏幕时,我点击的地方应该出现气泡(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;
}

}

最佳答案

我看到的一件事是您应该在 for 循环之外创建新的 BubbleView 对象。 我会使用一个 bool 值,如果您在循环内找不到任何人,那么您可以创建一个。

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);
        }

关于android - 单击手势确认奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129690/

相关文章:

java - 在listview android中多次点击

Android Studio Webview 不调用照片库

java - Android 中的 SQLITE 获取语句不起作用

java - 不通过 AndroidBenchmarkRunner 使用 IsolationActivity

java - 运行相机服务时 takePicture 失败

android - Google Checkout 帐户注册错误

android - onNavigationItemSelected 没有被调用

android - 如何制作一次显示三个 View 的 ViewFlipper?

java - 使用从 "App A"到 "App B"的 URI 在 Intentw 中传递参数 -Android

java - 在 Openshift 上发布网站后,无法在 Android 应用程序上获取 FCM 有效负载消息