【问题标题】:Rotate image in android在android中旋转图像
【发布时间】:2011-03-22 10:08:22
【问题描述】:

我想以顺时针和逆时针两种方式旋转图像。 我试过但没有同时旋转图像, 所以如果你知道的话,请给我解决我的问题..

在此先感谢..................///

【问题讨论】:

标签: android


【解决方案1】:

使用下面的代码

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);
}}

【讨论】:

    【解决方案2】:

    这是加载位图并左右旋转的基本代码:

    // 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);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用此代码希望它会有所帮助... 你必须写这个方法才能做旋转操作

      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);  
              }
      

      【讨论】:

        【解决方案4】:

        您只需将这个简单的代码添加到您的按钮

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

        【讨论】:

          【解决方案5】:
          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
          

          使用上面的代码。

          【讨论】:

          • 您的回答不能解决这个问题。它只是指导如何使用 Matrix 旋转图像。
          猜你喜欢
          • 1970-01-01
          • 2015-11-04
          • 1970-01-01
          • 2011-07-20
          • 1970-01-01
          • 2021-01-03
          • 1970-01-01
          • 2022-01-10
          • 2018-08-24
          相关资源
          最近更新 更多