图片加点击事件java_java – 如何为Android自定义视图添加OnClick事件

本文介绍了一个基于Android平台的游戏应用开发案例,通过实现触摸屏幕控制人物移动并触发球体下落的动画效果。文章详细展示了如何使用Sprite类来管理角色与球体的位置,并通过监听点击事件来控制动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我有两个图像在屏幕上移动,一个是球,一个是男人.

我想要发生的是当用户触摸到男人的形象时,球会掉落.

我的问题是我似乎无法添加onclick / ontouch事件并让它工作.

我没有正确实施,有人可以帮忙吗?

我在下面列出了3个课程. Greg是男人,球被命名为球:)

TestAnimationActivity.java

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

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

}

}

MyAnimationView.java

package com.test.firstAnimation;

import android.content.Context;

import android.graphics.Canvas;

import android.view.View;

public class MyAnimationView extends View{

private Sprite greg;

private Sprite ball;

private int xCoOr;

private int ballXCoOr;

public static boolean ballDropping;

public MyAnimationView(Context context) {

super(context);

ballDropping = false;

greg = new Sprite(context,R.drawable.greg);

ball = new Sprite(context,R.drawable.ball);

OnClickListener gregClicked = new OnClickListener() {

public void onClick(View v) {

ballDropping = true;

}

};

greg.setOnClickListener(gregClicked);

}

@Override protected void onDraw(Canvas canvas) {

canvas.drawColor(0xFFFFFFFF); //white background

ballXCoOr = Sprite.getBX();

xCoOr = Sprite.getX();

Sprite.moveGreg(xCoOr); //move ball left or right depending

Sprite.moveBall(ballXCoOr);

if(ballDropping == true)

{

Sprite.dropBall();

}

canvas.drawBitmap(greg.getBitmap(),xCoOr,Sprite.getY(),null);

canvas.drawBitmap(ball.getBitmap(),ballXCoOr,Sprite.getBY(),null);

invalidate();

}

}

提前谢谢,我已经被困了好几天了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值