2010-04-08 32 views
1

我正在嘗試在Android中旋轉和調整圖像大小。我的代碼如下,(從this教程獲取):調整大小和旋轉Android中的圖像

UPDATE:更多添加以下代碼更具建設性的反饋

 public class AnimatedSprite { 
     private Bitmap bitmap; 
     private double posX, posY; 
     private double velocityX, velocityY; 
     private int width, height; 
     private int numFrames, frameRate, curFrame; 
     private int startFrame, endFrame; 
     private Rect dstRect, srcRect; 
     private long lastTime, lastFrameTime; 
     private boolean looping; 
     private enum SpaceshipState {ALIVE, EXPLODE, DEAD}; 
     public SpaceshipState curState; 
     private int respawnTime; 


     public AnimatedSprite(Context context, int id, int frames, int fps) { 
      this.bitmap = BitmapFactory.decodeResource(context.getResources(),id); 
      this.posX = 0; 
      this.posY = 0; 
      this.velocityX = 0; 
      this.velocityY = 0; 
      this.numFrames = frames; 
      this.frameRate = fps; 
      this.width = this.bitmap.getWidth()/frames; 
      this.height = this.bitmap.getHeight(); 
      this.dstRect = new Rect((int)this.posX,(int)this.posY,(int)this.posX+this.width,(int)this.posY+this.height); 
      this.startFrame = 0; 
      this.endFrame = frames-1; 
      this.looping = true; 
      this.lastFrameTime = 0; 
      this.respawnTime = 0; 
      this.setFrameRect(this.startFrame); 
     } 
      . 
      . 
      //Other methods 
      . 
      . 

     //Returns a rotated copy of the AnimatedSprite a 
    public AnimatedSprite rotateSprite(AnimatedSprite originalSprite, float angle) 
    { 
       AnimatedSprite rotatedSprite = originalSprite; 

      int orgHeight = originalSprite.bitmap.getHeight(); 
      int orgWidth = originalSprite.bitmap.getWidth(); 

      //Create manipulation matrix 
      Matrix m = new Matrix(); 
      // resize the bit map 
      m.postScale(.25f, .25f); 
      // rotate the Bitmap by the given angle 
        //Static angle for testing 
      m.postRotate(180); 
      //Rotated bitmap 
      Bitmap rotatedBitmap = Bitmap.createBitmap(originalSprite.bitmap, 0, 0, 
        orgWidth, orgHeight, m, true); 
      //Set the new sprite bitmap to the rotated bitmap and return 
      rotatedSprite.bitmap = rotatedBitmap; 

      return rotatedBitmap; 
     } 

} 

的形象似乎轉動得很好,但它不適合就像我期望的那樣。我已經嘗試了0和.99之間的不同值,但圖像只是在縮小比例值時獲得更多像素化,但它在視覺上保持相同的大小;而我試圖實現的目標是實際縮小視覺。我不知道該怎麼做,任何幫助表示讚賞。

回答

1

你正在做的是從位圖操縱像素數據。您正在減少圖像中的像素數量,但聽起來像(因爲我們無法看到此代碼),就好像位圖被縮放以適合屏幕上的相同大小。爲了減少屏幕尺寸,您必須調整ImageView上的參數,或者您使用任何技術來顯示圖像。

+0

THanks Jim,我正在使用一個Rect對象來包含我的位圖,有沒有一種方法來旋轉和縮放包含我的位圖的Rect?這樣我就不必亂用像素數據本身。 – kingrichard2005 2010-04-08 07:02:56

+0

絕對有,但我看不到你所有的代碼,這取決於你如何顯示位圖。 – 2010-04-08 08:59:28

+0

再次感謝Jim,基本上我的設置方式是我有一個名爲AnimatedSprite的類,它使用Rect來包含我試圖進行動畫處理的Bitmap對象。我現在正在工作,但我會嘗試發佈一些相關代碼參數以獲得反饋。 – kingrichard2005 2010-04-08 17:54:34