2014-08-31 151 views
0

我正在苦於位圖旋轉。我希望圍繞一個替代軸旋轉一個圖形,但無論我做什麼,我都只能讓它圍繞中心點旋轉,放入postTranlate。 preTranslate,以任何順序設置翻譯不起作用我也嘗試了postRotate(45,0,0),但它總是圍繞中心旋轉。 下面的代碼採取互聯網我會做什麼來改變它的旋轉點,下面的代碼使用的是正方形的發射器圖標,我正在使用像箭頭一樣的細長圖形。圍繞替代軸旋轉

// Rotate image to 45 degrees. 

public void RotateImage(){ 
    ImageView image; 
    Bitmap bMap; 
    Matrix matrix; 

    //Get ImageView from layout xml file 
    image = (ImageView) findViewById(R.id.imageView1); 

    //Decode Image using Bitmap factory. 
    bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 

    //Create object of new Matrix. 
    matrix = new Matrix(); 

    //set image rotation value to 45 degrees in matrix. 
    matrix.postRotate(45); 

    //Create bitmap with new values. 
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, 
      bMap.getWidth(), bMap.getHeight(), matrix, true); 

    //put rotated image in ImageView. 
    image.setImageBitmap(bMapRotate); 
} 

我曾嘗試下面的代碼,但它仍然繞中心旋轉,或者至少出現過

public void RotateImage{ 

    ImageView image; 
    Bitmap bMap; 
    Matrix matrix; 

    //Get ImageView from layout xml file 
    image = (ImageView) findViewById(R.id.imageView); 

    //Decode Image using Bitmap factory. 
    bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 

    //Create object of new Matrix. 
    matrix = new Matrix(); 

    //set image rotation value to 45 degrees in matrix. 
    matrix.setTranslate(-100,-200); 
    matrix.postRotate(angle); 
    matrix.postTranslate(100,200); 
    //Create bitmap with new values. 
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, 
      bMap.getWidth(), bMap.getHeight(), matrix, true); 

    //put rotated image in ImageView. 
    image.setImageBitmap(bMapRotate); 

感謝

回答

0

如果你希望你的位圖旋轉45度繞x軸與(a,b),您可以撥打matrix.rotate(45, a, b)

+0

矩陣不會出現有一個旋轉的方法我猜你可能意味着postRotate (45,a,b)仍然看起來圍繞中心旋轉,文檔確實表明這可以工作,但是我總是出現在中間我猜這個視圖可能會重新調整它,但不能解決它。 – user3129787 2014-08-31 08:36:59

+0

@ user3129787你能提供你正在使用的示例值嗎?同時發佈a和b各種值的輸出屏幕截圖。 – 2014-08-31 08:55:56

0

也許您想使用Camera類繞X,Y或Z軸旋轉:

matrix = new Matrix(); 

Camera camera = new Camera(); 
camera.save(); 
camera.rotateX(45f); 
camera.getMatrix(matrix); 
camera.restore(); 
+0

你指的是哪個'攝像頭類? – 2014-08-31 08:41:53

+0

android.graphics.Camera – Simas 2014-08-31 08:42:51

+0

我不認爲這回答了OP。我將問題看作是詢問如何將圖像旋轉到除圖像中心以外的另一點。 – 2014-08-31 08:43:47

0

我理解你的問題的方式是,你想旋轉一些點的圖像,說(x, y)。從概念上講,你需要對圖像進行下列轉換:

  1. 45度翻譯由(-x, -y)
  2. 旋轉
  3. 翻譯由(x, y)
+0

它與旋轉點旋轉有什麼不同? – pskink 2014-08-31 08:52:59

+0

@pskink它是一樣的。這是'postRotate()'的文檔是什麼意思?海事組織,這些文件是神祕的,或者使用我不熟悉的數學符號。 – 2014-08-31 08:54:46

+0

我已經添加了下面的內容,但它保持居中我不真正瞭解PRE,Post,Set前綴,所以只是猜測,但它仍然圍繞中心旋轉 matrix.setTranslate(-100,-200); 矩陣。postRotate(角度); matrix.postTranslate(100,200); – user3129787 2014-08-31 08:55:47

0

您可以使用此方法來旋轉從對象中心在sprite類中創建此方法

public void paintFromCenter(float angle, Canvas c) { 
    Bitmap b = sprite; 
    Bitmap h = b; 
    // Canvas canvas = new Canvas(a); 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle, h.getWidth()/2, h.getHeight()); 
    matrix.postTranslate(getX(), getY()); 
    // canvas.drawBitmap(bitmap, matrix, new Paint()); 
    Bitmap bmp2 = Bitmap.createBitmap(h, 0, 0, frameWidth, frameHeight, 
      matrix, true); 
    c.drawBitmap(h, matrix, null); 
    // g.getCanvas().drawBitmap(bmp2, getX(), getY(), null); 
} 

現在的onTouchEvent()

public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_MOVE) { 

     evX = (int) event.getX(); 
     evY = (int) event.getY(); 
     int initX = objectSprite.getX(); 
     int inity = objectSprite.getY(); 

     if ((evX > objectSprite.getX() 
       && evX < objectSprite.getX() + objectSprite.getWidth() 
       && evY > objectSprite.getY() && evY < objectSprite.getY() 
       + objectSprite.getHeight())) { 
      if (angle < 90) { 
       angle += 5; 
      } else if (angle < -180) 
       angle -= 5; 

     } 
} 

    return true; 
} 

在draw()方法繪製圖像/對象

private void draw(Canvas canvas) { 
objectSprite.paintFromCenter(angle, canvas); 
} 

嘗試