[英]How to detect single tap on canvas drawings or bitmaps using onTouchListener?
The objective of this game is similar to that of a bubble popping game. 该游戏的目标与泡泡弹出游戏的目标相似。 Each time a bubble is touched, an interaction/event happens.
每次碰到气泡,都会发生互动/事件。
So far this is what i have. 到目前为止,这就是我所拥有的。 It's kinda messy since i am new at this, but my professor told me to use onTouch(view v, motionevent event) and in that method, i will use something like event.getSource();
因为我是新来的,所以有点混乱,但是我的教授告诉我使用onTouch(view v,motionevent event),在这种方法中,我将使用event.getSource();之类的方法。 for me to be able to create an interaction with methods serving the purpose of a touchable drawing/bitmap image.
对我来说,能够与用于可触摸的图形/位图图像的方法进行交互。
Any advice or help would be great! 任何建议或帮助都会很棒! Many thanks.
非常感谢。
public class MainActivity extends Activity implements OnTouchListener {
OurView v;
Bitmap icon1, icon2, icon3;
float x,y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = new OurView(this);
v.setOnTouchListener(this);
icon1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
icon2 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
icon3 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
x = y = 0;
setContentView(v);
}
@Override
protected void onPause() {
super.onPause();
v.pause();
}
@Override
protected void onResume() {
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable {
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public OurView(Context context) {
super(context);
holder = getHolder();
}
@Override
public void run() {
while (isItOK == true){
if (!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 100, 120, 10);
c.drawBitmap(icon1, x=50, y=100, null);
c.drawBitmap(icon2, x=180, y=100, null);
c.drawBitmap(icon3, x=310, y=100, null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isItOK = true;
t = new Thread(this);
t.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
me.getSource();
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
} }
尝试使init检查xy是否协调位图的矩形是否在循环内,是否在位图的矩形内?
You are drawing your bitmaps at known coordinates so why not just do a range check? 您正在以已知坐标绘制位图,那么为什么不仅仅进行范围检查呢? Eg if x > 50 and x < 100 and y > 100 and y < 150 then they tapped icon 1 (assuming your bitmap is 50x50) A few seconds ago via mob
例如,如果x> 50且x <100且y> 100且y <150则他们点击了图标1(假设位图为50x50),几秒钟前通过mob
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.