简体   繁体   English

如何在不更改位置的情况下旋转位图

[英]how to rotate bitmap without changing position

I have to rotate a bitmap of 180°. 我必须旋转180°的位图。 The problem is, that the bitmap is rotated but the position is different. 问题是位图已旋转,但位置不同。

Without rotation it looks like this: 没有旋转,它看起来像这样: 在此处输入图片说明

With rotation it looks like this: 旋转后看起来像这样: 在此处输入图片说明

Here is my code: 这是我的代码:

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

I changed my code to following, because px and py was 0 but changed nothing: 我将代码更改为以下代码,因为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;
        }
    });


}

Thanks a lot 非常感谢

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM