绕备用轴旋转

时间:2014-08-31 08:20:31

标签: android

我正在努力进行位图旋转。我希望围绕一个替代轴旋转一个图形但是我只能让它围绕中心点旋转,无论我做什么,放入postTranlate。 preTranslate,设置Translate以任何顺序不起作用我也尝试了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);

由于

4 个答案:

答案 0 :(得分:0)

如果您希望Bitmap使用pivot(a,b)围绕x轴旋转45度,则可以调用matrix.rotate(45, a, b)

答案 1 :(得分:0)

也许你想使用Camera类围绕X,Y或Z轴旋转:

matrix = new Matrix();

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

答案 2 :(得分:0)

我理解你的问题的方式是你要围绕某个点旋转图像,比如(x, y)。从概念上讲,您需要对图像执行以下转换:

  1. 翻译(-x, -y)
  2. 旋转45度
  3. 翻译(x, y)

答案 3 :(得分:0)

您可以使用此方法从中心旋转对象在精灵类

中创建此方法
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);
}

试试吧