简体   繁体   English

如何在imageViews数组中的每个imageView周围绘制一个矩形?

[英]How can I draw a rectangle around each imageView in an array of imageViews?

I am making an game in Android Studio. 我正在Android Studio中制作游戏。

I have an array of fish(imageViews). 我有一组鱼(imageViews)。

I need to put a rectangle around each fish in the array to begin implementing collision detection of the fishes. 我需要在数组中的每个鱼周围放置一个矩形,以开始实现鱼的碰撞检测。

In the function below, I create the imageView, add it to my layout, and then move the imageView across the screen. 在下面的函数中,我创建imageView,将其添加到布局中,然后在屏幕上移动imageView。

How do i draw a rectangle around each fish in the array? 如何在数组中的每条鱼周围绘制一个矩形?

I call this function in a runnable every two seconds. 我每两秒钟运行一次此函数。

public void spawnFish(){

        Random r = new Random();

        int low = 170;
        int high = 500;

        int start = r.nextInt(high - low) + low;
        int end = r.nextInt(high - low) + low;

        myImage = new ImageView(getContext());
        myImage.setImageBitmap(resizedFish);
        //drawRectangle around myImage for collision detection
        fragment_main.addView(myImage);
        arrayOfEnemies.add(myImage);

        myImage.setX(950);
        myImage.setY(start);


        TranslateAnimation moveLefttoRight = new TranslateAnimation(myImage.getX(), -width, myImage.getY(), end);
        moveLefttoRight.setDuration(12000);
        moveLefttoRight.setFillAfter(true);
        myImage.startAnimation(moveLefttoRight);

        Log.d("fishArraySize", Integer.toString(arrayOfEnemies.size()));

    }

Here is the runnable 这是可运行的

    public HUDDrawView(Context context){super(context);
    initMyView();
    }

    public void initMyView(){

        spawnHandler = new Handler();
        SpawnEnemies = new Runnable(){
            public void run(){
                //Log.d("2seconds", "spawnfish");
                spawnFish();
                spawnHandler.postDelayed(this, 2000);

            }
        };

        spawnHandler.post(SpawnEnemies);

} }

If you mind me say so, using imageviews for your game in android might be a bad idea for different reasons, those including: 如果您介意的话,出于各种原因,在Android中为游戏使用imageviews可能不是一个好主意,这些原因包括:

  1. Accesibility. 可及性。
  2. Performance. 性能。
  3. Scalability. 可扩展性。

If you want to develop a small Android Game I would recommend you to use SurfaceView and a secondary thread managing all the logic and drawing. 如果您想开发一款小型Android游戏,建议您使用SurfaceView和辅助线程来管理所有逻辑和图形。

Nonetheless if you still wanna go with that, you should take a look at the method getDrawable() from which you can get the image's bounding box for implementing collisions. 但是,如果您仍然想这样做,则应该看一下getDrawable()方法,从中可以获取用于实现碰撞的图像边界框。

Finally if you still let me and want to try another approach I woule encourage you to look any game framework such as Cocos2dx, Starling or Monogame, you won't regret it! 最后,如果您仍然允许我并想尝试另一种方法,我鼓励您查看任何游戏框架,例如Cocos2dx,Starling或Monogame,您将不会后悔!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM