Rotate image in android

前端 未结 5 1000
自闭症患者
自闭症患者 2020-12-19 05:48

I want rotate image in both the ways Clockwise as well as Anti clock wise. I had try but not rotate image both the way, so plz give me solution for my problem if you know..

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 06:15

    Here is the base code to load a bitmap and rotate it left or right:

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

    Now go and use rSprite and lSprite.

    Here is a full sample that actually draws the bitmaps to screen:

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

提交回复
热议问题