3

I have a custom ImageView. I want to rotate my imageview at some angle. Here is its onDraw() method:

canvas.save();
canvas.rotate(currentAngle,getWidth()/2,getHeight()/2);
super.onDraw(canvas);

The image is rotating but the problem is after rotation, the images is cutting from sides. How to avoid that cutting thing?

Sample Screenshot: enter image description here

Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
  • You can do this through animation or Matrix. Check this link out http://stackoverflow.com/questions/8981845/androidrotate-image-in-imageview-by-an-angle – Gyonder Feb 26 '13 at 14:19

2 Answers2

0

Try for the following code:

   public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);

        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
    }
Karan_Rana
  • 2,813
  • 2
  • 26
  • 35
0

Was once I have a similar problem. I was decided this problem that way. I zoomed image in so that it looked like


|_ __ | | | | | ||_|| |_ __ _|

Half width image added left and right. And half height added top and bottom. New space fill some color, or do alapha canal equals+ 0. When you rotate the image, you needed cut the excess. I did that not on Android, but i hope this can help you.

QArea
  • 4,955
  • 1
  • 12
  • 22