Dibujable Rotación alrededor de su centro Android

Estoy obteniendo resultados extraños con el código siguiente:

iv = (ImageView) findViewById(R.id.iv); iv.setImageResource(R.drawable.spinner_white_76); Animation a = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, iv.getDrawable() .getIntrinsicWidth() / 2, Animation.RELATIVE_TO_SELF, iv.getDrawable().getIntrinsicHeight() / 2); a.setRepeatCount(-1); a.setDuration(1000); iv.startAnimation(a); 

¿Cuál es la forma correcta de especificar el punto del eje (centro del dibujable)?

Se siente estúpido! Lo tengo que trabajar después de pasar algún tiempo leyendo de cerca la documentación:

 Animation a = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); a.setRepeatCount(-1); a.setDuration(1000); 

Tenga en cuenta que esto no funcionará si el objeto tiene relleno asimétrico (por ejemplo, un relleno izquierdo de 5px y un relleno derecho de 0px), ya que el relleno se considera parte del objeto. Lo que significa que el centro calculado será desplazado.

Una solución a esto es utilizar márgenes en lugar de relleno si está utilizando relleno por razones de diseño. Como dice el API sobre los márgenes: "Este espacio está fuera de los límites de esta vista". ( ViewGroup.MarginLayoutParams )

Esto significa que los márgenes no girarán como lo hace el relleno.

Como el título para estas preguntas es Drawable Rotating alrededor de su centro (y yo estaba buscando exactamente por él, no lo había encontrado y tuve que implementarlo yo mismo) me gustaría publicar mi solución / respuesta.

Acabé escribiendo personalizado dibujable que puede envolver cualquier dibujable y permitir su rotación. Aquí está el código:

 public class RotatableDrawable extends DrawableWrapper { private float rotation; private Rect bounds; private ObjectAnimator animator; private long defaultAnimationDuration; public RotatableDrawable(Resources resources, Drawable drawable) { super(vectorToBitmapDrawableIfNeeded(resources, drawable)); bounds = new Rect(); defaultAnimationDuration = resources.getInteger(android.R.integer.config_mediumAnimTime); } @Override public void draw(Canvas canvas) { copyBounds(bounds); canvas.save(); canvas.rotate(rotation, bounds.centerX(), bounds.centerY()); super.draw(canvas); canvas.restore(); } public void rotate(float degrees) { rotate(degrees, defaultAnimationDuration); } public void rotate(float degrees, long millis) { if (null != animator && animator.isStarted()) { animator.end(); } else if (null == animator) { animator = ObjectAnimator.ofFloat(this, "rotation", 0, 0); animator.setInterpolator(new AccelerateDecelerateInterpolator()); } animator.setFloatValues(rotation, degrees); animator.setDuration(millis).start(); } @AnimatorSetter public void setRotation(float degrees) { this.rotation = degrees % 360; invalidateSelf(); } /** * Workaround for issues related to vector drawables rotation and scaling: * https://code.google.com/p/android/issues/detail?id=192413 * https://code.google.com/p/android/issues/detail?id=208453 */ private static Drawable vectorToBitmapDrawableIfNeeded(Resources resources, Drawable drawable) { if (drawable instanceof VectorDrawable) { Bitmap b = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); drawable.setBounds(0, 0, c.getWidth(), c.getHeight()); drawable.draw(c); drawable = new BitmapDrawable(resources, b); } return drawable; } } 
  • ¿Cómo hacer que el drawableLeft de un Android Button siga girando?
  • Gire un botón (o el texto dentro) del código
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.