gpt4 book ai didi

android - 使用 ObjectAnimation 或 TranslateAnimation 进行碰撞检测

转载 作者:行者123 更新时间:2023-11-29 00:25:57 25 4
gpt4 key购买 nike

嗨, friend ,我提供了以下代码,任何人都可以让我知道如何在给定的两个图像/对象之间找到碰撞检测。如果可能,请尽快帮助我。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

img1=(ImageView) findViewById(R.id.imgLeft);
img2=(ImageView) findViewById(R.id.imgRight);

tw=ObjectAnimator.ofFloat(img2,
"translationX", 20, -550f);
tw.setDuration(6000);
tw.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator animation) {
img_one_CurrentX=(int)img2.getY();

}
});
tw.start();


tw_One=ObjectAnimator.ofFloat(img1,
"translationX",0, 550f);
tw_One.setDuration(6000);
tw_One.setTarget(img1);
tw_One.start();
tw_One.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator animation) {
img_two_CurrentX=(int)img1.getY();

if(img_one_CurrentX==img_two_CurrentX ){
Toast.makeText(getApplicationContext(), "Collision", Toast.LENGTH_LONG).show();
}
}
});

}

提前致谢。

最佳答案

嗨, friend 们,经过努力终于找到了一个好的解决方案。这对我来说很完美,我也分享了一些我的代码。

tw_One.addUpdateListener(new AnimatorUpdateListener() {

        @Override
public void onAnimationUpdate(ValueAnimator animation) {

int x1=(int)img1.getX();
int y1=(int)img1.getY();
int width1=img1.getWidth();
int height1=img1.getHeight();
int x2=(int)img2.getX();
int y2=(int)img2.getY();
int width2=img2.getWidth();
int height2=img2.getHeight();
int right1 = x1 + width1;
int right2 = x2 + width2;
int bottom1 = y1 + height1;
int bottom2 = y2 + height2;
if(RunningTest)
{
if (x2 >= x1 && x2 <= right1 && y2 >= y2 && y2 <= bottom1)
{
obj.end();
}

if (right2 >= x1 && right2 <= right1 && bottom2 >= y2 && bottom2 <= bottom1)
{
obj.end();
}


});

关于android - 使用 ObjectAnimation 或 TranslateAnimation 进行碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19448816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com