gpt4 book ai didi

android - 手势 onSingleTapConfirmed 奇怪的行为

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:12 28 4
gpt4 key购买 nike

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

我有以下情况。我创建了用于学习如何使用手势和 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 - 手势 onSingleTapConfirmed 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129690/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com