Java 图像视图之间的碰撞检测
我需要帮助使两个ImageView发生冲突,我在这个网站和许多youtube视频上四处查看,认为我找到了解决问题的一个方法。我从另一个人的帖子里找到了一些代码 我只是想知道我应该把代码放在我的程序中的什么地方,因为当它在底部时,我会尝试记录.d,以显示我是否成功地检测到ImageView是否发生冲突而什么都没有显示。总之,这是我的代码,我从另一个问题中使用的代码在最底部,用作注释。非常感谢你的帮助,如果你帮助了我,谢谢你 Main.javaJava 图像视图之间的碰撞检测,java,android,collision-detection,collision,Java,Android,Collision Detection,Collision,我需要帮助使两个ImageView发生冲突,我在这个网站和许多youtube视频上四处查看,认为我找到了解决问题的一个方法。我从另一个人的帖子里找到了一些代码 我只是想知道我应该把代码放在我的程序中的什么地方,因为当它在底部时,我会尝试记录.d,以显示我是否成功地检测到ImageView是否发生冲突而什么都没有显示。总之,这是我的代码,我从另一个问题中使用的代码在最底部,用作注释。非常感谢你的帮助,如果你帮助了我,谢谢你 Main.java package com.example.admin.
package com.example.admin.basketball;
import android.graphics.Point;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//Layout
private RelativeLayout myLayout = null;
//Screen Size
private int screenWidth;
private int screenHeight;
//Position
private float ballDownY;
private float ballDownX;
//Initialize Class
private Handler handler = new Handler();
private Timer timer = new Timer();
//Images
private ImageView net = null;
private ImageView ball = null;
//for net movement along x-axis
float x;
float y;
//points
private int points = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLayout = (RelativeLayout) findViewById(R.id.myLayout);
//score
final TextView score = (TextView) findViewById(R.id.score);
//imageviews
net = (ImageView) findViewById(R.id.net);
ball = (ImageView) findViewById(R.id.ball);
//retrieving screen size
WindowManager wm = getWindowManager();
Display disp = wm.getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
//move to out of screen
ball.setX(-80.0f);
ball.setY(screenHeight + 80.0f);
//start timer
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
changePos();
}
});
}
}, 0, 20);
}
public void changePos() {
//down
ballDownY += 10;
if (ball.getY() > screenHeight) {
ballDownX = (float) Math.floor((Math.random() * (screenWidth -
ball.getWidth())));
ballDownY = -100.0f;
}
ball.setY(ballDownY);
ball.setX(ballDownX);
//make net follow finger
myLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
MainActivity.this.x = event.getX();
y = event.getY();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
net.setX(MainActivity.this.x);
net.setY(y);
}
return true;
}
});
}
}
/*
private boolean viewsOverlap(ImageView net, ImageView ball) {
int[] net_coords = new int[2];
net.getLocationOnScreen(net_coords);
int net_w = net.getWidth();
int net_h = net.getHeight();
Rect net_rect = new Rect(net_coords[0], net_coords[1], net_coords[0] +
net_w, net_coords[1] + net_h);
int[] ball_coords = new int[2];
ball.getLocationOnScreen(ball_coords);
int ball_w = ball.getWidth();
int ball_h = ball.getHeight();
Rect ball_rect = new Rect(ball_coords[0], ball_coords[1], ball_coords[0]
+ ball_w, ball_coords[1] + ball_h);
return net_rect.intersect(ball_rect) || net_rect.contains(ball_rect) ||
ball_rect.contains(net_rect);
}*/
让我给你一个例子,我是如何在10行代码中实现工作冲突检测的。这不是完全相同的问题,但它可以让您了解如何基于坐标操纵对象
// update the canvas in order to display the game action
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int xx = 200;
int yy = 0;
if (persons != null) {
synchronized (persons) {
Iterator<Person> iterate = persons.iterator();
while (iterate.hasNext()) {
Person p = iterate.next();
if (p.getImage() != 0) {
bitmap = BitmapFactory.decodeResource(getResources(), p.getImage()); //load a character image
// Draw the visible person's appearance
if(xx > canvas.getWidth())
xx = 0;
canvas.drawBitmap(bitmap, xx , canvas.getHeight()- bitmap.getHeight() , null);
// Draw the name
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
canvas.save();
paint.setStrokeWidth(1);
paint.setColor(Color.WHITE);
paint.setTextSize(50);
canvas.drawText(p.name, (float)(xx+0.25*bitmap.getWidth()), (float) (canvas.getHeight() ), paint);
xx += bitmap.getWidth()*0.75;
}
}
}
}
canvas.save(); //Save the position of the canvas.
canvas.restore();
//Call the next frame.
invalidate();
}
}
//更新画布以显示游戏动作
@凌驾
公共空白onDraw(画布){
super.onDraw(帆布);
int xx=200;
int-yy=0;
如果(人!=null){
(人){
迭代器迭代器=persons.Iterator();
while(iterate.hasNext()){
Person p=iterate.next();
如果(p.getImage()!=0){
bitmap=BitmapFactory.decodeResource(getResources(),p.getImage());//加载字符图像
//绘制可见的人的外观
如果(xx>canvas.getWidth())
xx=0;
drawBitmap(位图,xx,canvas.getHeight()-bitmap.getHeight(),null);
//画名字
油漆=新油漆();
绘制.设置样式(绘制.样式.填充);
canvas.save();
油漆。设置行程宽度(1);
油漆。设置颜色(颜色。白色);
油漆.尺寸(50);
drawText(p.name,(float)(xx+0.25*bitmap.getWidth()),(float)(canvas.getHeight()),paint);
xx+=bitmap.getWidth()*0.75;
}
}
}
}
canvas.save();//保存画布的位置。
canvas.restore();
//调用下一帧。
使无效();
}
}
在上面的代码中,我只是检查xx
是否与其他图像数组冲突,然后我只是相应地更新xx
。欢迎您使用此代码查看我的 碰撞检测和分数增加;-)
public class MainActivity extends AppCompatActivity
{
//Layout
private RelativeLayout myLayout = null;
//Screen Size
private int screenWidth;
private int screenHeight;
//Position
private float ballDownY;
private float ballDownX;
//Initialize Class
private Handler handler = new Handler();
private Timer timer = new Timer();
//Images
private ImageView net = null;
private ImageView ball = null;
//score
private TextView score = null;
//for net movement along x-axis
public float x = 0;
public float y = 0;
//points
private int points = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
this.myLayout = (RelativeLayout) findViewById(R.id.myLayout);
this.score = (TextView) findViewById(R.id.score);
this.net = (ImageView) findViewById(R.id.net);
this.ball = (ImageView) findViewById(R.id.ball);
//retrieving screen size
WindowManager wm = getWindowManager();
Display disp = wm.getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
//move to out of screen
this.ball.setX(-80.0f);
this.ball.setY(screenHeight + 80.0f);
//Error here
/*//Run constantly
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Render();
}
}, 100); //100 is miliseconds interval than sleep this process, 1000 miliseconds is 1 second*/
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(100);
runOnUiThread(new Runnable() {
@Override
public void run(){Render();}});}
}catch (InterruptedException e) {}}};
t.start();
}
public void Render()
{
changePos();
if(Collision(net, ball))
{
points++; //You dont need findView Textview score for that exists in OnCreate Method
this.score.setText("Score:" + points);
}
}
public void changePos()
{
//down
ballDownY += 10;
if (ball.getY() > screenHeight) {
ballDownX = (float) Math.floor((Math.random() * (screenWidth - ball.getWidth())));
ballDownY = -100.0f;
}
ball.setY(ballDownY);
ball.setX(ballDownX);
//make net follow finger
myLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
x = event.getX();
y = event.getY();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
net.setX(x);
net.setY(y);
}
return true;
}
});
public boolean Collision(ImageView net, ImageView ball)
{
Rect BallRect = new Rect();
ball.getHitRect(BallRect);
Rect NetRect = new Rect();
net.getHitRect(NetRect);
return BallRect.intersect(NetRect);
}
}