2014-08-31 152 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); 
} 

尝试