studio rotate rotar imagen android matrix bitmap center image-rotation

rotate - Android: Cómo rotar un mapa de bits en un punto central



rotar imagen android studio (8)

He estado buscando más de un día para encontrar una solución a este problema, pero nada ayuda, incluso las respuestas aquí. La documentación no explica nada también.

Simplemente estoy tratando de obtener una rotación en la dirección de otro objeto. El problema es que el mapa de bits no se rota alrededor de un punto fijo, sino alrededor de los mapas de bits (0,0).

Este es el código con el que estoy teniendo problemas:

Matrix mtx = new Matrix(); mtx.reset(); mtx.preTranslate(-centerX, -centerY); mtx.setRotate((float)direction, -centerX, -centerY); mtx.postTranslate(pivotX, pivotY); Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, spriteWidth, spriteHeight, mtx, true); this.bitmap = rotatedBMP;

Lo extraño es que no importa cómo cambie los valores dentro de pre / postTranslate() y los argumentos de flotación en setRotation() . ¿Alguien puede por favor ayudarme y empujarme en la dirección correcta? :)


Espero que la siguiente secuencia de código te ayude:

Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config); Canvas canvas = new Canvas(targetBitmap); Matrix matrix = new Matrix(); matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2); canvas.drawBitmap(source, matrix, new Paint());

Si comprueba el siguiente método desde ~frameworks/base/graphics/java/android/graphics/Bitmap.java

public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

esto explicaría lo que hace con la rotación y la traducción.



Puedes usar algo como lo siguiente:

Matrix matrix = new Matrix(); matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2); RectF rectF = new RectF(0, 0, source.getWidth(), source.getHeight()); matrix.mapRect(rectF); Bitmap targetBitmap = Bitmap.createBitmap(rectF.width(), rectF.height(), config); Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(source, matrix, new Paint());


También puede rotar ImageView utilizando una RotateAnimation :

RotateAnimation rotateAnimation = new RotateAnimation(from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setInterpolator(new LinearInterpolator()); rotateAnimation.setDuration(ANIMATION_DURATION); rotateAnimation.setFillAfter(true); imageView.startAnimation(rotateAnimation);


Utilicé estas configuraciones y todavía tengo el problema de la pixelización:

Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.map_pin); Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()), (bmpOriginal.getHeight()), Bitmap.Config.ARGB_8888); Paint p = new Paint(); p.setAntiAlias(true); Matrix matrix = new Matrix(); matrix.setRotate((float) lock.getDirection(),(float) (bmpOriginal.getWidth()/2), (float)(bmpOriginal.getHeight()/2)); RectF rectF = new RectF(0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight()); matrix.mapRect(rectF); targetBitmap = Bitmap.createBitmap((int)rectF.width(), (int)rectF.height(), Bitmap.Config.ARGB_8888); Canvas tempCanvas = new Canvas(targetBitmap); tempCanvas.drawBitmap(bmpOriginal, matrix, p);


Volví a este problema ahora que estamos finalizando el juego y solo pensé en publicar lo que funcionó para mí.

Este es el método para rotar la Matriz:

this.matrix.reset(); this.matrix.setTranslate(this.floatXpos, this.floatYpos); this.matrix.postRotate((float)this.direction, this.getCenterX(), this.getCenterY());

( this.getCenterX() es básicamente la posición X de los mapas de bits + el ancho de los mapas de bits / 2)

Y el método para dibujar el mapa de bits (llamado a través de una clase RenderManager ):

canvas.drawBitmap(this.bitmap, this.matrix, null);

Por lo tanto, es muy sencillo, pero me resulta extraño que no pueda hacer que funcione con setRotate seguido de postTranslate . Tal vez algunos saben por qué esto no funciona? Ahora todos los mapas de bits giran correctamente, pero no sin una pequeña disminución en la calidad del mapa de bits: /

¡De todas formas, gracias por su ayuda!


Editado : código optimizado.

public static Bitmap RotateBitmap(Bitmap source, float angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); }

Para obtener Bitmap de los recursos:

Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);


matrix.reset(); matrix.setTranslate( anchor.x, anchor.y ); matrix.postRotate((float) rotation , 0,0); matrix.postTranslate(positionOfAnchor.x, positionOfAnchor.x); c.drawBitmap(bitmap, matrix, null);