位图而不是Drawable

时间:2011-09-04 18:13:25

标签: java android bitmap drawable

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

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

所以我尝试使用GameObject代替Bitmap更改此课程(Drawable),但出现了一些问题。

我有一些问题:

  1. 我在这里使用的Rect对象就像字段一样使用它自己的分辨率,那么如何使用Bitmap制作这样的内容?
  2. .getBounds()是否存在Bitmap之类的内容?
  3. 我能以某种方式制作动画对象(闪烁,更改颜色和e.t.c)DrawableBitmap更适合动画吗?
  4. 这是我的代码:

    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; }
    }
    

3 个答案:

答案 0 :(得分:2)

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

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

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

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

我在寻找BitmapDrawable