Drawable rotante attorno al suo centro Android
Domanda
sto ottenendo risultati strani con il seguente codice:
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);
Che cosa è il modo giusto per specificare il punto dell'asse (centro della drawable)?
Soluzione
sentire stupido! Capito al lavoro dopo aver trascorso qualche tempo a stretto contatto lettura della documentazione:
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);
Altri suggerimenti
Si noti che questa non funziona se l'oggetto ha un'imbottitura asimmetrica (ad esempio, un padding sinistro di 5px e un diritto di imbottitura 0px), perché imbottitura è considerato parte dell'oggetto. Il che significa che il centro calcolata sarà compensato.
Una soluzione a questo è quella di utilizzare i margini invece di imbottitura se si utilizza imbottitura per motivi di layout. Come l'API dice riguardanti margini: "Questo spazio è al di fuori dai limiti di questo punto di vista" ( ViewGroup.MarginLayoutParams )
Questo significa che i margini non ruotano come imbottitura fa.
Per quanto titolo per questa domanda è Drawable di rotazione attorno al suo centro (e stavo cercando esattamente per questo, non aveva trovato e aveva per la sua attuazione me) vorrebbe inviare la mia soluzione / risposta.
ho finito per scrivere drawable personalizzato che può avvolgere qualsiasi drawable e consentirne la rotazione. Ecco il codice:
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;
}
}