ontouchevent android,onTouchevent()问题android

Android 活动与自定义视图的触摸事件处理问题
博客作者在尝试处理Android应用中的触摸事件时遇到了问题。当触摸事件处理代码位于自定义视图DrawView内部时,一切正常,可以正确检测并拖动屏幕上的彩色球。但当将触摸事件处理代码移至主活动时,尽管事件被捕捉,却无法正确读取和更新变量X和Y的值,导致拖动功能失效。作者已经排除了代码逻辑错误的可能性,如条件判断和变量赋值,问题可能涉及到活动和视图之间的交互或事件传递机制。

当我运行下面的代码一切正常,这是一个简单的应用程序与三个球,你可以移动...

public class dragndrop extends Activity {

/** Called when the activity is first created. */

private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls

private static final String TAG="MyTAG";

DrawView myView;

private int balID = 0; // variable to know what ball is being dragged

int X;

int Y;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Point point1 = new Point();

point1.x = 50;

point1.y = 20;

Point point2 = new Point();

point2.x = 100;

point2.y = 20;

Point point3 = new Point();

point3.x = 150;

point3.y = 20;

// declare each ball with the ColorBall class

colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1);

colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2);

colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3);

myView = new DrawView(this);

setContentView(myView);

}

public class DrawView extends View {

public DrawView(Context context) {

super(context);

setFocusable(true); //necessary for getting the touch events

// setting the start point for the balls

}

// the method that draws the balls

@Override protected void onDraw(Canvas canvas) {

//draw the balls on the canvas

for (ColorBall ball : colorballs) {

canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);

}

}

// events when touching the screen

public boolean onTouchEvent(MotionEvent event) {

int eventaction = event.getAction();

X = (int)event.getX();

Y = (int)event.getY();

switch (eventaction ) {

case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball

balID = 0;

for (ColorBall ball : colorballs) {

Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y);

Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY());

int x =X;

int y =Y;

Log.d(TAG,"lalalalalala"+x+" coords: "+y);

if (x > ball.getX() && x < ball.getX()+50 && y > ball.getY() && y < ball.getY()+50){//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){

Log.d(TAG,"inside ball coords!!!!!!!!!!!!!!!!!!!!!!!!:"+ball.getX()+" coords: "+ball.getY());

balID = ball.getID();

break;

}

}

break;

case MotionEvent.ACTION_MOVE: // touch drag with the ball

// move the balls the same as the finger

if (balID > 0) {

colorballs[balID-1].setX(X-25);

colorballs[balID-1].setY(Y-25);

}

break;

case MotionEvent.ACTION_UP:

// touch drop - just do things here after dropping

break;

}

// redraw the canvas

myView.invalidate();

return true;

}

}

}

但是,当我尝试从主要活动处理onTouchevent不起作用时,奇怪的是它无法读取一个简单的变量(x,y)!

我无法理解为什么会发生这种情况,它似乎只有在它的视图中它才能被红色!

public class dragndrop extends Activity {

/** Called when the activity is first created. */

private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls

private static final String TAG="MyTAG";

DrawView myView;

private int balID = 0; // variable to know what ball is being dragged

int X;

int Y;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Point point1 = new Point();

point1.x = 50;

point1.y = 20;

Point point2 = new Point();

point2.x = 100;

point2.y = 20;

Point point3 = new Point();

point3.x = 150;

point3.y = 20;

// declare each ball with the ColorBall class

colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1);

colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2);

colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3);

myView = new DrawView(this);

setContentView(myView);

}

// events when touching the screen

public boolean onTouchEvent(MotionEvent event) {

int eventaction = event.getAction();

X = (int)event.getX();

Y = (int)event.getY();

switch (eventaction ) {

case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball

balID = 0;

for (ColorBall ball : colorballs) {

Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y);

Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY());

int x =X;

int y =Y;

Log.d(TAG,"lalalalalala"+x+" coords: "+y);

if (x > ball.getX() && x < ball.getX()+50 && y > ball.getY() && y < ball.getY()+50){//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){

Log.d(TAG,"inside ball coords!!:"+ball.getX()+" coords: "+ball.getY());

balID = ball.getID();

break;

}

}

break;

case MotionEvent.ACTION_MOVE: // touch drag with the ball

// move the balls the same as the finger

if (balID > 0) {

colorballs[balID-1].setX(X-25);

colorballs[balID-1].setY(Y-25);

}

break;

case MotionEvent.ACTION_UP:

// touch drop - just do things here after dropping

break;

}

// redraw the canvas

myView.invalidate();

return true;

}

public class DrawView extends View {

public DrawView(Context context) {

super(context);

setFocusable(true); //necessary for getting the touch events

// setting the start point for the balls

}

// the method that draws the balls

@Override protected void onDraw(Canvas canvas) {

//canvas.drawColor(0xFFCCCCCC); //if you want another background color

//draw the balls on the canvas

for (ColorBall ball : colorballs) {

canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);

}

}

}

}

谁知道为什么?

是@bigstones onTouchevent工作,它捕获所有操作,问题是,如果我在活动中有ontouchevent代码变量X,Y似乎不工作,虽然他们有一个值,我可以打印它(或日志)我说的是测试,我已经尝试将if()语句(getX,getY)中的所有值更改为整数,并且它不仅适用于X,Y .....再次检查代码请!

谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值