Java 如何为Android自定义视图添加OnClick事件
我有两个画面在屏幕上移动,一个是球,一个是人。 我想发生的是,当用户触摸到这个人的图像时,球就会掉下来 我的问题是我似乎无法添加onclick/ontouch事件并使其工作 我没有正确地执行它,有人能帮忙吗 我已经包括了下面的3门课。格雷格就是那个人,球的名字叫鲍尔:) TestAnimationActivity.javaJava 如何为Android自定义视图添加OnClick事件,java,android,eclipse,Java,Android,Eclipse,我有两个画面在屏幕上移动,一个是球,一个是人。 我想发生的是,当用户触摸到这个人的图像时,球就会掉下来 我的问题是我似乎无法添加onclick/ontouch事件并使其工作 我没有正确地执行它,有人能帮忙吗 我已经包括了下面的3门课。格雷格就是那个人,球的名字叫鲍尔:) TestAnimationActivity.java package com.test.firstAnimation; import android.app.Activity; import android.
package com.test.firstAnimation;
import android.app.Activity;
import android.os.Bundle;
public class TestAnimationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyAnimationView(this));
}
}
Sprite.java
package com.test.firstAnimation;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class Sprite extends View implements OnClickListener{
private static int gregCoordX = 410; // the x coordinate at the canvas for greg
private Bitmap img; // the image of Greg
private Bitmap img2; // the image of pointer
private static int gregCoordY = 125; // the y coordinate at the canvas for greg
private static int pointCoordX = 10;
private static int pointCoordY = 10;
private static int count = 1;
private static int ballSpeed = 25;
private static boolean goingRight = false;
private static boolean goingLeft = true;
private static boolean pointerGoingRight = false;
private static boolean pointerGoingLeft = true;
public Sprite(Context context, int drawable) {
super(context);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
img = BitmapFactory.decodeResource(context.getResources(), drawable);
img2 = (BitmapFactory.decodeResource(context.getResources(), drawable));
count++;
}
public static int getCount() {
return count;
}
void setX(int newValue) {
gregCoordX = newValue;
}
public static int getX() {
return gregCoordX;
}
public static int getY() {
return gregCoordY;
}
public static int getBX() {
return pointCoordX;
}
public static int getBY() {
return pointCoordY;
}
public Bitmap getBitmap() {
return img;
}
public Bitmap getImg2() {
return img2;
}
public static void dropBall()
{
pointCoordY++;
}
public static void moveBall(int x) {
// check the borders
//if more than ten go right
//if ten go left
//if more than 250 go left
if (x <= 10 && pointerGoingLeft)
{
pointCoordX = pointCoordX + ballSpeed;
pointerGoingRight = true;
pointerGoingLeft = false;
}
else if (x >= 410 && pointerGoingRight)
{
pointCoordX = pointCoordX - ballSpeed;
pointerGoingLeft = true;
pointerGoingRight = false;
}
else if (pointerGoingRight)
pointCoordX = pointCoordX + ballSpeed;
else
pointCoordX = pointCoordX - ballSpeed;
if(MyAnimationView.ballDropping == true)
{
while (pointCoordY<gregCoordY)
dropBall();
}
}
public static void moveGreg(int x) {
if (x <= 10 && goingLeft)
{
gregCoordX = gregCoordX + count;
goingRight = true;
goingLeft = false;
}
else if (x >= 410 && goingRight)
{
gregCoordX = gregCoordX - count;
goingLeft = true;
goingRight = false;
}
else if (goingRight)
gregCoordX = gregCoordX + count;
else
gregCoordX = gregCoordX - count;
}
@Override
public void onClick(View arg0) {
dropBall();
}
}
提前谢谢,我已经被困了好几天了
本
然后,
触摸x、触摸y是在屏幕上单击的点的坐标。你可以比较格雷格坐标和这些坐标。如果相同,那么做你想做的。我为格式错误道歉,这是我的第一篇帖子:)我认为你的代码不起作用,因为精灵视图不是MyAnimationView的子视图。onClick侦听器不会启动,因为从未使用过视图-您只需读取它们的位图并在onDraw中绘制它们。我会将此作为一个答案发布,但这是问题,而不是解决方案。看看这个解决方案:谢谢你的帮助,我会尝试一下:)
public class CustomView extends View implements OnClickListener {
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context) {
super(context);
init();
}
private void init(){
setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Do whatever you need to
}
}
float touched_x, touched_y;
boolean touched = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
touchCounter++;
touched_x = event.getX();
touched_y = event.getY();
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
touched = true;
break;
case MotionEvent.ACTION_MOVE:
touched = true;
break;
case MotionEvent.ACTION_UP:
touched = false;
break;
case MotionEvent.ACTION_CANCEL:
touched = false;
break;
case MotionEvent.ACTION_OUTSIDE:
touched = false;
break;
default:
}
return true; // processed
}
if (touched) {
//control here
}
public class CustomView extends View implements OnClickListener {
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context) {
super(context);
init();
}
private void init(){
setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Do whatever you need to
}
}