【问题标题】:While rotating an image its changing the actual position of the image旋转图像时,它会改变图像的实际位置
【发布时间】:2012-08-03 05:17:17
【问题描述】:

我尝试使用 Matrix 旋转位图,如果角度为 90 度而不是 90 度,则位图位置正在改变。

代码片段:

            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap map= Bitmap.createBitmap(resizedBitmap, 0, 0,resizedBitmap.getWidth(), resizedBitmap.getHeight(),matrix, true);

你们能告诉我如何在不改变图像位置的情况下旋转图像吗?

谢谢。

【问题讨论】:

    标签: android matrix bitmap image-rotation


    【解决方案1】:

    您好,如果您使用的是 ImageView,我建议您为 ImageView 设置矩阵,而不是每次都创建新的位图

                  Matrix matrix=new Matrix();
         img.setScaleType(ScaleType.MATRIX);   
         int height =  img.arrow.getHeight();
            int width =  img.arrow.getWidth();
        matrix.postRotate((float) angle, width/2,height/2);
         img.setImageMatrix(matrix);
    

    【讨论】:

    • 我在这里没有使用图像视图,我创建了扩展视图对象的自定义视图,所以我只能在这里使用位图。
    • 你可以试试这个代码 matrix.postRotate((float) angle, this.getWidth()/2,this.getHeight()/2);我不确定它是否正常工作,只是猜测它适用于 ImageView
    【解决方案2】:

    这是唯一一个为我工作的人

    private Bitmap rotateBitmap(Bitmap bitmap, int rotationAngleDegree){
    
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
    
            int newW=w, newH=h;
            if (rotationAngleDegree==90 || rotationAngleDegree==270){
                newW = h;
                newH = w;
            }
            Bitmap rotatedBitmap = Bitmap.createBitmap(newW,newH, bitmap.getConfig());
            Canvas canvas = new Canvas(rotatedBitmap);
    
            Rect rect = new Rect(0,0,newW, newH);
            Matrix matrix = new Matrix();
            float px = rect.exactCenterX();
            float py = rect.exactCenterY();
            matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2);
            matrix.postRotate(rotationAngleDegree);
            matrix.postTranslate(px, py);
            canvas.drawBitmap(bitmap, matrix, new Paint( Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG ));
            matrix.reset();
    
            return rotatedBitmap;
        }
    

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多