2011-09-04 69 views
2

我正在嘗試在Android上製作一個簡單的遊戲(如Arkanoid)。位圖而不是可繪製

我使用了乒乓球遊戲模式中的一些元素,我在互聯網上找到了它。

所以我試圖改變這個類(GameObject)通過使用Bitmap而不是Drawable,但出現了一些問題。

我有一些問題:我已經來到這裏一樣使用它自己的分辨率場這樣怎麼能做出這樣的事情使用Bitmap

  1. Rect對象?
  2. 是不是像.getBounds()存在Bitmap
  3. 我可以通過DrawableBitmap製作動畫對象(閃爍,改變顏色和e.t.c)嗎?

這裏是我的代碼:

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

回答

2

要獲取位圖的尺寸,請使用getWidth()getHeight()方法。

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

從所需尺寸的位圖創建位圖,使用Bitmap

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

您可以使用Android Animation類通過佈局位圖動畫應用的createScaledBitmap方法。在APIDemos應用程序中可用。 (ApiDemos->視圖 - > Animation-> 3D轉換)

+0

謝謝,我會嘗試 – MarkMark

+0

哦,順便說一句,我不明白如何檢查對象路口。我在'Rect'前使用了'Image.getBounds();'但是現在我該怎麼做? – MarkMark

0

您正在尋找BitmapDrawable我想。

+0

哦thx我應該嘗試看起來smth,但在JetBoy遊戲的例子,只是Bitmaps無處不在,所以這就是爲什麼我對位圖感興趣 – MarkMark

1

哦,前幾天我找對象intersetcion的解決方案srot - 此方法只是一個例子實際上還要考慮如何讓你的一些對象類

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