在安卓系统中旋转图片

7
我希望能够将图片顺时针和逆时针旋转。 我已经尝试过但无法同时进行两种方式的旋转。 如果您知道如何解决我的问题,请给予解决方案。 提前感谢。///

在此处查看答案:http://stackoverflow.com/questions/13099652/custom-rotating-of-an-image-view - AYBABTU
5个回答

6
请使用以下代码
public class Rotateimage extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);
          // or just load a resource from the res/drawable directory:
          Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flo);

          // find the width and height of the screen:
          Display d = getWindowManager().getDefaultDisplay();
          int x = d.getWidth();
          int y = d.getHeight();

          // get a reference to the ImageView component that will display the image:
          ImageView img1 = (ImageView)findViewById(R.id.imageView1);

          // scale it to fit the screen, x and y swapped because my image is wider than it is tall
          Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);

          // create a matrix object
          Matrix matrix = new Matrix();

          matrix.postRotate(45, 90, 180);

          // create a new bitmap from the original using the matrix to transform the result
          Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
                // display the rotated bitmap
          img1.setImageBitmap(rotatedBitmap);
}}

4
以下是加载位图并向左或向右旋转的基础代码:
// Load a bitmap from a drawable, make sure this drawable exists in your project
Bitmap sprite = BitmapFactory.decodeResource(this.getResources(),
        R.drawable.ic_launcher);

// Create two matrices that will be used to rotate the bitmap
Matrix rotateRight = new Matrix();
Matrix rotateLeft = new Matrix();

// Set the matrices with the desired rotation 90 or -90 degrees
rotateRight.preRotate(90);
rotateLeft.preRotate(-90);

// Create bitmaps based on the loaded bitmap 'sprite' and apply one of
// the rotation matrices
Bitmap rSprite = Bitmap.createBitmap(sprite, 0, 0,
        sprite.getWidth(), sprite.getHeight(), rotateRight, true);
Bitmap lSprite = Bitmap.createBitmap(sprite, 0, 0,
        sprite.getWidth(), sprite.getHeight(), rotateLeft, true);

现在使用rSprite和lSprite。

这是一个完整的示例,实际将位图绘制到屏幕上:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new drawView(this));
    }

    private class drawView extends View{
        public drawView(Context context){
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            // Load a bitmap from a drawable, make sure this drawable exists in your project
            Bitmap sprite = BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.ic_launcher);

            // Create two matrices that will be used to rotate the bitmap
            Matrix rotateRight = new Matrix();
            Matrix rotateLeft = new Matrix();

            // Set the matrices with the desired rotation 90 or -90 degrees
            rotateRight.preRotate(90);
            rotateLeft.preRotate(-90);

            // Create bitmaps based on the loaded bitmap 'sprite' and apply one of
            // the rotation matrices
            Bitmap rSprite = Bitmap.createBitmap(sprite, 0, 0,
                    sprite.getWidth(), sprite.getHeight(), rotateRight, true);
            Bitmap lSprite = Bitmap.createBitmap(sprite, 0, 0,
                    sprite.getWidth(), sprite.getHeight(), rotateLeft, true);

            //Draw the first unrotated sprite at the top left of the screen
            canvas.drawBitmap(sprite, 0, 0, null);

            //Draw the rotated right sprite on the 2nd row
            canvas.drawBitmap(rSprite, 0, sprite.getHeight() + 5, null);

            //Draw the rotated left sprite on the 3rd row
            canvas.drawBitmap(lSprite, 0, sprite.getHeight() * 2 + 5, null);
        }
    }
}

0

你只需要将这段简单的代码添加到你的按钮中

            imVCature_pic.setRotation(imVCature_pic.getRotation() + 90);

0

使用这段代码,希望它能有所帮助... 你必须编写这个方法来执行旋转操作

public void paintFromCenter(float angle, Canvas c) {
    Bitmap b = sprite;
    Bitmap h = b;
    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);
}

在你的程序中,你必须编写onTouchEvent()方法。
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN) {
        edX = (int) event.getX();
        edY = (int) event.getY();
        if ((edX > objectSprite.getX()
                && edX < objectSprite.getX() + objectSprite.getWidth()
                && edY > objectSprite.getY() && edY < objectSprite.getY()
                + objectSprite.getHeight())) {
        }
    }
    if (action == MotionEvent.ACTION_MOVE) {
        emX = (int) event.getX();
        emY = (int) event.getY();
        if (edX < emX && angle <= 90) {
            update(canvas);
            CHAR = 'D';
        } else if (edX > emX && angle > 0) {
            update(canvas);
            CHAR = 'U';
        }
        if (edY < emY && angle <= 90) {
            update(canvas);
            CHAR = 'L';
        } else if (edY > emY && angle >= 0) {
            update(canvas);
            CHAR = 'R';
        }
    }
    return true;
}

你需要创建一个update()方法,在触摸事件中旋转你的角度,角度可以根据你的选择定义。最初我已经声明了

int angle=1;
public void update(Canvas canvas) {
    switch (CHAR) {
    case 'D':
        angle += 1;
        break;
    case 'U':
        angle -= 1;

        break;
    case 'R':
        angle -= 1;
        break;
    case 'L':
        angle += 1;
        break;
    }

}

现在你需要在draw()方法中像这样绘制你的对象。
public void draw() {
  objectSprite.paintFromCenter(angle, canvas);  
        }

-1
Matrix mat = new Matrix();
mat.preRotate(angle);///in degree
Bitmap mBitmap = Bitmap.createBitmap(originalBmp, 0, 0, modWidth, modHeight, mat, true);
        //originalBmp -> original img as bitmap
         //modHeight -> new height
        //modWidth -> new width

请使用上述代码。

你的回答并不是这个问题的解决方案,它只是指导如何使用矩阵旋转图像。 - secretlm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接