android - 如何在不改变位置的情况下旋转位图

标签 android matrix bitmap imageview

我必须将位图旋转 180°。问题是,位图旋转但位置不同。

没有旋转它看起来像这样: enter image description here

旋转后看起来像这样: enter image description here

这是我的代码:

geste_Bitmap = gesture.toBitmap ( width, height, 100, Color.YELLOW );
                   Bitmap gedrehte_Geste = rotatePicture(geste_Bitmap, width, height, imageView);


                   //  rotatePicture2(geste_Bitmap, imageView);
                     imageView.setImageBitmap(gedrehte_Geste);


private Bitmap rotatePicture(Bitmap bitmapOrg, int width, int height, ImageView imageView){


    int viewWidth = imageView.getMeasuredWidth();
    int viewHeight = imageView.getMeasuredHeight();
    float px = viewWidth/2;
    float py = viewHeight/2;


    Matrix matrix = new Matrix();

    matrix.postTranslate(-bitmapOrg.getWidth() / 2, -bitmapOrg.getHeight() / 2);
    matrix.postRotate(180);
    matrix.postTranslate(px, py);



    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg,width,height,true);

    Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);


    return rotatedBitmap;
}

我将我的代码更改为以下内容,因为 pxpy 为 0 但未更改任何内容:

private void rotatePicture(final Bitmap bitmapOrg, final int width, final int height, final ImageView imageView){

    ViewTreeObserver vto = imageView.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            int viewWidth = imageView.getMeasuredWidth();
            int viewHeight = imageView.getMeasuredHeight();
            float px = viewWidth / 2;
            float py = viewHeight / 2;

            Log.d("px", "px " + px + "|" + py + bitmapOrg.getWidth());

            Matrix matrix = new Matrix();

            matrix.postTranslate(-px, -py);
            matrix.postTranslate(-bitmapOrg.getWidth() / 2, -bitmapOrg.getHeight() / 2);
            matrix.postRotate(180);
            matrix.postTranslate(bitmapOrg.getWidth() / 2, bitmapOrg.getHeight() / 2);
            matrix.postTranslate(px, py);



            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg,width,height,true);

            Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

            imageView.setImageBitmap(rotatedBitmap);
            return true;
        }
    });


}

非常感谢

最佳答案

试试这个:

Matrix matrix = new Matrix();
matrix.postRotate(180, bitmapOrg.getWidth() / 2, bitmapOrg.getHeight() / 2);
matrix.postTranslate(px, py);

关于android - 如何在不改变位置的情况下旋转位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199346/

相关文章:

android - doInBackground 中的新 LazyAdapter

python - Python 中(大)1554 x 1554 矩阵的行列式

c++ - C/C++错误:超出范围读取。寻找错误的内存地址

java - map 位置更改监听器未触发

c - 在c中访问动态矩阵

android - 在 Canvas 中绘制时如何调整位图大小?

android - 在 native 代码中捕获信号 (SIGSEGV) 后从 JNI 回调到 Java Android 应用程序代码

android - 在 Android Canvas 多边形上单击

Android - ActionbarSherlock 异常崩溃

c - 传递 2 个未知大小的矩阵作为参数