Frage

Ich erhalte seltsame Ergebnisse mit dem folgenden Code:

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

Was ist der richtige Weg, um den Achsenpunkt (Mitte des ziehbar) angeben?

War es hilfreich?

Lösung

Fühlen Sie sich dumm! Verstanden zu arbeiten, nachdem er einige Zeit eng Lesen der Dokumentation zu verbringen:

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

Andere Tipps

Beachten Sie, dass dies nicht funktionieren, wenn Ihr Objekt asymmetrische Polsterung hat (zum Beispiel eine linke Polsterung von 5px und eine rechte Polsterung von 0px), weil Polsterteil des Objekts betrachtet wird. Was bedeutet, dass das berechnete Zentrum wird ausgeglichen werden.

Eine Lösung hierfür ist die Margen statt Polsterung zu verwenden, wenn Sie Polsterung aus layouttechnischen Gründen verwenden. Da die API sagt Margen in Bezug auf: „Dieser Raum außerhalb Ansicht dieser Grenzen ist“ ( ViewGroup.MarginLayoutParams )

Das bedeutet, dass die Margen nicht drehen, wie Polsterung der Fall ist.

Als Titel für diese Fragen ist Drawable seine Mitte Drehung um (und ich war auf der Suche genau dafür hatte fand es nicht und hatte es selbst zu implementieren) möchte meine Lösung / Antwort schreiben.

Ich landete benutzerdefinierte ziehbar schreiben, die jede ziehbar wickeln können und ermöglichen, seine Drehung. Hier ist der Code:

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;
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top