如何在旋转时停止更改位图大小?

时间:2012-03-02 05:47:13

标签: android image matrix bitmap

我想在我的应用中旋转位图。当我要旋转位图时,位图大小也发生了变化。请帮帮我..

我尝试了以下方法,但失败了:

1st Method:




public Bitmap rotate(Bitmap bitmapOrg) {
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();

        Matrix matrix = new Matrix();
        matrix.postRotate(45);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
        bitmapOrg = resizedBitmap;
        ImageView imageView = new ImageView(mContext);
        imageView.setImageDrawable(bmd);
        imageView.setScaleType(ScaleType.CENTER);

        bitmapOrg=((BitmapDrawable)imageView.getDrawable()).getBitmap();
        return bitmapOrg;

    }

    float totalRotated = 0;

    public Bitmap rotate(Bitmap mBitmap,float degrees){
            // compute the absolute rotation
            totalRotated = (totalRotated + degrees) % 360;
            // precompute some trig functions
            double radians = Math.toRadians(totalRotated);
            double sin = Math.abs(Math.sin(radians));
            double cos = Math.abs(Math.cos(radians));
            // figure out total width and height of new bitmap
            int newWidth = (int) (mBitmap.getWidth() * cos + mBitmap.getHeight() * sin);
            int newHeight =  (int) (mBitmap.getWidth() * sin + mBitmap.getHeight() * cos);
            // set up matrix
            matrix.reset();
            matrix.postTranslate((newWidth - mBitmap.getWidth()) / 2, (newHeight - mBitmap.getHeight()) / 2);
            matrix.setRotate(totalRotated);

            // create new bitmap by rotating mBitmap
         Bitmap   rotated = Bitmap.createBitmap(mBitmap, 0, 0,newWidth, newHeight, matrix, true);
        return rotated;
    }

0 个答案:

没有答案