简体   繁体   English

Drawable围绕其中心Android旋转

[英]Drawable Rotating around its center Android

I am getting strange results with the following 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);

Whats the right way to specify the axis point (center of the drawable)? 什么是正确的方式来指定轴点(可绘制的中心)?

Feel stupid! 感觉很蠢! Got it to work after spending some time closely reading the documentation: 花了一些时间仔细阅读文档后才能使用它:

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

Note that this will not work if your object has asymmetric padding (for example a left padding of 5px and a right padding of 0px), because padding is considered part of the object. 请注意,如果您的对象具有非对称填充(例如,左边填充为5px,右边填充为0px),则此操作无效,因为填充被视为对象的一部分。 Which means the computed center will be offset. 这意味着计算中心将被抵消。

One solution to this is to use margins instead of padding if you are using padding for layout reasons. 解决此问题的一种方法是,如果出于布局原因使用填充,则使用边距而不是填充。 As the API says concerning margins: "This space is outside this view's bounds." 正如API所说的关于边距:“这个空间超出了这个观点的范围。” ( ViewGroup.MarginLayoutParams ) ViewGroup.MarginLayoutParams

This means that margins will not rotate as padding does. 这意味着边距不会像填充那样旋转。

As title for this questions is Drawable Rotating around its center (and I was searching exactly for it, had not found it and had to implement it myself) would like to post my solution/answer. 因为这个问题的标题是Drawable围绕它的中心旋转(我正在寻找它,没有找到它并且必须自己实现它)想发布我的解决方案/答案。

I ended up writing custom drawable which can wrap any drawable and allow its rotation. 我最终编写了自定义drawable,可以包装任何drawable并允许其旋转。 Here is the 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;
    }
}

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

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