位图代替 Drawable

发布于 2024-12-03 09:57:11 字数 3395 浏览 0 评论 0原文

我正在尝试在 Android 上制作一个简单的游戏(例如 Arkanoid)。

我使用了在互联网上找到的乒乓球游戏模式中的一些元素。

因此,我尝试使用 Bitmap 而不是 Drawable 来更改此类(GameObject),但出现了一些问题。

我有一些问题:

  1. 我在这里得到的 Rect 对象就像具有自己分辨率的字段一样使用,那么如何使用 Bitmap 制作这样的东西?
  2. Bitmap 是否存在类似 .getBounds() 的东西?
  3. 我可以使用 DrawableBitmap 以某种方式制作动画对象(闪烁、改变颜色等)吗?

这是我的代码:

package project.java.game.objects;

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public abstract class GameObject {

    // Directions
    public final static int DIR_LEFT = -1;
    public final static int DIR_RIGHT = 1;
    public final static int DIR_NONE = 0;
    public final static int DIR_UP = 1;
    public final static int DIR_DOWN = -1;


    /** Higer border of object */
    public int getTop() { return mPoint.y; }

    /** Lower border */
    public int getBottom() { return mPoint.y + mHeight; }

    /** Left border */
    public int getLeft() { return mPoint.x; }

    /** right border */
    public int getRight() { return mPoint.x + mWidth; }

    public int returnCenter(){ return mPoint.x + mWidth/2; }

    /** Central point */
    public Point getCenter() { return new Point(mPoint.x + mWidth / 2, mPoint.y + mHeight / 2); }

    /** Height of object */
    public int getHeight() { return mHeight; }

    /** Width */
    public int getWidth() { return mWidth; }

    /** @return Recatngle, which is limit objects */
    public Rect getRect() { 
        return mImage.getBounds(); 
    }

    /** for intersection */
    public static boolean intersects(GameObject obj1, GameObject obj2)
    {
        return Rect.intersects(obj1.getRect(), obj2.getRect());
    }


    /** for coordinates */
    protected Point mPoint;

    /** Height of image */
    protected int mHeight;

    /** Width */
    protected int mWidth;

    /** image */
    private Drawable mImage;

    /** speed */
    protected int mSpeed;

    /**Life level*/
    protected int mLifeLvl;

    /**
     * Constructor
     * @param image image which will use for object
     */
    public GameObject(Drawable image)
    {
        mImage = image;
        mPoint = new Point(0, 0);
        mWidth = image.getIntrinsicWidth();
        mHeight = image.getIntrinsicHeight();
    }

    /** point change position  */
    protected abstract void updatePoint();

    /** object change position */
    public void update()
    {
        updatePoint();
        mImage.setBounds(mPoint.x, mPoint.y, mPoint.x + mWidth, mPoint.y + mHeight);
    }

    /**to draw object */
    public void draw(Canvas canvas)
    {
        mImage.draw(canvas);
    }
    /** set Left bound */
    public void setLeft(int value) { mPoint.x = value; }

    /** set Right bound  */
    public void setRight(int value) { mPoint.x = value - mWidth; }

    /** set top bound */
    public void setTop(int value) { mPoint.y = value; }

    /** set Lower bound */
    public void setBottom(int value) { mPoint.y = value - mHeight; }

    /** center of object on OX */
    public void setCenterX(int value) { mPoint.x = value - mHeight / 2; }

    /** center of object on OY */
    public void setCenterY(int value) { mPoint.y = value - mWidth / 2; }
}

I'm trying to make a simple game (such as Arkanoid) on Android.

I used some elements from ping-pong-game pattern which I found on the internet.

So I was trying to change this class (GameObject) by using Bitmap instead of Drawable, but some problems appeared.

I have got some questions:

  1. The Rect object which I've got here used like field with it own resolution so how can make something like this using Bitmap?
  2. Is something like .getBounds() exist for Bitmap?
  3. Can I make animated objects (blinking, change colour and e.t.c) somehow with Drawable or Bitmap is better for animation?

Here's my code:

package project.java.game.objects;

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public abstract class GameObject {

    // Directions
    public final static int DIR_LEFT = -1;
    public final static int DIR_RIGHT = 1;
    public final static int DIR_NONE = 0;
    public final static int DIR_UP = 1;
    public final static int DIR_DOWN = -1;


    /** Higer border of object */
    public int getTop() { return mPoint.y; }

    /** Lower border */
    public int getBottom() { return mPoint.y + mHeight; }

    /** Left border */
    public int getLeft() { return mPoint.x; }

    /** right border */
    public int getRight() { return mPoint.x + mWidth; }

    public int returnCenter(){ return mPoint.x + mWidth/2; }

    /** Central point */
    public Point getCenter() { return new Point(mPoint.x + mWidth / 2, mPoint.y + mHeight / 2); }

    /** Height of object */
    public int getHeight() { return mHeight; }

    /** Width */
    public int getWidth() { return mWidth; }

    /** @return Recatngle, which is limit objects */
    public Rect getRect() { 
        return mImage.getBounds(); 
    }

    /** for intersection */
    public static boolean intersects(GameObject obj1, GameObject obj2)
    {
        return Rect.intersects(obj1.getRect(), obj2.getRect());
    }


    /** for coordinates */
    protected Point mPoint;

    /** Height of image */
    protected int mHeight;

    /** Width */
    protected int mWidth;

    /** image */
    private Drawable mImage;

    /** speed */
    protected int mSpeed;

    /**Life level*/
    protected int mLifeLvl;

    /**
     * Constructor
     * @param image image which will use for object
     */
    public GameObject(Drawable image)
    {
        mImage = image;
        mPoint = new Point(0, 0);
        mWidth = image.getIntrinsicWidth();
        mHeight = image.getIntrinsicHeight();
    }

    /** point change position  */
    protected abstract void updatePoint();

    /** object change position */
    public void update()
    {
        updatePoint();
        mImage.setBounds(mPoint.x, mPoint.y, mPoint.x + mWidth, mPoint.y + mHeight);
    }

    /**to draw object */
    public void draw(Canvas canvas)
    {
        mImage.draw(canvas);
    }
    /** set Left bound */
    public void setLeft(int value) { mPoint.x = value; }

    /** set Right bound  */
    public void setRight(int value) { mPoint.x = value - mWidth; }

    /** set top bound */
    public void setTop(int value) { mPoint.y = value; }

    /** set Lower bound */
    public void setBottom(int value) { mPoint.y = value - mHeight; }

    /** center of object on OX */
    public void setCenterX(int value) { mPoint.x = value - mHeight / 2; }

    /** center of object on OY */
    public void setCenterY(int value) { mPoint.y = value - mWidth / 2; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-12-10 09:57:11

要获取位图的尺寸,请使用 getWidth()getHeight() 方法。

Bitmap b = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
int width = b.getWidth();
int height = b.getHeight();  

要从所需尺寸的位图创建位图,请使用 Bitmap 类的 createScaledBitmap 方法

Bitmap newBitmap  = Bitmap.createScaledBitmap(b, width, height, false);

您可以使用 Android Animation 类在其上应用动画位图通过布局。在 APIDemos 应用程序中可用。 (ApiDemos->视图->动画->3D 过渡)

To get bitmap's dimension use the getWidth() and getHeight() methods.

Bitmap b = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
int width = b.getWidth();
int height = b.getHeight();  

To create bitmap from a bitmap of desired dimension, use the createScaledBitmap method of the Bitmap class

Bitmap newBitmap  = Bitmap.createScaledBitmap(b, width, height, false);

You can use the Android Animation class to apply animation on bitmap through layout. Available in the APIDemos application. (ApiDemos->Views->Animation->3D Transition)

萌能量女王 2024-12-10 09:57:11

哦,几天前,我找到了对象交叉的解决方案 - 这里的方法只是一个例子,取决于你如何创建一些对象类

private boolean checkCollision(Someobject first, Someobject second) {
    boolean retValue = false;
    int width = first.getBitmap().getWidth();//get bitmap from ur object then get 
    int height = first.getBitmap().getHeight();//it size
    int firstXRangeStart = first.getCoordinates().getX();//get coordinates
    int firstXRangeEnd = firstXRangeStart + width;
    int firstYRangeStart = first.getCoordinates().getY();
    int firstYRangeEnd = firstYRangeStart + height;
    int secondXRangeStart = second.getCoordinates().getX();
    int secondXRangeEnd = secondXRangeStart + width;
    int secondYRangeStart = second.getCoordinates().getY();
    int secondYRangeEnd = secondYRangeStart + height;
    if ((secondXRangeStart >= firstXRangeStart && secondXRangeStart <= firstXRangeEnd)
        || (secondXRangeEnd >= firstXRangeStart && secondXRangeEnd <= firstXRangeEnd)) {
        if ((secondYRangeStart >= firstYRangeStart && secondYRangeStart <= firstYRangeEnd)
            || (secondYRangeEnd >= firstYRangeStart && secondYRangeEnd <= firstYRangeEnd)) {
            retValue = true;
        }
    }
    return retValue;
}Bitmap

Oh few days ago I find srot of solution for objects intersetcion - methods here just an example depens on how you make your some object class

private boolean checkCollision(Someobject first, Someobject second) {
    boolean retValue = false;
    int width = first.getBitmap().getWidth();//get bitmap from ur object then get 
    int height = first.getBitmap().getHeight();//it size
    int firstXRangeStart = first.getCoordinates().getX();//get coordinates
    int firstXRangeEnd = firstXRangeStart + width;
    int firstYRangeStart = first.getCoordinates().getY();
    int firstYRangeEnd = firstYRangeStart + height;
    int secondXRangeStart = second.getCoordinates().getX();
    int secondXRangeEnd = secondXRangeStart + width;
    int secondYRangeStart = second.getCoordinates().getY();
    int secondYRangeEnd = secondYRangeStart + height;
    if ((secondXRangeStart >= firstXRangeStart && secondXRangeStart <= firstXRangeEnd)
        || (secondXRangeEnd >= firstXRangeStart && secondXRangeEnd <= firstXRangeEnd)) {
        if ((secondYRangeStart >= firstYRangeStart && secondYRangeStart <= firstYRangeEnd)
            || (secondYRangeEnd >= firstYRangeStart && secondYRangeEnd <= firstYRangeEnd)) {
            retValue = true;
        }
    }
    return retValue;
}Bitmap
我做我的改变 2024-12-10 09:57:11

我认为您正在寻找 BitmapDrawable

You are looking for BitmapDrawable I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文